Can't show lines and sprite together?

I am sure this is a super simple thing I am missing about how the SDK works (I also have zero previous exp developing games, so might miss a known game dev thing...), but I can't seem to draw lines, while also updating a moving sprite.
I already realized that any direct drawing (drawText(), fillRect(), drawLine() etc.) must happen after sprite.update() in playdate.update(), otherwise they are not shown.
I have a list of lines, trying to draw all of them, and for some reason though, even when calling draw after sprite.update(), they still don't show. If I remove the sprite.update() call completely, they are drawn (see gifs, with and without the sprite).
Note that the lines suppose to track to path that the orly poly was doing - when the bug is shown, we do see the very last line (looks like a point on top of the bug...), but not any previous lines.

Code is here: https://github.com/avielg/playdate-poly/tree/main

Note sure what I'm doing wrong? Would love any advice, thank you!

line
bug

This also got me when I was starting out.

What you have to do is draw to the sprite background, which is taken into consideration during sprite updates.

playdate.graphics.sprite.setBackgroundDrawingCallback(drawCallback)
https://sdk.play.date/1.9.0/Inside%20Playdate.html#f-graphics.sprite.setBackgroundDrawingCallback

Also see the example game at: https://sdk.play.date/1.9.0/Inside%20Playdate.html#_a_basic_playdate_game_in_lua

ps: I do exactly this with the tyre skid marks in my game Daily Driver

Thanks for the tips! So this brought me a step closer, and then after lots of debugging I realized I was also using Lua tables wrong, which I am still not sure what was wrong :thinking:
I declared local lines = {} at the file scope, then added new lines like this:

local line = Line:new(fx, fy, tx, ty) -- fromx/y and tox/y
table.insert(lines, line)

And later tried to draw them (inside setBackgroundDrawingCallback) like this:

for _, line in ipairs(lines) do -- I tried pairs() too
    line:draw()
end

And it didn't work. Only when I saved them as primitives in 4 tables it worked:

local linesfx = {}
local linesfy = {}
local linestx = {}
local linesty = {}
local linesIndex = 1

-- in drawCallback:
for key, _ in pairs(linesfx) do
	local fx = linesfx[key]
	local fy = linesfy[key]
	local tx = linestx[key]
	local ty = linesty[key]
	local line = Line:new(fx, fy, tx, ty)
	line:draw()
end

-- when adding a line:
linesfx[linesIndex] = fx
linesfy[linesIndex] = fy
linestx[linesIndex] = tx
linesty[linesIndex] = ty
linesIndex += 1

This is pretty ugly stuff :sweat_smile: Is there a way to keep the Lines with all 4 values in a single table?

I assume Line is a custom object of yours?

In theory the method you use with the table lines looks fine and should work. I suspect that maybe the lines table is being access reseted somewhere else in your code.

How does it fails exactly? Nothing is drawn?
Check if the new lines are properly saved in the table by printing the number of element in the table.

local line = Line:new(fx, fy, tx, ty) -- fromx/y and tox/y
table.insert(lines, line)
print("New line. Total:", #lines)
print("Lines to draw:", #lines)
for _, line in ipairs(lines) do -- I tried pairs() too
    line:draw()
end

Ok so with your assurance that I add and iterate the the lines correctly, I printed the values of each line, and saw that they all reference the same object:

New line. Total:	6
Lines to draw:	6
200.0	40.99999	200.0	40.99999
200.0	40.99999	200.0	40.99999
200.0	40.99999	200.0	40.99999
200.0	40.99999	200.0	40.99999
200.0	40.99999	200.0	40.99999
200.0	40.99999	200.0	40.99999

Eventually I found the problem → I forgot to add local self = {} at the beginning of Line:new() :sweat_smile:
This is me still learning the very basics of Lua....

Anyway thank you so much matt and Nic for your guidance and help! The bug is now officially digging :tada:

image

2 Likes

Nice! Cool concept, too.

Do you plan to do more with the lines after the hole is dug?

I ask because you can draw straight to the sprite backing and forget about it, for my skid marks i draw them and do not remember their position. Obviously, this means I cannot do anything with them afterwards, but at the same time everything remains quick because I do not have an array of things to draw.

So, if you don't plan to do anything with the lines other that drawing, you can simply draw a white circle at the bug position onto the sprite backing and as it moves along it will draw its path.

Thanks!
Yes eventually there will be a scorpion chasing the bug - following the path the bug digs - so I need to keep the path around for that (I think?)

At first I was calculating the given rect in the background drawing callback to decide which line needs to be drawn, but I then saw that even if I am just always drawing all of the lines somehow the OS is smart enough to not re-draw things (I used the "highlight screen updates") - so maybe it's fine?

1 Like

Sprite system wins! Take them and do a little dance.

1 Like