How to get the size of a table/object in love2d

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

How to get the size of a table/object in love2d

Post 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?
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
darkfrei
Party member
Posts: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to get the size of a table/object in love2d

Post by darkfrei »

I know only

Code: Select all

local size=0
for i, v in pairs (tabl) do
  size=size+1
end
print(size)
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
togFox
Party member
Posts: 774
Joined: Sat Jan 30, 2021 9:46 am
Location: Brisbane, Oztralia

Re: How to get the size of a table/object in love2d

Post by togFox »

$tabl will return the number of elements - if that's what you mean.
Current project:
https://togfox.itch.io/backyard-gridiron-manager
American football manager/sim game - build and manage a roster and win season after season
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How to get the size of a table/object in love2d

Post 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
Last edited by ivan on Sat Dec 11, 2021 8:17 am, edited 1 time in total.
User avatar
Gunroar:Cannon()
Party member
Posts: 1088
Joined: Thu Dec 10, 2020 1:57 am

Re: How to get the size of a table/object in love2d

Post 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 ^^ )
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How to get the size of a table/object in love2d

Post 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
User avatar
darkfrei
Party member
Posts: 1172
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to get the size of a table/object in love2d

Post by darkfrei »

See:
https://github.com/kikito/bump.lua#demos

Somehow the memory size:
Attachments
2021-03-10T12_58_23-GitHub - kikito_bump.lua_ A collision detection library for Lua.png
2021-03-10T12_58_23-GitHub - kikito_bump.lua_ A collision detection library for Lua.png (5.9 KiB) Viewed 4390 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: How to get the size of a table/object in love2d

Post 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.
Post Reply

Who is online

Users browsing this forum: Google [Bot], Jimanzium and 31 guests