• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM Variables resetting[Solved]

W

Wild_West

Guest
I've been trying to solve this for the last week and I've tried literally everything I can think of.
Looking at the variable's state in the debugger, using a message to show if I'm making an object duplicate,
removing the code that says to reset the value to false. Nothing is fixing it and I don't know why.

But my problem is I'm trying to make a collectables system where once I grab something I don't have to recollect it again f I die. The problem of course is, this is not working. I grab the items, I die, the variable for tracking if I've collected them is reset to false.

The way I set it up, or tried to is that if the number of items I want to collect is at zero that means I have them all since that's the only way they're ever destroyed. so once the instance number is zero I set the collection variable for that room to true. and this variable gets set in a persistent object so the value shouldn't be getting reset, yet it is. why?


switch( room )
{
case solar_space_section_1 :
if not( instance_exists(bronze_bit_piece) )
{ solar_1_bronze_bit_pieces_collected = true; }
//else
//{ solar_1_bronze_bit_pieces_collected = false; }

if not( instance_exists(copper_bit_piece) )
{ solar_1_copper_bit_pieces_collected = true; }
//else
//{ solar_1_copper_bit_pieces_collected = false; }

if not( instance_exists(silver_bit_piece) )
{ solar_1_silver_bit_pieces_collected = true; }
//else
//{ solar_1_silver_bit_pieces_collected = false; }

if not( instance_exists(gold_bit_piece ) )
{ solar_1_gold_bit_pieces_collected = true; }
//else
//{ solar_1_gold_bit_pieces_collected = false; }
break;
 

chamaeleon

Member
Here's a fun thing you can try
Code:
show_debug_message("About to set or reset collected items");
switch( room )
{ 
    case solar_space_section_1 :
    show_debug_message("Room is solar_space_section_1");
    if not( instance_exists(bronze_bit_piece) )
    {
        show_debug_message("Bronze collected set to true");
        solar_1_bronze_bit_pieces_collected = true; 
    }
    else
    {
        show_debug_message("Bronze collected set to false");
        solar_1_bronze_bit_pieces_collected = false;
    }
    /// and so on..
The idea is simply to give you a trace in the debug console what your program is doing, rather than guessing. If you see some messages more than you expect, you figure out what the cause is, and then you correct your code or object configuration as necessary. If you don't know why something is happening, find data to help you figure it out. show_debug_message(), breakpoints and the debugger are very useful tools for this.
 
W

Wild_West

Guest
Here's a fun thing you can try
Code:
show_debug_message("About to set or reset collected items");
switch( room )
{
    case solar_space_section_1 :
    show_debug_message("Room is solar_space_section_1");
    if not( instance_exists(bronze_bit_piece) )
    {
        show_debug_message("Bronze collected set to true");
        solar_1_bronze_bit_pieces_collected = true;
    }
    else
    {
        show_debug_message("Bronze collected set to false");
        solar_1_bronze_bit_pieces_collected = false;
    }
    /// and so on..
The idea is simply to give you a trace in the debug console what your program is doing, rather than guessing. If you see some messages more than you expect, you figure out what the cause is, and then you correct your code or object configuration as necessary. If you don't know why something is happening, find data to help you figure it out. show_debug_message(), breakpoints and the debugger are very useful tools for this.
Hm, I put yours in but the message never changed
 

chamaeleon

Member
Hm, I put yours in but the message never changed
Then search your project for other instances where you assign a value to the collected variables (perhaps a Create event, or a script called from a create event) and insert show_debug_message() statements there as well. What I showed was not supposed to help solve the problem entirely (although it might have, it didn't apparently), but rather show you a methodology for digging deeper on your own.
 
W

Wild_West

Guest
Then search your project for other instances where you assign a value to the collected variables (perhaps a Create event, or a script called from a create event) and insert show_debug_message() statements there as well. What I showed was not supposed to help solve the problem entirely (although it might have, it didn't apparently), but rather show you a methodology for digging deeper on your own.
I did all that already, like 3 times. there's nothing trust me
 

chamaeleon

Member
It is probably really a stretch, but the setting code you use above does run in the context of the same instance that you later check whether all variables has been set to true..?
 

chamaeleon

Member
Desperate option: open up a powershell window, cd to your project directory and see what lines "findstr /s pieces_collected" results in, and see if anything looks out of place.
 
W

Wild_West

Guest
A number doesn't change by itself without code to do it.. How about instance creation code in the room editor?
I know it shouldn't be changing that's why I've been baffeld by this all week. There's nothing that says to reset it. The one thing I do have that says to reset is commented out
 
W

Wild_West

Guest
It is probably really a stretch, but the setting code you use above does run in the context of the same instance that you later check whether all variables has been set to true..?
No it runs in an object that follows me through all the rooms. I said that in my first post already
 

chamaeleon

Member
I know it shouldn't be changing that's why I've been baffeld by this all week. There's nothing that says to reset it. The one thing I do have that says to reset is commented out
I just imagine there must be another setting to false/0 elsewhere besides this reset, or your check to see whether it is true or false in order to determine whether you're done collecting per your first post, or you'd get some kind of error when that code tries to run..
 
W

Wild_West

Guest
Desperate option: open up a powershell window, cd to your project directory and see what lines "findstr /s pieces_collected" results in, and see if anything looks out of place.
I don't even know what that is
I just imagine there must be another setting to false/0 elsewhere besides this reset, or your check to see whether it is true or false in order to determine whether you're done collecting per your first post, or you'd get some kind of error when that code tries to run..
I mean Game maker has a search in script feature. and I already used it to look for solar_1_copper_bit_pieces_collected = false;. It's only in 2 places, the code in the step event that I already commented out, and the create event of the persistent object
 

chamaeleon

Member
It's only in 2 places, the code in the step event that I already commented out, and the create event of the collectable object.
So you are saying that a show_debug_message() the line before setting it in the create event, and in the script above where you set lines to true, and the line where you finally check whether all have been collected results in
1. false
2. true
3. false
for one of these variables, without a repeating output from show_debug_message() about setting it to false?
 

chamaeleon

Member
Probably unlikely, but since you have very long names for your variables, no chance the create event and the check event have a different name by mistake than the names used in the code above? It would also explain it.
 
W

Wild_West

Guest
Probably unlikely, but since you have very long names for your variables, no chance the create event and the check event have a different name by mistake than the names used in the code above? It would also explain it.
If they had different names wouldn't that give me a reference error too?
 
W

Wild_West

Guest
Not in this case.
1. Set name_a to false
2. Set name_b to true
3. Check name_a for false/true
Okay well I just checked them all in the order you said, it all sets to the right values when it should but it resets the "is collected" variable to false when the room starts again. so the collectable objects must be ignoring the script I set where it says "If it's recorded that I've been collected already, destroy myself"
 
W

Wild_West

Guest
What happens when you die? What code runs?
Basically after death character becomes invisible, timer starts, counts for like 3 seconds , and then you go to the You died screen, room restarts after 3 seconds.
But it's okay I found the problem actually.
 
W

Wild_West

Guest
What was the problem if you don't mind. For future reference in case people will read this thread who have a similar issue.
Actually false alarm, I thought I solved it because the collectables weren't appearing but I reset the game to test it again, and the same problem is still there.
 
script I set where it says "If it's recorded that I've been collected already, destroy myself"
Please post the code for this script and which events it runs in.

Basically after death character becomes invisible, timer starts, counts for like 3 seconds , and then you go to the You died screen, room restarts after 3 seconds.
Instead of a description, it would be more helpful to post the actual code when someone asks you for it.

Have you checked all your room start events if you have them.

You say the objects destroy themselves after running your checking script...

but also when you do a room start the objects obviously will return as you placed them in the room editor.

You say there's nothing that's resetting your code - but unless there is a bug, there's gotta be a script thats setting things back to normal. (Or your code has become sentient and is re-writing itself)
 
W

Wild_West

Guest
Please post the code for this script and which events it runs in.



Instead of a description, it would be more helpful to post the actual code when someone asks you for it.

Have you checked all your room start events if you have them.

You say the objects destroy themselves after running your checking script...

but also when you do a room start the objects obviously will return as you placed them in the room editor.

You say there's nothing that's resetting your code - but unless there is a bug, there's gotta be a script thats setting things back to normal. (Or your code has become sentient and is re-writing itself)
If I were unsure about it I would have posted it. but the only thing the code does literally is restart the room. It's not relevant.

I DID actually find the problem is most likely my save and load scripts, because when I took out using load_game() the code for the collectables worked and they destroyed themselves like they should have after I'd already gotten them and restarted the room.

But I don't know why it's not saving and loading right. Because my other save progress works fine.
 
ok, so you sound like you know what you are doing. However, I'm going to state the obvious here then.

If you are using the built-in load_game() function, this resets the entire game state to what it was when you used the save_game() function. This would include any variables that you had previously set. Which would possibly go someway towards explaining why your code is resetting its values. Unless you save the state of your variables to an external file like an INI file for example, and then reload that data after calling load_game().

It's worth noting here for those that don't know, if you use the built-in load_game() and save_game() functions, any time you make a new build/version of your game, any existing saved game data will usually become invalid, basically you lose your saved game state.

It's recommended to make your own custom save game routine in most cases for this reason alone.
 
W

Wild_West

Guest
ok, so you sound like you know what you are doing. However, I'm going to state the obvious here then.

If you are using the built-in load_game() function, this resets the entire game state to what it was when you used the save_game() function. This would include any variables that you had previously set. Which would possibly go someway towards explaining why your code is resetting its values. Unless you save the state of your variables to an external file like an INI file for example, and then reload that data after calling load_game().

It's worth noting here for those that don't know, if you use the built-in load_game() and save_game() functions, any time you make a new build/version of your game, any existing saved game data will usually become invalid, basically you lose your saved game state.

It's recommended to make your own custom save game routine in most cases for this reason alone.
Yeah it's an INI file setup I actually didn't even know they had their own save ad load game functions I just titled mine save_game and load_game
 
Yeah it's an INI file setup I actually didn't even know they had their own save ad load game functions I just titled mine save_game and load_game
Ah, slap my forehead, yeah sorry, got the spelling backwards, the built in functions are game_save() and game_load(). Ignore the previous info with respect to your game.
 
W

Wild_West

Guest
Ah, slap my forehead, yeah sorry, got the spelling backwards, the built in functions are game_save() and game_load(). Ignore the previous info with respect to your game.
The strange thing about it though is I use the same stuff basically for saving stuff like level progress and info I get about enemies and those stay when I close and reopen the game.
So it's literally just these collectables that keep resetting. Because it only seems to run the code for not needing to collect them twice if I don't call load_game() and I don't know why
 
W

Wild_West

Guest
Ah, slap my forehead, yeah sorry, got the spelling backwards, the built in functions are game_save() and game_load(). Ignore the previous info with respect to your game.
EDIT :

Okay so I found out what about my save and load was messing things up. You were right technically, it was that I was reloading the old data, because I wasn't calling save_game() when I should have been. So now it works, but I tried scripting this

Code:
if not( instance_exists(bronze_bit_piece) )
    {
       solar_1_bronze_bit_pieces_collected = 1;
    }
    else
    if( instance_exists(bronze_bit_piece) )and( solar_1_bronze_bit_pieces_collected == 1)
    {
       with( bronze_bit_piece )
       { instance_destroy(); }
    }
   
    //bit_piece_collecting( bronze_bit_piece, solar_1_bronze_bit_pieces_collected );
Substituting the value to check for and which bit piece to affect with arguments 0 and 1, but it only seems to work if I use the raw version not the scripted one with arguments.
 
Well, you could still put the code in a script, if you want to keep things tidy in whatever event/script the code is running in.

You just wouldn't pass the variables as arguments, and it would just be moving the code you have now to another location.
 
W

Wild_West

Guest
Well, you could still put the code in a script, if you want to keep things tidy in whatever event/script the code is running in.

You just wouldn't pass the variables as arguments, and it would just be moving the code you have now to another location.
Yeah I probably should do then.
 
Top