Store Room Location in Playdate PulpScript

Hi! I'm pretty new to pulp, although I am a fairly versatile programmer. In my pulp game, I want to save the user's location and room area. However, when I try to create variables to save the user's position and room, it shows the variables as undefined, even though I defined them. Here's my game code:


on load do
	restore "varRoomLocation"
	restore "varPlayerX"
	restore "varPlayerY"
	if varRoomLocation==0 then
		goto 0,9 in "card"
	else
		if varRoomLocation=="level-1" then
			goto varPlayerX, varPlayerY in "level-1"
		elseif varRoomLocation=="level-2" then
			goto varPlayerX, varPlayerY in "level-2"
		elseif varRoomLocation=="level-3" then
			goto varPlayerX, varPlayerY in "level-3"
		elseif varRoomLocation=="level-4" then
			goto varPlayerX, varPlayerY in "level-4"
		elseif varRoomLocation=="level-5" then
			goto varPlayerX, varPlayerY in "level-5"
		elseif varRoomLocation=="level-6" then
			goto varPlayerX, varPlayerY in "level-6"
		elseif varRoomLocation=="level-7" then
			goto varPlayerX, varPlayerY in "level-7"
		elseif varRoomLocation=="level-8" then
			goto varPlayerX, varPlayerY in "level-8"
		elseif varRoomLocation=="level-9" then
			goto varPlayerX, varPlayerY in "level-9"
		elseif varRoomLocation=="level-10" then
			goto varPlayerX, varPlayerY in "level-10"
		elseif varRoomLocation=="level-11" then
			goto varPlayerX, varPlayerY in "level-11"
		elseif varRoomLocation=="level-12" then
		end
	end
	store "varRoomLocation"
	store "varPlayerX"
	store "varPlayerY"
end

and my player code:


on update do
	if event.dx>0 then
		swap "MouseMan1"
		
	elseif event.dx<0 then
		swap "MouseMan0"
	end
end

on any do
	varRoomLocation = event.room
	varPlayerX = self.x
	varPlayerY = self.y
	store "varRoomLocation"
	store "varPlayerX"
	store "varPlayerY"
end

on exit do
	varRoomLocation = event.room
	varPlayerX = self.x
	varPlayerY = self.y
	store "varRoomLocation"
	store "varPlayerX"
	store "varPlayerY"
end

on enter do
	varRoomLocation = event.room
	varPlayerX = self.x
	varPlayerY = self.y
	store "varRoomLocation"
	store "varPlayerX"
	store "varPlayerY"
end

I think this might have been solved in Help With Player Position Save, but I tried my code on the online sim, the sdk sim, and the actual hardware, so, sorry if I'm being redundant. Any help would be incredibly appreciated :playdate_finder:

Here's my 10 cents:

Firstly, for the sake of stability, I would avoid doing things like "goto" in the load event. I'd wait until after it, and just start the player in that card room by default if thats what you want the default room to be.
Then once in that room, check for where they last were, and send them to it.
It's not 100%, but a lot of issues have happened for me in the past because stuff was happening inside of load, rather than after it.

Secondly, is there a reason you sometimes hardcode the room name, and other times use the varRoomLocation variable?

Third, I noticed you're storing the variables in the load event. Any reason why? It seems like you either just used them, so they should remain the same, or theyre empty and you're just saving 0's. Just was confused about that, tbh.

Fourth, is that "self." thing real? Like I've seen that in Java and stuff, but I had no idea that Pulp could do that. I usually just do event.px and event.py for the player's last marked x and y coordinates. Also, I make my own copies of those separate from the ones that Pulp provides.

Fifthly, in the player code, I noticed that in the Exit event you have the location get stored. Seems like you're storing the room they just left. Is that on purpose? Regardless, I also would just caution against doing too much in exit and enter (or any other simultaneous) events at the same time, as that also can cause crashes and errors. Like your Enter and Any events will also be updated those same resources at the same time. It might be redundant event, or contradictory. The timing of stuff in Pulp isnt always 100% clear, but sometimes stuff doesn't all resolve in the order you want, or event within the same frame, and if you have 2 things trying to change the same variable, they might do it in an unwanted order. I dont know if this is doing that, but just thought Id spread the word.

