0
Answered

how to implement a "while" condition

jpdevin 1 month ago in Game Creator updated by Marti (Lead Developer) 1 month ago 1

Hi,

Everything is in the title, I would like a condition to loop until an event happens (ex : global variable = 0). I have tried to insert a "repeat action" under my if condition, but then it loops again and again and never gets out. 

Please help.

Unity version:
2019
Game Creator version:
1.1.1

Answer

Answer
Answered

Hi JPDevin;

We get this question a lot, so it's good we can finally have a place we can relay to the answer. The thing is that we initially had a Trigger called "Update" that could constantly execute its associated Actions & Conditions, over and over again.

This works fine, but doesn't encourage good "coding" practices. This is what is commonly known as "Semaphore-Programming", where you test-check if a condition is true. If it is not, you check it the next frame until it's successful and execute it.

As you can imagine, this is not very performant. Instead, we want Game Creator to be event-based: Triggers listen (this is the keyword: listening) for certain events and react accordingly.

For example, to detect if a user presses a certain key, you could constantly check if the button is being pressed (bad) or simply add a listener to your input device and react only when it informs you back that the key was pressed.

Now going back to your questions, if you want to detect when a Global Variable is equal to zero, you can use the "On Variable Change" Trigger and call the Conditions there.

Hope this helps! :-)

Answer
Answered

Hi JPDevin;

We get this question a lot, so it's good we can finally have a place we can relay to the answer. The thing is that we initially had a Trigger called "Update" that could constantly execute its associated Actions & Conditions, over and over again.

This works fine, but doesn't encourage good "coding" practices. This is what is commonly known as "Semaphore-Programming", where you test-check if a condition is true. If it is not, you check it the next frame until it's successful and execute it.

As you can imagine, this is not very performant. Instead, we want Game Creator to be event-based: Triggers listen (this is the keyword: listening) for certain events and react accordingly.

For example, to detect if a user presses a certain key, you could constantly check if the button is being pressed (bad) or simply add a listener to your input device and react only when it informs you back that the key was pressed.

Now going back to your questions, if you want to detect when a Global Variable is equal to zero, you can use the "On Variable Change" Trigger and call the Conditions there.

Hope this helps! :-)