SOLVED Need some eyes real quick

flyinian

Member
If I have this:

CREATE:
GML:
Variable_1 = false;

Variable_A = false;

STEP EVENT:
GML:
if(a == b)
{
    Variable_1 = true;
}
else
{
    Variable_1 = false;
}
STEP EVENT:
GML:
if(Variable_1)
{
Variable_A = true;
};
Then the following code should only execute once because Variable_A is initially true then set to false.

STEP EVENT:
GML:
if(Variable_A)
{
// Execute code.
Variable_A = false;
};
I've been doing this and haven't had any problems w/ this until now. This is probably a pointless post because I'll probably find the issue tomorrow after a break.
 
Last edited:

flyinian

Member
might wanna look at that again..
in your create event you have:

Variable_1 = false;
Variable_A = false;
In the create event they're set to false but, would that not matter since they're being changed in the step events? I mean, I've many other variables set to false in create events and used in step events just fine.

When I said:
Then the following code should only execute once because Variable_A is initially true then set to false.
I wasn't including the create event but, rather only the chunk of code below the quote in the op.

Thanks for the reply. I'm going to scour my code and see if I can figure it out. Further feedback is still appreciated.
 

chamaeleon

Member
Seems to me the last two step events snippets will work in tandem to let the code you say will only execute once will actually be executed more than once as steps execute over time.
 

flyinian

Member
When I go from this:
GML:
if(Variable_1)
{
// Code
Variable_1 = false;
};
To this:
GML:
if(Variable_1)
{
// Code
}
It works but, now it's "broken" because I need it to stop and idk what I'm missing.
 

TailBit

Member
Is this over several instances since there is 3 step events? If so then the instance order for the room matters..
 

flyinian

Member
Seems to me the last two step events snippets will work in tandem to let the code you say will only execute once will actually be executed more than once as steps execute over time.
How would I get it to only execute once no matter what?
 

chamaeleon

Member
How would I get it to only execute once no matter what?
By using a single variable that is initially true, and is set to false in the code block. Why you used two variables in your original example to control whether to execute code, I don't know. Since your last message says multiple instances and global variables you will need to clarify whether they use the same or different variables.
 

flyinian

Member
By using a single variable that is initially true, and is set to false in the code block.
Something like this?:

GML:
Create:

Variable_1 = true;


Step:

if(Variable_1)
{
// Code
Variable_1 = false;
}
Why you used two variables in your original example to control whether to execute code, I don't know.
I used the the two variables for clarification purposes but, I now see that is no good. Now I'm wondering if I wrote code like that somewhere.

Since your last message says multiple instances and global variables you will need to clarify whether they use the same or different variables.
If I were to create a global in instance A and then use said global variable in instance B to turn it true/false and then use the same global variable in instance C,D,E,F,etc. to run/halt code, would this work?
 
My advice: Stop trying to abstract and give the actual code. Abstraction by people running into a problem often leads them to abstract the code to do what they think should be happening because they can't see where the problem is, rather than what the actual code is doing which is directly where the problem is.
 

chamaeleon

Member
If I were to create a global in instance A and then use said global variable in instance B to turn it true/false and then use the same global variable in instance C,D,E,F,etc. to run/halt code, would this work?
If they use the same global variable it should be fairly obvious that only one instance would get to run the conditional code.

Eh, I misunderstood, I guess. if b is toggling and c through f is checking its value only, then all those instances will or will not run the code. of course, it is possible the instance order of execution puts b in the middle, so in a given step some instances may execute to code while others may not. begin and end step events help here.
 
Last edited:

flyinian

Member
My advice: Stop trying to abstract and give the actual code. Abstraction by people running into a problem often leads them to abstract the code to do what they think should be happening because they can't see where the problem is, rather than what the actual code is doing which is directly where the problem is.
Good advice. I'll look at the code and see what I need.
 

flyinian

Member
Here's the code. Its been cleaned up for readability. There is one create event and two step events involved.

GML:
/// Create event on Game Initialization:

global.Variable1 = false;


/// Step event in persistent instance_A:

// There is a "timer" that determines when A = 1. For debugging purposes, this is probably every few seconds.
if(A = 1)
{
// This works as intended.
A = 0;
// unrelated code.
global.Variable1 = true; // This works
};


