A Question on Emptying Tables on Lua

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

A Question on Emptying Tables on Lua

Post by sphyrth »

So, I usually clear my tables just by doing this.

Code: Select all

sample_table = {}
But I think I should be doing something like this.

Code: Select all

while #sample_table > 0 do
	table.remove(sample_table, 1)
end
Creating small games might not be an issue with the former but I assume that it produces some memory leaks or something, and that's why I should be doing the latter. Is my reasoning correct, or any of you have some better suggestions?
User avatar
Beelz
Party member
Posts: 234
Joined: Thu Sep 24, 2015 1:05 pm
Location: New York, USA
Contact:

Re: A Question on Emptying Tables on Lua

Post by Beelz »

I would just keep doing what you were... You're actually creating more work for LOVE. Either re-declare the table or set nil for garbage collection to handle.

Code: Select all

if self:hasBeer() then self:drink()
else self:getBeer() end
GitHub -- Website
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: A Question on Emptying Tables on Lua

Post by Nixola »

Only issue is, if the table has other references the first line won't delete them.
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: A Question on Emptying Tables on Lua

Post by sphyrth »

Thanks for the reply guys!

I guess I'll just go with the laziest bet and rely on Lua's Garbage Collector for small games, and do something else for the more ambitious projects.
Post Reply

Who is online

Users browsing this forum: No registered users and 149 guests