0
Completed

Combine Buttons?

Aeon FitzGerald 1 week ago in Game Creator updated by Marti (Lead Developer) 5 days ago 1

I want to trigger an action when pressing two buttons, i.e A + B = action 

i'm not sure if their is a way to do this with conditions or if its not implemented yet, what do you recommend? 

thanks in advance. 

Unity version:
2019.2
Game Creator version:
1.1.2

Answer

Answer
Completed

That's a good question. This is an apparent trivial problem, but hides a bit of complexity. When a user presses two buttons, it doesn't do that at the same frame. There are a few milliseconds between one key being pressed and the other key.

So, in order to detect when two keys are pressed at the same time I would create the following scenario:

- Create a Local Variable "buttons-pressed" initializing it to zero.

- Create a Trigger that detects when A is pressed, which calls an Action that adds +1 to "button-pressed" variable.

- Create a Trigger that detects when B is pressed, which calls an Action that adds +1 to "button-pressed" variable.

- Create a Trigger that detects when A is released, adding to the "button-pressed" variable -1.

- Create a Trigger that detects when B is released, adding to the "button-pressed" variable -1.

Now when no key is pressed, the local variable will have a value of 0.

When one of the keys is pressed, the value will be 1

If both keys are pressed, the value will be 2.

So, all that's left to do is create one last Trigger that detects a change in the "button-pressed" variable. Use a Condition to check if "button-pressed" equals 2. If so, it means both keys are being pressed.

Cheers!

GOOD, I'M SATISFIED

Thank you! 

Satisfaction mark by Aeon FitzGerald 5 days ago
Answer
Completed

That's a good question. This is an apparent trivial problem, but hides a bit of complexity. When a user presses two buttons, it doesn't do that at the same frame. There are a few milliseconds between one key being pressed and the other key.

So, in order to detect when two keys are pressed at the same time I would create the following scenario:

- Create a Local Variable "buttons-pressed" initializing it to zero.

- Create a Trigger that detects when A is pressed, which calls an Action that adds +1 to "button-pressed" variable.

- Create a Trigger that detects when B is pressed, which calls an Action that adds +1 to "button-pressed" variable.

- Create a Trigger that detects when A is released, adding to the "button-pressed" variable -1.

- Create a Trigger that detects when B is released, adding to the "button-pressed" variable -1.

Now when no key is pressed, the local variable will have a value of 0.

When one of the keys is pressed, the value will be 1

If both keys are pressed, the value will be 2.

So, all that's left to do is create one last Trigger that detects a change in the "button-pressed" variable. Use a Condition to check if "button-pressed" equals 2. If so, it means both keys are being pressed.

Cheers!