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:
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!