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: GitHub - avielg/playdate-poly · GitHub

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

line
bug

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

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?