Page 1 of 1
game: crashes; I: need answers
Posted: Sun Feb 28, 2021 3:36 pm
by Raskol'nikov
Every time I use while and repeat cycles my game crashes*. It doesn't matter how complicated the code is, it can crash even with this
Code: Select all
function love.load()
x = 0
end
function love.update()
while x do
x = x + 1
end
end
I already know that the reason isn't in LOVE itself it's all my machine, I only want to know possible reasons of that.
*the window freezes and stops responding
Re: game: crashes; I: need answers
Posted: Sun Feb 28, 2021 4:00 pm
by darkfrei
The
x will be always
true, no exit from while-loop.
https://en.wikipedia.org/wiki/While_loop
Re: game: crashes; I: need answers
Posted: Sun Feb 28, 2021 4:21 pm
by MrFariator
To add to darkfrei's answer, there's two main "falsy" values in lua: false, and nil (the absence of a value).
So long the condition (in your case, simply the variable x) evaluates to anything that is non-false or non-nil, the while loop will continuously run forever (because the variable x having a number value means it will never be false or nil). This usually leads to applications hanging up or crashing, because the loop never yields the control and let other parts of the application to run.
Of course, there's also the "break" keyword that you might see in the wikipedia page darkfrei linked. Break is reserved for breaking out of the inner-most loop when a certain condition is met. Below is some demonstration code:
Code: Select all
local myCondition = true
while myCondition do
while true do
while x do
x = x + 1
if x == 10 then
break -- break out of the 'while x do' loop
end
end
break -- break out of the 'while true do' loop
end
myCondition = false -- break out of the "while myCondition do" loop eventually, because the condition will return false
end
The keyword "return" works as well, but it will return from the scope's context (whether it's the root of a file or function), and break all the loops in that scope.
Re: game: crashes; I: need answers
Posted: Sun Feb 28, 2021 6:11 pm
by Raskol'nikov
It was just a bad example, the game will crash with
Code: Select all
x = 0
while x < 10 do
x = x + 1
end
or
Code: Select all
x = 0
repeat x = x + 1 until x == 10
Re: game: crashes; I: need answers
Posted: Sun Feb 28, 2021 6:20 pm
by darkfrei
Raskol'nikov wrote: ↑Sun Feb 28, 2021 6:11 pmIt was just a bad example
Please attach the .love file with your error.
Re: game: crashes; I: need answers
Posted: Sun Feb 28, 2021 6:25 pm
by Raskol'nikov
the rest of the code doesn't matter, if there's even one while or repeat loop in the entire code game will crash. if the file is really needed i will provide it later
Re: game: crashes; I: need answers
Posted: Sun Feb 28, 2021 6:53 pm
by Raskol'nikov
here
Re: game: crashes; I: need answers
Posted: Sun Feb 28, 2021 7:19 pm
by MrFariator
In main.lua, you have the following code:
Code: Select all
function ccol(a)
if a.y + a.height == 600 then
return true
else
return false
end
end
function love.update(dt)
while not ccol(q,c) do
yspeed = yspeed + a * dt
a = a^10 * dt
end
end
This code assumes that the object a received by ccol() has fields a.y and a.height that will equal 600 eventually. However, there's nothing that will keep updating the values contained by q (the object being passed to ccol()) once the loop starts. You're simpy adding to the values of yspeed and a, and nothing else gets done - there is no way for the loop to exit, causing an infinite loop.
Similarly, the while loop in gravity() function in reservation.lua depends on a global 'col' variable. If this variable is any falsey value by the time the while loop is executed, it will loop infinitely - because nothing is going to change the 'col' variable once the loop starts.
Edit: interpreted the code wrong at first due to its formatting, fixed the post.