Help with player position save

Hello again. I'm having trouble figuring out how to save a player's position upon entering and leaving the game. For example, in my case, I enter through a title screen to begin the game and start in a room. But if I move to a new room and then leave the game, upon re-entry the character spawns in the new room.

That's what I'm trying to figure out at the moment.

thanks.

2 Likes

Did you get a solution yet?

What you're going to want to do is use the store and restore PulpScript functions.

Whenever you change rooms, for example during on enter do event, you will want to save the player's location to a variable for persistent storage:
store "varRoomLocation"

Before leaving the game, this information needs to be actually put into persistent storage. This happens when you change rooms or call an ending. So you'll have to make sure one of those 2 things happen before the player quits, otherwise it doesn't actually save it.

Upon loading the game, you will need to call the restore function. You could do this in the game script's on load do. This should now load the saved "varRoomLocation" into your current "varRoomLocation" variable. At that point, you need to send the player to that room. Such as goto x,y in varRoomLocation

This is just one way of handling it, but the important part is the store/restore pair of functions.

3 Likes

Thanks for responding.

I just tried this and it worked! Is there a way to do this in more than one room?

Yeah. You could just set the "varRoomLocation" in every room within its on enter do script, but with a different room name each time. I don't think there's a built-in function for retrieving the current room's name, so you have to set it manually in PulpScript.

on enter do
     varRoomLocation = "NewRoom Name"
     store "varRoomLocation"
end
1 Like

You can use event.room to get the current room name. From the docs:

"In every event except load , its room member set to the name of the current room."

2 Likes

Oh, cool. Thanks orkn.

So then I guess I would do this once inside the player script perhaps, instead of in every room.

on update do
     varRoomLocation = event.room
     store "varRoomLocation"
end
2 Likes

I had a similar question and yall helped. A further thing Im wondering is, how to pick the x,y in the varRoomLocation.

Like how do I get the player to be in the exact same position they were last in, in addition to the last room. Or even guarantee a safe position without having to design every room with a safe tile?

I have a script for an RPG where I need to remember the room and location before going to the battle screen. I did it like this:

on collect do
randEncounter = random 1,30
if randEncounter>=29 then
returnX = event.px
returnY = event.py
returnRoom = event.room
battleNum = random 1,2
say "B-B-B-BATTLE!!!"
goto 18,7 in "battle"
end
end

event.px and py is the key!

1 Like

Does it work after someone quits the game? Can they return?

Also in addition, is there a way to guarantee that items don’t respawn after someone reloads the game? So they don’t pick up an item multiple times?

You would have to store that information with "store" and then recalling it with something like

on load do
restore
end

in the script for the Game. I haven't yet gotten to it with saving position and reloading, but it has worked with other variables so it shouldn't be any different.
As far as items go, you can put in specific checks for each one, which becomes a lot of code if there are a lot of items. Someone else will probably have a better way to solve this. But that's what I've been doing.