Tap/Touch Button Control Template

pHghostpHghost London, UKPosts: 1,885
edited February 11 in Community Tutorials

Here's a simple thing I put together to manage control for things like pressing buttons, especially in menus etc.

There are basically two types of buttons:

  1. the first is activated by pressing it down only, and this one will usually stay activated as long as you hold it (say a button to control an actor moving left/right). This button has only 2 states, pressed or released.
  2. the second is activated by pressing it down and then letting it go (usually menu buttons and similar). This button really has three states, pressed, released and neutral.

GameSalad is great with type 1 buttons, but sucks terribly with the other -- thus the purpose of this simple template.

You might be asking: wait...why does it suck?

You might not have come across this issue before, in which case good for you!

The issue with the second type of button is that if you use more requirements for pressing the button, it will activate itself repeatedly without being pressed again. For example, let's say it is a button to collect some reward. Every 10 seconds, the player gets a reward he/she can collect. So, the rule in the button will say

WHEN ALL: game.reward_ready = 1 AND touch is released ==>

CHANGE: game.rewards TO: game.rewards + 1 AND game.reward_ready TO: 0

Now, a timer in a rule will be set, that will

WHEN ALL: game.reward_ready = 0 ==>

AFTER 10 seconds, CHANGE: game.reward_ready TO: 1.

The problem is that the 'released' state as implemented by GameSalad will see the button you pressed and let go once as 'released' forever, unless it is currently being pressed. So it will still 'say' released even if you don't push it a second time. This way, once the next reward is ready, game.reward will become 1 and your rule to collect the reward will trigger automatically, with no need for the button to be touched again, because both statements will suddenly be valid again at the same time. It is enough for one statement in a whole rule sequence to go from valid to invalid and then back to valid for a rule to trigger repeatedly.

For most buttons like this, it makes no sense to see them as 'released' all the time. They are sitting idle, neutral, not in use. Being released is a state right after you stop touching them, not five minutes later.

So yeah, you might not have come across this issue before, in which case good for you! But if you have, or you ever do, this little template is here for you.

P.S. The template can be used for both types of buttons I described, the button will still maintain its pressed state if held down, so it's cool to use for walking controls and similar, just change the trigger state to 1 instead of 2 (you can even use both states as triggers for fancy effects). What this template basically does is turns any button into a proper 3-state, pressed, released and neutral button. You can put the code into every single button in your game.

Here is the download link: Tap Control


Sign In or Register to comment.