When I try to use loops with a button related condition inside, the whole tic-80 freezes. I tried on the 0.80 on windows.
A simple example:
function TIC()
testrepeat()
end
function testrepeat()
t = 0
flag = true
while (flag) do
cls()
t = t + 1
if btn(4) then
flag = false
end
print("t"..t, 60, 60)
end
end
It works correctly if the btn is changed for a different condition.
It's because the gamepad/mouse/keyboard states _aren't updated in real time_. They only update after each call to TIC().
So when it enters testrepeat(), you're setting the global flag to true. Then you're running a while-loop that runs while flag is evaluated as true in Lua. Since btn(4) is always returning false, flag never gets set to false to exit the loop.
TIC-80 has nothing equivalent to PICO-8's flip() to help you out. Best option is to make testrepeat() a coroutine, then do a yield from it after the print() call. I haven't tested this myself, but maybe this will work.
Thanks, it was not obvious why gamepad/mouse/keyboard would cause such behavior, so it makes sense for the TIC 80 to actually enter an infinite loop and hang.
I imagine a note about this in the wiki would be helpful for future reference.
Most helpful comment
It's because the gamepad/mouse/keyboard states _aren't updated in real time_. They only update after each call to
TIC().So when it enters
testrepeat(), you're setting the globalflagto true. Then you're running a while-loop that runs whileflagis evaluated as true in Lua. Sincebtn(4)is always returning false,flagnever gets set to false to exit the loop.TIC-80 has nothing equivalent to PICO-8's
flip()to help you out. Best option is to maketestrepeat()a coroutine, then do a yield from it after theprint()call. I haven't tested this myself, but maybe this will work.