Game keeps crashing when building - FIXED

I’m trying to make a game, but I got to some point where when I try to build (C-b or Ctrl+b), the game/engine crashes after about 5 seconds. I tried updating Defold but that did nothing to stop the crashes. What is happening, and is there any way to find out if the crash is happening because of some code I wrote, and if so can I found out where so I can fix it? Thanks.

FIXED - It turned out to be a weird bug in Lua itself, where the engine/language tried to iterate through an empty table, starting at index 1. This apparently caused the interpreter to time out, and I wrote a small script mimicking my code here to test it and got similar crash-like results. So, solution? I put a starting value in the table (outside the range I was trying to generate a number for).

Have you seen this?

There’s this:
image

Also, there might be something in [project folder] → build → [target name] → log.txt

1 Like

So, I’ve narrowed down the cause. In one file, there is a chunk of code that I think might just be overloading the computer’s processing power; I commented it out after narrowing it down to the file and the game worked fine (except for the occasional error from the code being commented out).

local y
local y_good = false
repeat
	y = rnd.range(375, 1600)
	for i = 1, #taken_positions do
		if not (y <= taken_positions[i] - 100 or y >= taken_positions[i] + 100) then
			break
		else
			y_good = true
		end
	end
until y_good

Does anybody have any recommendations for how to make this code not crash the engine? The goal is to generate a value that is not within 100 units of any of the values in the taken_positions table.

Looks like you break the inner loop as soon as you find a bad value, rather than break when you find a good one. That might make for an infinite (or very long) loop.

1 Like

There’s no issue with trying to iterate an empty table in Lua - it just won’t run any iterations. This code was resulting in an infinite loop when your table was empty because y_good is only set to true, breaking the outer loop, once taken_positions has something to check the new position against and passes the check.

It should work as you expect if you just invert the logic of y_good - assume a position is fine until you check it against previous positions and find it’s too close.

local y
local y_good
repeat
	y = rnd.range(375, 1600)
	y_good = true
	for i = 1, #taken_positions do
		if not (y <= taken_positions[i] - 100 or y >= taken_positions[i] + 100) then
			y_good = false
			break
		end
	end
until y_good
1 Like