game: crashes; I: need answers

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

game: crashes; I: need answers

Post 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
User avatar
darkfrei
Party member
Posts: 1178
Joined: Sat Feb 08, 2020 11:09 pm

Re: game: crashes; I: need answers

Post by darkfrei »

Code: Select all

while x do
	x = x + 1
end
The x will be always true, no exit from while-loop.

https://en.wikipedia.org/wiki/While_loop
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: game: crashes; I: need answers

Post 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.
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

Re: game: crashes; I: need answers

Post 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
User avatar
darkfrei
Party member
Posts: 1178
Joined: Sat Feb 08, 2020 11:09 pm

Re: game: crashes; I: need answers

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

Re: game: crashes; I: need answers

Post 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
Raskol'nikov
Prole
Posts: 9
Joined: Sun Feb 28, 2021 1:48 pm

Re: game: crashes; I: need answers

Post by Raskol'nikov »

here
Attachments
gravity test.love
(1.78 KiB) Downloaded 112 times
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: game: crashes; I: need answers

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 88 guests