buttonSingleClicked() performed out of time?

Hi everyone.

I’m sharing here this problem just to know if someone is having it and how to solve it.

If the action buttonSingleClicked() is performed on the main Loop() but you dont have any action for that click in the main Loop(), the blink “save” that action and perform it later if he find that action on another loop.

Example:

void loop() {
if (buttonDoubleClicked()) {
secondloop();
}
}
void secondloop() {
if (buttonSingleClicked()){
setColor(RED)
}
}

If I CLICK on the first loop, that action is saved, and if then I do a DOUBLECLICK, the first click is performed in the second loop (not always, but sometimes at random points, that allow the game to do random actions).

I managed to solve it adding an empty action to the main loop:
if (buttonSingleClicked()) { }

Am I doing something wrong?

Thank you for the help!!

This is expected. When a button is clicked, an internal flag is set and it is not reset until buttonSingleClicked() is called. I actually wrote about this:

1 Like

Thank you!

I will check the full thread to understand it better and know how to fix it in future games :slightly_smiling_face:

Yea, the semantics here are confusing. What would be better? Having all these state-checking functions unconditionally self clear after each loop() return? Having explicit clear functions?

Or how about going to a pure event-loop model like…

void loop( Event event)

…where the event stream is serialized?

Or go full Elm with something like…

BlinkState loop( BlinkState oldBlinkState, Event newEvent);

…? I kind of like that one :slight_smile:

Other ideas?

I am not a fan of this one because this would cause clicks that were just missed in a loop call be consumed and then the next loop call would not have it available. This makes Blinks feel unresponsive and make it look like it did not detect a click (I had this issue in Hexxagon and I now only reset button clicks on states that consume them (there are still drawbacks if you are not careful. Like a click in a Blink that happened sometime ago suddenly being registered on a state change).

I am always in favor of flexibility for developers, but I also happen to think that the consume when checking is fine.

1 Like