As I understand it, if I use the "restore" function without naming a variable it will set all variables to their "stored" value.
Does the same work for the "store" function (ie. if I just write "store" will it save all variable's values in persistent storage)?
My adventure game uses tons of variables and I'm trying to figure out a simple way of creating a save function, since it will take upwards of 2hrs to complete.
So, say I have a door that unlocks with a key. Once I unlock the door, the "haskey" variable then sets to 0. If I restore the game, the player's key will be gone (haskey=0) but the door will revert to being locked again.
EDIT: the door swaps for another tile (unlocked door) in this scenario by the way.
Presumably "haskey" starts the game at 0, gets set to 1 when you pick up the key, then gets set back to 0 when you unlock the door. That means if we are restoring the game we won't know if "haskey" is 0 because the player never picked up the key, or because they did but then unlocked the door. Instead you probably want to store a variable called something like "unlocked_door".
This is a kind of "flag" - we can use flags to track progress with certainty as we make sure they only have a value of 0 if not yet triggered, and a value of 1 thereafter.
If we took a completely flag-based approach, we would replace "haskey" with a flag called something like "key_picked_up". Then the three states of the game are unambiguously:
picked_up_key = 0, unlocked_door = 0
picked_up_key = 1, unlocked_door = 0
picked_up_key = 1, unlocked_door = 1
It's not necessary to only store flags - if you want the player to pick up multiple keys, then it makes sense to have a keys variable that can go up and down and to store that on saving, but if you think about where the player finds each key you will probably realise that you will need a flag for each key anyway so you know which keys have and haven't been collected.
You don't need flags for every single thing that permanently changes providing things change together. If opening the door means a character's dialogue changes elsewhere for example, you can use the unlocked_door flag when deciding what dialogue to say.
For the actual mechanics of making sure the door is unlocked when unlocked_door is set to 1, I would put this in the room's enter event like this:
on enter do
if unlocked_door==1 then
swap "white" at x,y
end
end
If progress is linear you might be able to simplify having multiple flags to just one stored variable that has multiple values. Then rather than checking if a flag has been set or not, you check whether stage (or whatever) is greater than a certain number. Because the door is always unlocked after picking up the key the above states could be
stage = 0 (not yet picked up key)
stage = 1 (picked up key)
stage = 2 (unlocked door)
and then on enter you can have
on enter do
if stage>=3 then
swap "white" at x,y
end
end
Update: I've created flags for every variable in my game and "stored" their values. When I "restore" the game, all of the values return correctly. Hooray!
Except... the one variable that isn't working is the variable that stores the player's last room they entered. I've tried this in the Room script:
on enter do
lastRoom = event.room
store "lastRoom"
end
But that didn't work. Then I tried this on the Player script:
on update do
lastRoom = "NameofRoom"
store "lastRoom"
end
That didn't work either.
EDIT: Did a little more experimentation. Apparently, the variable is being stored when the player leaves the room instead of entering. Which is strange because I have the script running on on enter do. Thoughts?