Fangames > Programming Questions
Making GM remember a variable
(1/1)
RandomFangamer:
Hi, :denKamilia:
So I have just finished making my first boss and when it is killed I have it set a variable (trollkilled) to true
However, when I press r it resets trollkilled to whatever it was originally :denRin:
I am wondering, is there a way to make a variable not reset when the game closes or I press R?
I am guessing that there is some way to store it in the save, but I don't know how.
Code:
(click to show/hide)if room = rTrollBossP2{
room_goto(roomselect)
global.trollkilled = true
sound_play8(sndTeleporterActivate)
with other instance_destroy()
}
Thanks for reading this
YoSniper:
The only way to get the game to permanently remember a variable like that is to put that variable in a persistent object like world.
However, even then, the variable will reset if you restart the game (or shut it down and start it back up.) So for an even more permanent solution, include that variable in your save data (again, remember to save it within the world object.)
RandomFangamer:
How do you put the variable in world?
I have it so that when the player collides with an object (the warp) the variable gets set to true.
YoSniper:
Either set the variable in the world object, or an alternative is to just use a global variable (but it still requires initialization, which is best done in the Create Event of the world object, anyway.)
If the variable starts out as false, and is later set to true upon that collision, say:
Create Event of world
--- Code: ---myVariable = false;
--- End code ---
Collision Event with object
--- Code: ---world.myVariable = true;
--- End code ---
Then you would just need to save "world.myVariable" in your save data.
But please, for the sake of code maintainability, don't name the variable "myVariable".
pieceofcheese87:
The best way is to write it to the savefile.
In the saveGame script, add this at the end of the list of variables, but before file_bin_close(f);
--- Code: ---file_bin_write_byte(f,global.trollkilled)
--- End code ---
This will write the variable to your savefile when you hit a savepoint.
Now, assuming you're using the yuuutu engine or similar, you need to edit the saveExe script so that it loads the variable from the savefile when you restart.
Again, after everything but before file_bin_close(f); put this:
--- Code: ---global.trollkilled = file_bin_read_byte(f);
--- End code ---
Now it should save the variable.
oh, and a reminder to set the global variable in your warp collision event, instead of just the warp's variable. (global.trollkilled=true instead of trollkilled=true)
Navigation
[0] Message Index
Go to full version