Spawning / Creating a new object instance?

Evening folks,

Apologies for this one, it may be in the docs but couldn’t find it anywhere in the functions.

How do I spawn a new object / instance of a sprite?

Got as far as displaying one using the below code on player but “draw” is simply "display and not “spawn a new instance of this object” and doesn’t behave :

and while we’re at it, how do I reference “self”? I thought it’d be name.x,name.y (so player.x, player.y) or self.x,self.y but appears not? trying to go spawn bullet1 at player.x,player.y with a script on the bullet that then goes self.x+1,self.y for moving it along the screen.

on confirm do
	shoot = 1
end
	
on draw do
	// say "this works"
	
	if shoot==1 then
		draw "bullet1" at 12,12
		shoot = 0
	end
	
end

Cheers!

If you really want to spawn a new sprite you should use „swap“ instead, though this deletes the tile that it was swapped with. The draw function draws the new tile on top of the one already there, but it has to be redrawn every frame. With your current implementation the bullet should disappear after one frame, as you set shoot = 0 immediately after drawing.

For the „self“ part of the question: You can get the players coordinates with event.px and event.py. Storing those in variables, drawing the bullet there and increasing one variable every few frames should get the bullet moving.

Hope this helps! :slight_smile:

1 Like

Hello!

Re: instances, those don’t quite exist the way you might expect them to. You can make a sprite, which is just that one sprite; you could then duplicate it, or make other sprites that mimic the original’s behavior (look up mimic in the docs). That would be one way to have several of the “same kind” of thing.

There is no self.; the nearest equivalent would be the event members like event.px. See here: https://play.date/pulp/docs/pulpscript/#goto-event

I hope that helps!

2 Likes

Thanks Neven,

Yeah this is exactly what i’m struggling with, nothing works quite the way I’m expecting it to lol.

For context what i’m trying to make is a simple top down shooter, where you press a, that spawns a bullet at the player location then loops it’s movement across the screen and destroys when it reaches a certain position or overlaps another sprite… I’m not even sure this is possible without being able to create multiple instances of a sprite/object?

Goto is only for the player sprite, so it’s not possible to say: bullet1.goto x+1,y for example - not only because of no self reference but also not being able to do inline maths on a variable, making simple bullet behaviour very difficult to write :sweat_smile:

Is there a request sheet to request a feature to be implemented in a future update?

You can always request features through the Pulp Help link at the top!

I totally understand that Pulp is quite different from other programming languages—this is partly because of its performance limitations, partly to keep it a simple, “toy language” (Shaun’s wording) and partly because it’s still in development. For now, I try to think of it as an exercise in creativity :slight_smile: Often it’s a matter of approaching the problem from a different angle (something I find myself doing in game development on every platform.)

Here’s an Invaders-type game I worked on with Andrew Loebach (Drew-Lo on this forum). You’ll notice that almost all of it is handled in code rather than click-placement. Perhaps you could pick up some ideas from it!

IP Invaders.json.zip (4.2 KB)