Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
lua oop question - Gideros Forum

lua oop question

keszeghkeszegh Member
edited February 2013 in General questions
i have several instances of one object/class and i realized that this object has like 6 variants of a constructor function which are only invoked when the object is initialized. now what i don't know if these functions are stored for each instance separately? if yes, then probably i should make these functions global and change them so they give back a prepared fully constructed object. this way these functions would be in the memory once instead of 100 times.
as far as i remember in more oop-oriented languages you can decide if a function of a class is global or local in the sense that it is one for the whole class or one for each object. can you make such a differentiation with lua? and which case is happening by default?
my lack of understanding what is happening in the background goes farther of course, e.g. what if a function of the class is put on each onenterframe of each instance, then do we have 100 copies of this function in the memory or only 1? but maybe this works exactly the same way as my question above and making it an eventhandler does not change the number of functions in memory.

so probably there is an easy answer, please tell me.


  • @keszegh

    well if by global methods you mean static methods, then you can store them as properties of a class, as:
    --method for this specific instance
    function Sprite:somemethod()
        --self references instance
    --static method for all instances
    funciton Sprite.staticmethod()
        --self is nil

    Likes: zvardin

    +1 -1 (+1 / -0 ) Share on Facebook
  • thanks.

    and of the two ways which is stored only once in the memory? only the static? none? both?
    because actually being able to use self would be useful even in a function which is static in the above sense.

    i hope my question is understandable.
  • ar2rsawseenar2rsawseen Maintainer
    edited February 2013
    @keszegh static is stored once (although I think they both are, but static definitely) and there is nothing that prohibits you from passing self to it:
    --static method for all instances
    function Sprite.staticmethod(self)
        --self is now a reference to instance
  • My guess is they're stored once regardless and then each instance of that class will have a reference to them. (For either way the function is declared)
  • thanks for the answers,
    finally i put as much as i could outside of the class so that i save even the references. i doubt that it changed anything performance-wise, but whatever.
    maybe @atilim could give the final 'official' answer in this discussion about static and normal functions of classes, how they are stored/referenced per instance?
  • zvardinzvardin Member
    Accepted Answer
    Doing a quick test, I can confirm that the function is defined once and then instances of the class contain a reference to it.
  • zvardinzvardin Member
    edited February 2013
    The test I used:

    Test = Core.class()
    function Test:init()
    function Test:test1(param)
    function Test.test2(self, param)
    local t = Test.new()
    local t2 = Test.new()
    print(t.test1, t2.test1, Test.test1)
    print(t.test1 == t2.test1 and t.test1 == Test.test1 and true) 
    print(t.test2, t2.test2, Test.test2)
    print(t.test2 == t2.test2 and t.test2 == Test.test2 and true)
    Output is:
    Uploading finished.
    function: 07127F30 function: 07127F30 function: 07127F30
    function: 07127F50 function: 07127F50 function: 07127F50
Sign In or Register to comment.