I Wanna Community

Fangames => Game Design => Programming Questions => Topic started by: RandomFangamer on June 22, 2015, 08:25:24 PM

Title: Making GM remember a variable
Post by: RandomFangamer on June 22, 2015, 08:25:24 PM
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)

Thanks for reading this
Title: Re: Making GM remember a variable
Post by: YoSniper on June 22, 2015, 08:37:57 PM
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.)
Title: Re: Making GM remember a variable
Post by: RandomFangamer on June 22, 2015, 09:36:50 PM
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.
Title: Re: Making GM remember a variable
Post by: YoSniper on June 22, 2015, 10:11:45 PM
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: [Select]
myVariable = false;
Collision Event with object
Code: [Select]
world.myVariable = true;
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".
Title: Re: Making GM remember a variable
Post by: pieceofcheese87 on June 22, 2015, 11:53:59 PM
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: [Select]
file_bin_write_byte(f,global.trollkilled)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: [Select]
global.trollkilled = file_bin_read_byte(f);
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)