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?
How to get the size of a table/object in love2d
- Gunroar:Cannon()
- Party member
- Posts: 1144
- Joined: Thu Dec 10, 2020 1:57 am
Re: How to get the size of a table/object in love2d
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
$tabl will return the number of elements - if that's what you mean.
Last project:
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
https://togfox.itch.io/hwarang
A card game that brings sword fighting to life.
Current project:
Idle gridiron. Set team orders then idle and watch: https://togfox.itch.io/pad-and-pencil-gridiron
Re: How to get the size of a table/object in love2d
#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
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
Last edited by ivan on Sat Dec 11, 2021 8:17 am, edited 1 time in total.
- Gunroar:Cannon()
- Party member
- Posts: 1144
- Joined: Thu Dec 10, 2020 1:57 am
Re: How to get the size of a table/object in love2d
No, sorry, I mean getting the size in kb/mb(I see the confusion, my fault )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/
Re: How to get the size of a table/object in love2d
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
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
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.
Who is online
Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests