Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Best to use self.object or create local object ? — Gideros Forum

Best to use self.object or create local object ?

anneMurielleanneMurielle Member
edited March 2014 in General questions
There is something that I'm not really sure...

Could you tell me what it's better to user or what should be avoid ? If I want to create an object that I could use during some time in the application (some click activity or animation or set visibility later or remove it ), is it correct to create it as self.something ? Or it is better to create a global object in a lua page (local something) ?

Thanks

Comments

  • OZAppsOZApps Guru
    Accepted Answer
    You can get different response to this one, however in my opinion here's how I look at it.
    local tempObj = self.Object
    print(tempObject)
    then using tempObj is one opcode in LuaVM whereas,
    print(self.Object)
    is equivalent of two opcodes, one to first get the global to get self and then to get object.

    If there were a couple of lines that would require access to self.Object, that would be slightly more intensive (if you are looking for critical and optimisation) otherwise it should not matter much really.

    Secondly, if you are going to encapsulate everything then fine, otherwise you would need some place to save the objects (animation, etc) which would ultimately need to be global. So you can access it by assigning it to a local variable for that function.

    To back this
    local object = self.object
    object = object + 1
    object = object * 3
    print(object)
    Translates to the following
    getglobal  0   0        ; self
    gettable   0   0   257  ; "object"
    add        0   0   258  ; 1
    mul        0   0   259  ; 3
    getglobal  1   4        ; print
    move       2   0      
    call       1   2   1
    and then
    self.object = self.object + 1
    self.object = self.object * 3
    print(self.object)
    would on the other hand translate to
    getglobal  0   0        ; self
    getglobal  1   0        ; self
    gettable   1   1   257  ; "object"
    add        1   1   258  ; 1
    settable   0   257 1    ; "object"
    getglobal  0   0        ; self
    getglobal  1   0        ; self
    gettable   1   1   257  ; "object"
    mul        1   1   259  ; 3
    settable   0   257 1    ; "object"
    getglobal  0   4        ; print
    getglobal  1   0        ; self
    gettable   1   1   257  ; "object"
    call       0   2   1
    as you can see that each time we use
    self.object
    that first gets the global self, then gets the table sub-item "object" while when we use local, it doesn't have to access the global table and sub item.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • @OZApps
    nice!
    thanks!
    TNT ENGiNE for Gideors Studio - Particle Engine, Virtual Pad, Animator Studio, Collision Engine - DOWNLOAD NOW !!! IT'S FREE!!! -
    www.tntengine.com
Sign In or Register to comment.