Some basic Qs on sprite instancing

Hello, complete newbie to Lua and Playdate dev (using the sdk on Windows). I have a bit of experience with Gamemaker Studio, which is an object-based engine. In GM, every “object” in the game is actually an instance with its own ID which can be called to reference specific entities. I want to do something similar with my sprites in Lua/Playdate (assuming "sprites" are about as close as this language gets to "objects").

I have made a 2d array/table for a racetrack, each row acting as a laneway while the columns will hold the sprite of the terrain in that tile. See the below example with drawRect rectangles in each position to give a general idea of the layout:
image

When I made the table, I assigned my sprite to each array, like so:

local lanes = {};
local lane_length = 20;
for i = 1, #slots, 1 do
    lanes[i] = {};
    for j = 1, lane_length, 1 do
    lanes[i][j] = spr_grass;
    end
end

and here is the grass sprite creation code:

local grass_image = gfx.image.new("images/grass");
local spr_grass = gfx.sprite.new(grass_image);
spr_grass:add();

When I check random indexes in the lanes table, they are all showing the same reference. My sprite count is also not as high as I would expect. I assume it is because I am not assigning 20 instanced versions of the sprite to the table, but rather the exact same sprite over and over again to each position. How can I re-use the initialized sprite to create multiple versions of the same sprite which can be uniquely referenced like instances?

I have seen some people end up fully recreating OOP within Lua with metatables and such, but ideally I'd like to stick to something simpler and avoid building an entire secondary system inside my code just to manage multiple copies of the same sprite. I imagine someone has solved this problem in a much easier way!

You are indeed assigning the same sprite reference to all elements of your bidimensional array, the assignment itself won't create a copy. If I understand correctly what you ask, you have a few options to reuse the same sprite object/table to create multiple unique versions:

  • make a copy of an existing sprite via sprite:copy()
  • instantiate multiple sprites with gfx.sprite.new(grass_image) (assuming they're intended to share the same image), then copy any properties you're interested in from the to old to the new sprites
  • assign the image to a pre-created sprite via sprite:setImage(grass_image)

In every case each sprite can then track its own position, visibility, Z index etc. individually as it encapsulates those properties.

If you are interested in an OOP approach, the Playdate SDK does include a basic object-oriented class system, so another option is to define your own class that includes a sprite object via composition, or directly inherits from playdate.graphics.sprite.

Hope this helps somewhat!

1 Like

I ended up using the sprite:copy method and it worked beautifully. It was very easy to swap out just a few lines and run through the loops I had already built. Thank you for all the additional tips as well! Much for me to consider :slight_smile:
image

1 Like