Page 1 of 1
How to get the size of a table/object in love2d
Posted: Tue Mar 09, 2021 10:14 pm
by Gunroar:Cannon()
I'm wondering if there's a way to get the size of a table in love2d specifically. I checked and I found an answer with a
module that's like a new lua version but with getSize or its in C or one other thing that doesn't make it seem usable in löve to me, unless maybe it's rebuilt which I'd like to avoid.
So is there any built in function in love2d or method to et the size of an object?
Re: How to get the size of a table/object in love2d
Posted: Wed Mar 10, 2021 12:48 am
by darkfrei
I know only
Code: Select all
local size=0
for i, v in pairs (tabl) do
size=size+1
end
print(size)
Re: How to get the size of a table/object in love2d
Posted: Wed Mar 10, 2021 8:41 am
by togFox
$tabl will return the number of elements - if that's what you mean.
Re: How to get the size of a table/object in love2d
Posted: Wed Mar 10, 2021 8:49 am
by ivan
#tabl only works for numerically-indexed tables without gaps.
For all other tables you have to use darkfrei's method to count the number of non-nil elements.
Check out my table.lua library for similar functions:
https://github.com/2dengine/table.lua
Re: How to get the size of a table/object in love2d
Posted: Wed Mar 10, 2021 10:21 am
by Gunroar:Cannon()
ivan wrote: ↑Wed Mar 10, 2021 8:49 am
#tabl only works for numerically-indexed tables without gaps.
For all other tables you have to use darkfrei's method to count the number of non-nil elements.
Check out my table.lua library for similar functions:
https://bitbucket.org/itraykov/table.lua/src/master/
No, sorry, I mean getting the size in kb/mb(I see the confusion, my fault
)
Re: How to get the size of a table/object in love2d
Posted: Wed Mar 10, 2021 10:40 am
by ivan
No, there is no way to know how much memory the table consumes, Lua is a scripted language with managed memory (NOT pre-allocated). You can't measure the size of a reference to another table. There could be cycles and so forth. It's not like you can garbage collect any table and expect a specific amount of memory to be freed up.
ffi has ffi.sizeof but that's strictly for cdata objects:
https://luajit.org/ext_ffi_api.html
Re: How to get the size of a table/object in love2d
Posted: Wed Mar 10, 2021 11:58 am
by darkfrei
Re: How to get the size of a table/object in love2d
Posted: Wed Mar 10, 2021 12:01 pm
by ivan
That looks like collectgarbage("count") which measures the total amount of Lua memory for the current thread. Does not include love2d images, sounds or other assets.