Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Saving static data between class methods. — Gideros Forum

Saving static data between class methods.

ZelgodZelgod Member
edited July 2013 in General questions
Admittedly I am new-ish to Object programming and i am new to lua, i come from programming in javascript and PHP. Previously i've been using appcelerator for my projects, which is fine but sub-par for games. The forum has been helpful up to this point however im having difficulty understanding a few basic concepts that maybe someone(s) can shed some light on.

I guess the first is understanding classes despite scouring the internet and reading through articles.

What is the best practice method of creating static class variables? ex.

myclass.lua
-------
MyClass = Core.class()

function MyClass:init()
self.mystatic = 1
end

function: MyClass:someOtherMethod()
self.mystatic2 = self.mystatic2
end
------

I've seen a local table being used as storage but is this correct? should i be storing other objects in the same way?

local storage = {
myvar1 = "something"
refobj = someobject
}

My other thought is the use of metatables but honestly at this point i don't know what the best practice or simplest approaches are.

Any explanation is greatly appreciated.


Comments

  • atilimatilim Maintainer
    edited July 2013
    Hi,

    For static variables you can use:
    MyClass = Core.class()
     
    MyClass.mystatic1 = 1
     
    function MyClass:init()
        MyClass.mystatic2 = 1
    end
     
    function MyClass:someOtherMethod()
        MyClass.mystatic3 = MyClass.mystatic4
    end
    And use local as much as possible (Lua is global by default).

    And I highly recommend Programming in Lua (2nd ed) to learn all the details of Lua 5.1.
  • ZelgodZelgod Member
    edited July 2013
    I guess my second question has now become is there a reason i cannot access a executed lua file (containing a class or module) using that method?. ex

    MyClass.mystatic1 = MyExternalClass.new()

    I get thrown a "attempt to index global 'MyExternalClass' (a nil value)" even though the file has not been excluded from execution (myexternalclass.lua).
  • MellsMells Guru
    edited July 2013
    @Zelgod did you check the order of execution? ("Code dependencies", access it the same way as "Exclude from execution")
    twitter@TheWindApps Artful applications : The Wind Forest. #art #japan #apps
  • edited July 2013
    @Zelgod and maybe name of class in myexternalclass.lua is not MyExternalClass?

    Can you try this: (copy and modify from @atilim post above)
    MyExternalClass= Core.class()
     
    MyExternalClass.mystatic1 = 1
     
    function MyExternalClass:init()
        MyExternalClass.mystatic2 = 1
    end
     
    function MyExternalClass:someOtherMethod()
        MyExternalClass.mystatic3 = MyExternalClass.mystatic4
    end
    Coming soon
Sign In or Register to comment.