C
Chris S.
Guest
Hey guys, is there any way to detect a changed variable in the Step event and then for a loop complete an action only once until the variable changes again?
previousVariable = myVariable
myVariable = [get value]
if (previousVariable != myVariable) then
{
[do code that runs once until the variable changes]
}
CREATE
Value = 0;
OldValue = 0;
STEP
if(Value != OldValue){
// Stuff
OldValue = Value;
}
Then you should find out for yourself what that variable is and what's in it, and stay out of threads that aren't yours.but what if you don't know the value in variable ?????
no Lets Say That if ....Then you should find out for yourself what that variable is and what's in it, and stay out of threads that aren't yours.
It's your responsibility to know what's going on in your own projects, not ours. It's also your responsibility to read and obey these posting guidelines.
Then you simply reference myVariable by mentioning it without quotes.no Lets Say That if ....
myVariable = random()
then how i would be able to find the value in myVariable??
myVariable = random(100);
show_message(myVariable);
myVariable = random(100);
if (myVariable <= 50) {
show_message("50 or less");
} else {
show_message("More than 50");
}
myVariable = random(100);
show_message(myVariable);
show_message(myVariable);
show_message(myVariable);
If I could chime in here, this didn't work for me until I set the value equal to another variable in play. I also couldn't then set the values equal to one another, because the operation I needed to perform was to destroy the instance being manipulated prior to creating a new instance of the same object. So here's another way to do it. Using an existing counter in my game that incremented with each turn, I set the "OldValue" (shown below as "oValue") in the create event. I then set the new "Value" (shown below as "nValue") in the step event. So the code would look like this:Nux's code would run just fine. Let's say myVariable equals five. In the step, you store this value to previousVariable. Then you run whatever code that gives myVariable a new value. Let's say it returns five again. As you next compare the variables, their values are equal, so conditional code block will not run. Next step, you do the same again: previousVariable gains the value five, but as myVariable receives the value of six, the if-check is true (five does not equal six) and the conditional code block runs. The next step over, previousVariable becomes six, and as long as myVariable keeps receiving the value six, the condition will not be true.
Edit: now if the value is being changed externally, that is, object A reaches into object B and changes its values, you'd need to check a little differently.
This runs the conditional code only when it detects that Value has changed since last time step event was run.Code:CREATE Value = 0; OldValue = 0; STEP if(Value != OldValue){ // Stuff OldValue = Value; }
CREATE:
oValue = global.counter;
STEP:
nValue = global.counter;
if (nValue != oValue) {
do something;
}
CREATE:
runActionOnce = false;
runAction = false;
STEP:
if (EXTERNAL_CONDITION == true){
runActionOnce = true;
}
if (runActionOnce == true && runAction == false){
runAction = true;
//DO SOMETHING
}
Why not just use one flag variable?Wanted to give my 2-cents. As a beginner, I was running into the same problem, and Googling kept bringing up this same forum post. None of the above solutions worked for what I was trying to do, but I finally figured out a solution that I find easy to read and modify.
It's helped me a lot, and I hope it helps others too that just want to run an event once in a Step Event:
With a true/false event being triggered on every frame, this will execute the code only once, even though it's in a Step Event, and won't trigger again until you set runActionOnce and runAction both to false again somewhere else. Hope this helps!GML:CREATE: runActionOnce = false; runAction = false; STEP: if (EXTERNAL_CONDITION == true){ runActionOnce = true; } if (runActionOnce == true && runAction == false){ runAction = true; //DO SOMETHING }
Happy coding!
if (EXTERNAL_CONDITION == true) && (runActionOnce = false){
runActionOnce = true;
//DO SOMETHING
}