I’m working on a really simple thing - a settings menu - but having some problems with timing.
I can easily solve it by breaking a rule I’ve set for myself - I don’t want to store the same state in multiple places.
The task is - show a toggle button that reflects the current state. Update the state when pressed, read it back, and update the button text.
My first idea was simple: send a message to the audio controller to toggle its state, and read the state on the next frame:
(on_input):
msg.post("world_view:/audio#audio", "toggle_sfx")
timer.delay(0, false, update_sfx_label)
This didn’t work though. It seems that timer.delay(0) from on_input is processed on the same frame.
Next I tried a small non-zero delay. That worked most of the time, but occasionally it processed it the same frame. I could increase the delay, but a value that feels truly safe will feel laggy.
Then I tried introducing a delay by sending a message to the script:
msg.post("world_view:/audio#audio", "toggle_sfx")
msg.post(".", "update_sfx_label")
But it seems that messages sent to the current game object are always processed before others.
I can probably solve this with some additional layering of timers and messages, but is there a more elegant solution?
I wish, for example, that there was a variant of timer.delay() that allowed me to specify an integer number of frames. It would be no additional code - timer.delay_frames(2) - and the purpose of the magic number 2 could be explained in a comment.