My pathfinding code doesn't work! #2518
Replies: 2 comments
-
First of all, could you please post your code inside a code block? Pasting it directly into the comment breaks, the formatting, especially in the line I'm not sure entirely what you're going for with this code, so I won't try to completely fix it, but I can at least point out why it crashes/freezes. In I also don't think the nested loop has the behaviour you want. In the current version, when a point is spread, the inner loop adds a new point for every other point that already exists. That means if you have 5 points currently in the The commented version of the loop is also wrong. I'm guessing you're trying to only add a point if it's not already in the |
Beta Was this translation helpful? Give feedback.
-
I think you can get better help on the official Tic-80 discord |
Beta Was this translation helpful? Give feedback.
-
I'm trying to create a pathfinding algorithm by myself, but every time I run the program and press space to create a 'path' the TIC-80 itself crashes and I have to open it again and I don't know what's wrong.
Here is the code:
t=0
ti, tr = table.insert, table.remove
pl={x=16, y=64}
path={}
--create path
function path:new(x, y, parent)
ti(self, {
x = x, y = y,
t = 30,
id = #path+1,
spread = true,
parent = parent
})
end
function path:updt()
for i, p in ipairs(self)do
if p.t>0 then p.t=p.t-1 end
if p.spread and keyp(48) then --aaaaaaaaaaaaaaaaaaaa
for k, pa in ipairs(path)do
end
p.spread = false
end
end
end
function path:draw()
for i, p in ipairs(self)do
if i>1 then line(p.x+4, p.y+4,
path[p.parent].x+4, path[p.parent].y+4, 15) end
rectb(p.x, p.y, 7, 7, 15)
print(p.parent, p.x+2, p.y+1, 14, false, 1, true)
end
end
path:new(pl.x//88, pl.y//88, 0)
function TIC()
t=t+1
cls(0)
map()
--player control
if btn(0) then pl.y = pl.y - 1 end
if btn(1) then pl.y = pl.y + 1 end
if btn(2) then pl.x = pl.x - 1 end
if btn(3) then pl.x = pl.x + 1 end
--path functions
path:updt()
path:draw()
--player img
spr(16, pl.x, pl.y, 0)
end
[end of the code]
Please someone who can solve this help me
Beta Was this translation helpful? Give feedback.
All reactions