Playmaker Forum
PlayMaker Help & Tips => PlayMaker Help => Topic started by: Codybean1 on October 21, 2017, 12:05:52 AM
-
So I'm writing a custom action that is meant to work like Get Key, but it reads from a keyCode variable instead of a predetermined key set by the developer. This action currently mostly works as intended, but strangely doesn't work for a keyboard key when a mouse button is also held down.
To clarify, if the keyCode variable's value IS a mouse button, it works. If the value is a keyboard key, it works IF AND ONLY IF a mouse button isn't also being pressed, and I want the action to work for keyboard keys regardless if a mouse button is pressed or not.
Here's the code so far. StoreResult always returns false if a mouse button is held and a keyboard key is being tested, and I'm not sure why.
void DoGetKey()
{
int kHit = 0;
if (key==KeyCode.None){ //key is referring to the original 'key' variable in the old GetKey Action
foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode))){
if (Input.GetKey(vKey)){
keyE = vKey;
kHit++;
if (key2.Value.ToString()==keyE.Value.ToString()){
storeResult.Value = true;
continue;
}else {
storeResult.Value = false;
}
}
}
}
else{
storeResult.Value = Input.GetKey(key);
kHit++;
} if (kHit == 0){
storeResult.Value = false;
}
}
-
Okay, so I got it to work. It was a simple matter of the else statement mucking it up because 'mouse0' appears later in the KeyCode list than many keyboard keys. I removed the else statement and moved 'store.Result.Value = false' right before the 'foreach'. I also got rid of the 'khit' variable and cleaned the whole thing up a bit. I wrote this all up as reference in case anyone in the future will try something similar. :)
Here's the final code:
void DoGetKey(){
if (key==KeyCode.None){
storeResult.Value = false;
foreach (KeyCode vKey in System.Enum.GetValues(typeof(KeyCode))){
if (Input.GetKey(vKey)){
keyE = vKey;
if (key2.Value.ToString()==keyE.Value.ToString()){
storeResult.Value = true;
}
}
}
}else{
storeResult.Value = Input.GetKey(key);
}
}
-
Hi,
ok, it's a good effort, indeed I think we need a custom action to allow for this.
But I think your code is too complex for what's needed.
https://docs.unity3d.com/ScriptReference/Input.GetKey.html
getkey accepts a string to begin with, so you don't need to iterate through keys and compare, you simply ask directly.
have you tried this?
Bye,
Jean