Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
A question about variables — Gideros Forum

A question about variables

NinjadoodleNinjadoodle Member
edited March 2016 in General questions
Hi guys

I am making a game where all the levels are quite short and different.

I am setting up my levels objects this way ...
self.monster1 = ...
self.monster2 = ...
etc.

... to insure that they get removed when the scene is changed.

The question I have is about variables.

I've been setting up my variables by using ...
local myVar = something
but I'm wondering if I should be using the same approach as before ...
self.myVar = something
Could anyone help me understand which is better?

Thank you in advance!

Comments

  • ar2rsawseenar2rsawseen Maintainer
    local myVar = something
    would be removed once you exit the scope, which usually is a function
    self.myVar = something
    would be removed once whole class/object is garbage collected

    So it depends on how long you need your value to live :)

    Likes: antix, Ninjadoodle

    +1 -1 (+2 / -0 )Share on Facebook
  • NinjadoodleNinjadoodle Member
    edited March 2016
    Hi @ar2rsawseen

    Thank you for the explanation :)

    So, if Im setting up some variables inside the scenes init function ...
    Level1 = gideros.class(Sprite)
    function Level1:init()
     
    local myVar1 = something
    self.myVar2 = something
    ... would both of these get removed at the same time when the scene is exited (or changed to another scene)?

    Thank you again!
  • antixantix Member
    edited March 2016
    I am not sure but I think no. In all my classes I have a custom function that attempts to cleanup any variables and resources like so..
    SomeClass = Core.class()
     
    function SomeClass.init()
      self.var1 = 1
      self.var2 = 2
    end
     
    function SomeClass:doSomething()
      math.random()
    end
     
    function SomeClass:dispose()
      self.var1 = nil
      self.var2 = nil
    end
     
    local someThing = SomeClass.new() -- MAKE A NEW OBJECT
    someThing:doSomething() -- USE IT TO DO STUFF
    someThing:dispose() -- DISPOSE OF IT
    someThing = nil -- AFTER THIS IT SHOULD GET GARBAGE COLLECTED
    In theory at the end of my example the class should be disposed and be ready for garbage collection.

    Personally I think that's the best way, but somebody might have a better one.

    Likes: Ninjadoodle

    +1 -1 (+1 / -0 )Share on Facebook
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    local myVar1
    would be accessible only in init function and should be garbage collected automatically when exiting init function
    self.myVar1
    would be accessible in any method of this class and should be garbage collected automatically when class gets garbage collected.

    Here is more info on garbage collection with real example including global variable
    http://www.giderosmobile.com/forum/discussion/comment/9441#Comment_9441
  • NinjadoodleNinjadoodle Member
    edited March 2016
    Hi @antix and @ar2rsawseen

    Thank you again for the explanation and links, it really helps clarify things :)
Sign In or Register to comment.