I am currently working on a very basic 2d side-scroller game. My map is nothing more than a 2-d array, and most of that is just air.
I was wondering if I would it would somehow be more 'memory efficient' to change all the 0s in the table to nil (leave them blank).
I could still check if it was an 'air' block with something like:
That sounds like it should be fine to do that with nil.
As far as memory efficiency goes, I'm not actually sure. But it sounds logical that it would be more efficient as the table will be smaller, so you have to loop through fewer items when drawing and stuff. I don't think the difference is gonna be too major though. (unless you're dealing with huge maps)
As far as the difference between not and ~=, I'm not sure, in fact I'm interested in knowing that too.
At first glance it sounds silly, but since lua tables are not contiguous arrays (but rather hash tables), the amount of memory taken up is determined by how many non-nil entries are in the table.
One thing I do remember is that, under normal circumstances, lua will not shrink the memory that's already allocated to a table, so you need to make sure that those empty spaces never get allocated in the first place (as opposed to creating all of them, then running back through and setting them all to nil).
I forget if lua offers a function for this from script alone, but I know that in C, there's a function call that lets you get how much memory (in bytes) is being used by the virtual machine. Try to see if you can get that number, divide by 1,000,000 to convert to megabytes, and then compare how much memory is used for filling with nil vs false. The difference should be quite large if you have enough entries.
veethree wrote:As far as the difference between not and ~=, I'm not sure, in fact I'm interested in knowing that too.
The 'not' and '~=' operators have different semantics. The first is a logical operator, which is true if the expression it preceeds is equal to false or nil. The second, on the other hand, compares two things by value if they are numbers or strings, and compares everything else by reference (e.g. tables). It is also possible to redifine the meaning of '~=' for a table by writing a custom __eq function for the metatable of that table. This works because Lua treats 'x ~= y' as if it were written 'not (x == y)', which would invoke that custom __eq function.
To answer the original question, you probably shouldn't really need to check if something is 'air' or not. What is the particular reason you need to do so?
collect garbage will perform a full garbage collection run. I don't think it returns info. I did however just find the gcinfo() function. According to some scarce documentation, it returns two arguments: the number of bytes used and the capacity of the collector. That should do it!
EDIT: ah! Yes, collectgarbage can do that with the 'count' argument. Sorry for the confusion lol
Last edited by Beta Carotene on Fri Jan 18, 2013 3:40 am, edited 1 time in total.
To answer the original question, you probably shouldn't really need to check if something is 'air' or not. What is the particular reason you need to do so?
If I want to check if I can jump, I need to make sure the block above me is air. Stuff like that, because air has no properties of it's own, it could still be nil (i think)