Difference between local values and self.values in object

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Difference between local values and self.values in object

Post by hasen »

I was just wondering what the difference was between local values set in an object and values set to self in the initialise function in an object? I'm talking about values that aren't passed when creating the object.

Code: Select all

local timer = 12
local radius = 20

function Player:initialize(world, x, y, lives)
  Entity.initialize(self, world, x, y, width, height)
  self.lives = lives
  self.timer = 12
  self.radius  = 20
end
For example, should timer and radius here be created as local variables or self variables?
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Difference between local values and self.values in object

Post by MrFariator »

The local value that is defined outside the initialize function would effectively be available to every instance of the player class, or in other words this is a matter of do you want to make it a static variable or member variable. Anything that is defined as self.variable can only be accessed if you have an access to the object (or table). Another difference is that when you destroy the object (eq. self = nil), anything that is defined through self will also be deleted. As such, unless you want to have the variables stick around (and retain whatever value they had), it's better to just define object's variables with self.

In your case, if there are multiple players then each player tracking its life count would be reasonable, so just keep that within self. Same goes for the timer, as I imagine each player object would have its own time tracker (eq. why should one player's internal timer affect other players, particularly if it's something like gun firing rate?). For the radius, though, if that's never going to change then it could be just a local variable, but even then I'd personally define it with self.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Difference between local values and self.values in object

Post by hasen »

Ok I see, thanks, that's very helpful. When the object is destroyed wouldn't any local variables be gone too? Since they are local to the object after all.
MrFariator
Party member
Posts: 512
Joined: Wed Oct 05, 2016 11:53 am

Re: Difference between local values and self.values in object

Post by MrFariator »

The locals will only be destroyed if they're defined inside Player:initialize or another member function of that class, otherwise they will persist. This is because deleting your object doesn't tell lua "unload this whole code file", but rather just set that specific object (a lua table) to a nil value so that garbage collector can pick it up later. Of course, I'm not sure how lua will behave if you 'unrequire' a file, so someone can chime in on that.

See example:

Code: Select all

local myLocalValue = 10 -- gets removed when application closes

function Player:initialize( )
  local functionLocalValue = 10 -- gets removed as soon as the function finishes
  self.objectValue = 10 -- gets removed when the object is destroyed
end
Also it's worth noting that tables are passed by reference instead of value, so dealing with them is a bit different.

It's a good idea to read up on how lua scoping works.
hasen
Party member
Posts: 157
Joined: Sat Jan 20, 2018 2:01 pm

Re: Difference between local values and self.values in object

Post by hasen »

Ok that makes sense. Thanks.
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 210 guests