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.