/// Step event in persistent instance_B:
// With testing, It appears that the "global.Variable1 = false;" executes before the code rendering the code not executed before "global.Variable1" is set to false.
if(global.Variable1)
{
// The code that's in this if statement won't execute. Not once, not ever. If I remove the "global.Variable1 = false;" it works but, it runs continuously.
// Execute code
global.Variable1 = false;
};
P.S. W/ debug messages, it appears that the global.Variable1 returns false before it reaches the last if statement.
P.P.S. I have done searches for the variable to see if I forgot it somewhere in code and it appears all is accounted for.
P.P.P.S. With further debugging. It appears that it completely skips over the code that resides in the second if statement and immediately sets global.Variable1 to false.

P.P.P.P.S. If I change the global variable to an instance variable, it works as intended. Could someone explain why? Could this be because I'm using the same global variable in several children and they're all fighting over who gets to use the variable first and once its been set from true to false it's no longer usable until it's set to true once again? Repeating this "Battle" over who gets to use the variable before its set to false.



If I were to do:

GML:
/// Create event on Game Initialization:

global.Variable1 = false;


/// Step event in persistent instance_A:

if(A = 1)
{
// This works as intended.
A = 0;
// unrelated code.
};


/// Step event in persistent instance_B:
if(global.Variable1)
{
// This code works as intended w/ the button press code below.
// Execute code
};

if(keyboard_check_pressed(ord("L")))
{
    if(global.Variable_1)
    {
        global.Variable_1 = false;
    }
    else if(!global.Variable_1)
    {
        global.Variable_1 = true;
    };
I hope this clarifies everything up. The unrelated code shouldn't have any affect on this what so ever.
 
Last edited:

flyinian

Member
I do believe I solved my problem. Thanks for the help and replies.

Suspected Problem: If I change the global variable to an instance variable, it works as intended. Could someone explain why? Could this be because I'm using the same global variable in several children and they're all fighting over who gets to use the variable first and once its been set from true to false it's no longer usable until it's set to true once again? Repeating this "Battle" over who gets to use the variable before its set to false. Is this why room instance order was mentioned?
 
I think your problem might've been easier to fix earlier if you didn't name your variables Variable_1, etc. Using descriptive names is a debugging tool in itself.
 

flyinian

Member
I think your problem might've been easier to fix earlier if you didn't name your variables Variable_1, etc. Using descriptive names is a debugging tool in itself.
You're right. I'll add in more accuracy in future posts and avoid the whole "simplify" method that I used. Especially when code in question is this simple and short.
 
I just wanna say, I'm not trying to just tear some chunks off your back or anything. Honestly, other people on the forum (or in the wider internet) have probably solved the problem you are having, but when presented with a sterile "a == b == c" scenario, it's much harder to extrapolate to personal experience. If instead, it were "my_level == current_level_max == total_level_max" or some other scenario where we can kinda guess at what the end result should be, we can offer much more salient advice. Or even offer a solution that short circuits the problem altogether.
 

chamaeleon

Member
Could this be because I'm using the same global variable in several children and they're all fighting over who gets to use the variable first and once its been set from true to false it's no longer usable until it's set to true once again?
Since by definition a global variable is a single thing, there is only one of it with the given name in the entire game, it should be obvious that setting its value will have an effect on all subsequent reads of it by code executing after that point in time, whether by the same instance or different instances. If you don't want a global variable, because you want instances to be unaware of what another instance is doing, you of course reach for instance variables to let the instances manage their own state independently of each other.
 

flyinian

Member
I just wanna say, I'm not trying to just tear some chunks off your back or anything. Honestly, other people on the forum (or in the wider internet) have probably solved the problem you are having, but when presented with a sterile "a == b == c" scenario, it's much harder to extrapolate to personal experience. If instead, it were "my_level == current_level_max == total_level_max" or some other scenario where we can kinda guess at what the end result should be, we can offer much more salient advice. Or even offer a solution that short circuits the problem altogether.
Again, it just falls under me trying to keep things as simple as possible in the worst possible way I guess. But, I do understand what you're saying. Thanks for the advice.

P.S. I have a hard time finding similar/exact problems that I'm having. Some things are just to vague to look for. If there's some secret to finding a pre existing solution(for more vague questions/problems), please by all means let me know.
 

flyinian

Member
Since by definition a global variable is a single thing, there is only one of it with the given name in the entire game, it should be obvious that setting its value will have an effect on all subsequent reads of it by code executing after that point in time, whether by the same instance or different instances. If you don't want a global variable, because you want instances to be unaware of what another instance is doing, you of course reach for instance variables to let the instances manage their own state independently of each other.
Thanks for the clarification. I wish I had impeccable memory because, I think I've ran into this before.
 
Top