So it seems like you're doing a lot of the same actions over and over in several event types at the same time. That could be causing issues and conflicts, or putting bad data into those variables. Like you might want to stick to just the enter event for the room variable (since thats the only time the room actually changes), and then keep the X and Y coordinate variables updated on the enter and update events, since thats all the times those will be updated.
Id probably remove the stores from the load event, and maybe, just maybe delay doing the "goto" until after the game has loaded everything.
Maybe dont use the Any event, unless you need something to happen before other events begin consistently, because thats happening every time anything happens to the player to trigger an event, but first. Like move that stuff to the update event.

But maybe your issue is just that you're doing that self. thing, since I cant seem to find the word "self" anywhere in the Pulp or PulpScript documentation pages.

Im not great at this, but those are my best guesses. Good luck.

Thanks for your response! (and sorry I wasn't able to reply earlier) I've updated and simplified my code and I've tried to implement your suggestions (I hope I understood you correctly, I'm new to pulp, relatively speaking) I don't know if I'm just saving things incorrectly, but my updated code still doesn't work :playdate_sad: :

My game code:


on start do
	restore
	if roomLoc == 0 then
		goto 0,9 in "card"
		say "{roomLoc}, {PlayerX}, {PlayerY}"
	else
		goto PlayerX,PlayerY in roomLoc
	end
end

on loop do
config.follow = 1
config.followOverflowTile = "wall"
end

...and my player code:


on update do
	if event.dx>0 then
		swap "MouseMan1"
		call event.room
	elseif event.dx<0 then
		swap "MouseMan0"
	end
end

on loop do
roomLoc = event.room
PlayerX = event.px
PlayerY = event.py
store "PlayerY"
store "PlayerX"
store "roomLoc"
end


Thanks!

I was told this in an earlier post, doing:

store “variable”

Queues it to be stored, after that, you need to call store:

store “variable”
store

This should work.

The store command does not immediately store anything. Take a look here: Playdate PulpScript "Persistent storage is written to disk between the exit and enter events when changing rooms or when the Player reaches an ending." So if you want saving to happen, you have to store and then have them change rooms.

I usually do something like this:

store "my_variable"
event_room = event.room
event_px = event.px
event_py = event.py
goto 0,0 in "SaveRoom"

and then have a room named "SaveRoom" that has this code:

on enter do
  goto event_px,event_py in event_room
end

since you are already saving the room and location, that's even easier for you
but this can cause some complications, mostly that each time you save, it runs the enter event on the room you are in already. to avoid that, put a little something to stop that
code that would store something:

store "my_variable"
event_room = event.room
event_px = event.px
event_py = event.py
dont_redo_enter = 1
goto 0,0 in "SaveRoom"

and then in that rooms enter put something like:

on enter do
  if dont_redo_enter==1 then
    dont_redo_enter = 0
    done
  end
  ...

at the start of the enter. if you do this, you'll have to make sure there is something that resets "dont_redo_enter" in every room that stores things

Great! Thanks! I'll try that today! :playdate_happy:

Thanks! I tried it but it still doesn't save. It's probably just my user error, so I'll double check my stuff.

I figured it out!

Hi! After hours of trial and error, poring over the PulpScript docs, and testing, I've finally figured out the solution. I used the restore command to copy (or fetch) the variables from persistent storage, then you can change the variables however much you'd like, but the thing that makes the magic of writing the variables to persistent storage is the store command. For context, some functional example code would look like this:
on update do
restore "playerX"
restore "playerY"
restore "roomLoc"
playerX = event.px
playerY = event.py
roomLoc = event.room
store "playerX"
store "playerY"
store "roomLoc"
(I'm not sure if storing and restoring each variable is redundant, it probably is, but it works for me)
Then, to return to the saved location, I used the goto command, coupled with an if conditional to see if roomLoc was 0 (or null), which would occur if the user has never played the game.
on load do
restore
  if roomLoc != 0 then
    goto playerX, playerY in roomLoc
  else
    goto 0,9 in "card"
  end
end
This code autosaves the game for me thanks to the update event, so I can go back to the launcher, play a different game, then come back, and I'm taken back to my saved position. You could also welcome the user back and let them know that they are being returned to their saved position, like Welcome back! Loading your saved progress...
then, put it in a say command like
on load do
  if roomLoc != 0 then
    say "Welcome back! Loading your saved progress..."
    goto playerX, playerY in roomLoc
  else
    goto 0,9 in "card"
  end
end
There's the simpleish solution to the seemingly complex issue. I hope this is helpful! Thanks to all the Dev Forum users who helped me along the way!