ProcGen in Defold?(SOLVED)

Also, some unsolicited code advice for you:

Watch out for duplicate code, especially duplicate iterators (for loops and such). Generally, if you have to type or copy-paste the same statement more than once or twice, there is a better, easier way to do it. In your map.lua, in the create_floors function, you have no less than six almost identical for loops. They could all be combined into one loop. If you iterate backwards through the list, then adding and removing single items from the end of the list wonā€™t cause problems. Like so:

for i = #walkers, 0, -1 do -- counting down instead of up
   -- update walker
   -- possibly delete this walker
   -- possibly add a new 'child' walker
end

Thereā€™s another example in the setup function. You have this:

Click to show code (Original)
tiles = {}
for x = 0, ROOM_WIDTH do -- for each column of tiles...
	for y = 0, ROOM_HEIGHT do -- for each tile in that column...
		tiles[x] = {} -- make a new table for this column.
	end
end

for x = 0, ROOM_WIDTH do -- for each column of tiles... (again)
	for y = 0, ROOM_HEIGHT do -- for each tile in that column... (again)
		tiles[x][y] = EMPTY -- set the tile at this position. 
	end
end

First, you have two identical pairs of for loops, and thereā€™s no reason for that. Also, in the first pair of loops, youā€™re re-creating an empty table for that column for each tile in that column. Thatā€™s hundreds of tables that you donā€™t need. You could easily cut that down to this: (saving performance and lines of code)

Click to show code (Modified)
tiles = {}
for x = 0, ROOM_WIDTH do -- for each column of tiles...
	tiles[x] = {} -- make a new table for this column.
	for y = 0, ROOM_HEIGHT do -- for each tile in that column...
		tiles[x][y] = EMPTY -- set the tile at this position.
	end
end

Obviously, neither of these actually causes any problems in this case, but if you keep an eye out for it, you can save yourself a lot of typing, make your code more readable, and save performance. If you did this kind of thing on update, it could really kill the performance of your game.

4 Likes

You were right. Maybe it was that incomplete set_walls function causing it. I moved it to a separate module and everything is fine.

You are right again. In the beginning, I had this in script and the performance was very low. Then I moved it to Map.lua to increase some performance, but I used to think that moving to one loop only will cause problems. Thanks for showing me the path.
And @britzl The big question is- how to use the debugger( I think I have missed it S manual)

The manual is here: https://www.defold.com/manuals/debugging/#_running_the_debugger

1 Like