Altering sprites on button press?

I've been trying to set a sprite to toggle with a press of the A button, to no avail. I've tried using the Swap function to replace the sprite with a different object as well as setting the animation to either frame 0 or 1. I've also tried having the confirm event within the sprite itself as well as having a variable set by an event in the player, which is then checked by the sprite.

Here's my current code for the Player script:
on confirm do
if doorTog==0 then
doorTog = 1
log "1"
else
doorTog = 0
log "0"
end
end
The "0" and "1" messages log on button press, so I assume that the variable is successfully updated.

The following is the current script for the sprite that I want to change:
on any do
if doorTog==0 then
frame 0
log "AAAAAAAAAA"

else then
	frame 1
	log "AAAAAAAAAAAA"
end

end
Here's where things get interesting. Right when the game opens, the log message for doorTog == 0 plays once, followed by the 'else' log playing about 6 times. After that point, however, neither play, and the sprite doesn't toggle.
I get a similar result with both "any" and "loop" events.

I actually don’t know why your code isn’t working, sorry… :sweat_smile: But after some experimenting I came to this solution that should do the trick.

on confirm do
 doorX = 14 //Location of the door in this room
 doorY = 6

 tell doorX, doorY to
  if doorTog==0 then
   frame 0
   doorTog = 1
   log „Door is open“
  else
   frame 1
   doorTog = 0
   log „Door is closed“
  end
 end
end
1 Like

I see a few issues here.

The first is something that should be a syntax error: else then should just be else.
Second, Kucromy has the right idea, you need to use tell to target the sprite you’re “talking” to (otherwise it will perform the actions on the sprite you’ve defined the event handler on, in this case it would have to be the player because it is the only sprite that receives the confirm event).

oh, I see! I had thought you could use frame or swap on a sprite without coordinates and it would act on itself, whoops

1 Like

You can but only the player receives the confirm event so even if you add a handler for it to a sprite it will never be called.

1 Like

I bought this up yesterday, lack of direct or self references are such a pain when you're used to programming like that

be nice to just go "player.call "EventName" or "self.hide" or "self.collisions(false) or "if door1.frame==0 then "do something on the player"

this might be something that's doable in the full SDK though.

Yes, the full SDK has support for OOP programming. PulpScript will likely never support proper objects. Instances of tiles in a room are literally just integers in an array. They don’t even have a backing object in the runtime :playdate:

1 Like

Offtopic for this thread I know but does the full SDK have a gui / room / world editor similar to Pulps? Is it more like a full engine studio like GBStudio/ Construct / Unreal?

There's no GUI component to the SDK—it's mostly a compiler and documentation. You can set up the compiler in our code editor, Nova, or use any other text editor.

There are a few other tools: a Playdate Simulator (native app for Mac, Windows, and Linux) and a font editor called Caps (web-based, like Pulp).

There's a pretty healthy ecosystem of game development tools out there, including level editors like LDtk and Tiled. It's pretty simple to load their levels in Playdate games!

1 Like

Ah okay, thanks for clarifying @neven !

I was expecting it to be more like a standard game engine editor (Unity or Construct or similar) or like pulp but with source code access and ability to add new classes rather than just the compiler.

Sounds like it's worth sticking with Pulp then for now as I was a gameplay programmer in a previous life rather than a proper programmer so non-gui sdks just aren't for me :rofl:

1 Like

Can a sprite be changed with a button press specific to one room, or will it occur in every room?

Thank you in advance for any help.