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.
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
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 Is there a way to keep the Lines with all 4 values in a single table?
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:
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?