Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
OOP in gideros — Gideros Forum

OOP in gideros

mumujamumuja Member
edited September 2011 in General questions
According to the guide:

gideros.class function is used to create your own classes through inheritance. You can only inherit from Gideros API's own classes (EventDispathcher, Sprite, etc.) and you cannot inherit from your own classes.

However, I have successfully extend from my own class?
Here is the snippet of code, granted that the base class is still the API class
Parts = gideros.class(Sprite)
Parts.position = {x = 0, y = 0}
function Parts:init()
end
--------------------------------------------
Head = gideros.class(Parts)--Parts is something I define myself
 
function Head:init()
end
--------------------------------------------
local test = Head.new()
print(test.position.x) --prints 0
Either the guide is misleading or this is an unintended feature.

Comments

  • atilimatilim Maintainer
    In fact, it's an unintended feature :)

    At first I think that init functions may not be called correctly, but they're called and with the correct order (first Parts:init, then Head:init)

    After doing more tests, I will update the documentation

    Thank you,
  • Hi,

    @atilim: I need this too. So, can I use that way officially ? Did you update documents?
    Thank Mumujar!

    Thank you,
  • atilimatilim Maintainer
    Yes we've extended the class system and tested by inheriting multiple times. You can use it officially with the new release.
    +1 -1 (+2 / -0 )Share on Facebook
  • RickyngkRickyngk Member
    edited February 2012
    If I split the code into 3 files as following:
    --------------------------------------------
    --File1.lua
    --------------------------------------------
    Parts = gideros.class(Sprite)
    Parts.position = {x = 0, y = 0}
    function Parts:init()
    end
    --------------------------------------------
    --File2.lua
    --------------------------------------------
    Head = gideros.class(Parts)--Parts is something I define myself
     
    function Head:init()
    end
    --------------------------------------------
    --File3.lua
    --------------------------------------------
    local test = Head.new()
    print(test.position.x) --prints 0
    Then, I've got issue:
    [string "property.lua"]:138: attempt to index upvalue 'b' (a nil value)
    stack traceback:
    	[string "property.lua"]:138: in function 'new'
    Edit: It's ok if merge file1 and file2
  • Must be something else. If I take this pure code, I get no errors in 2012.2.
  • run with no error in beta8
  • RickyngkRickyngk Member
    edited February 2012
    Sorry, my mistake,
    File 1:
    Head = gideros.class(Parts)--Parts is something I define myself
     
    function Head:init()
    end
    File2:
    Parts = gideros.class(Sprite)
    Parts.position = {x = 0, y = 0}
    function Parts:init()
    end
  • MikeHartMikeHart Guru
    edited February 2012
    What is the difference to your second last posting? Still get no error.
  • hnimhnim Member
    edited February 2012
    i got another error event i merge file 1 and file 2 (test.position is always nil)
    f3.lua:2: attempt to index field 'position' (a nil value)
    stack traceback:
    	f3.lua:2: in main chunk
    If i place Part definition before Head, everything is ok. I think problem is order when compile code
  • Of course it would be. if you execute fiel1 before file2, then Parts is undefined. Make sure you set the order correctly in your project settings.
  • moopfmoopf Guru
    edited February 2012
    Hmm, trying out inheritance at the moment. I have this:
    RoomWrapper = gideros.class(Sprite)
     
    function RoomWrapper:init()
    	self.props = {}
     
    end
     
    function RoomWrapper:thisIsMyTest()
    	print ("in room wrapper!")
    end
    Room = gideros.class(RoomWrapper)
     
    function Room:init()
           self:thisIsMyTest();
    end
    However, I get "attempt to call method thisIsMyTest (a nil value)".

    It's been a long day so I'm probably missing something obvious here.
  • Works fine with me. Make sure you have your files in the correct order.
  • Hmm, do you mean in the Project bar on the left? Or is there somewhere else to set the execution order of .lua files?
  • Ah, not to worry, just found the 'Code Dependencies' option in the menu when you right click on a Lua file. Hadn't seen that before.
  • CarolineCaroline Guru
    edited April 2012
    @atilim - I tried to sign up with the bug tracker so that I could write this there, but I never got a confirmation email, so couldn't sign in.
    After doing more tests, I will update the documentation
    I think this still needs updating on this page:
    http://www.giderosmobile.com/documentation/classes_in_gideros.html, as you can inherit from your own classes(?):

    Core.class function is used to create your own classes through inheritance. You can only inherit from Gideros API’s own classes (EventDispathcher, Sprite, etc.) and you cannot inherit from your own classes.

    For example, you can create your EventDispathcher class like:

    MyEventDispathcher = Core.class(EventDispathcher)


    Also Dispatcher's spelled wrong.

  • gorkemgorkem Maintainer
    Caroline - just confirmed your email. There's an issue with how bugs.giderosmobile.com sends emails.
  • atilimatilim Maintainer
    Hi Caroline,

    I've changed all "*Dispathcher"s to "*Dispatcher" and updated the documentation.

    Thank you :)

  • There's duplicate Gideros documentation in under the Gideros Academy links and in the Ultimate Guide to Gideros and both and aren't always updated. For example:

    * The document about classes under Gideros Academy isn't correct regarding inheritance from user classes.

    * The document in the Ultimate Guide to Gideros still has the dispatcher spelled incorrectly (well, it did ... I corrected it).

    A suggestion: change pointers in Gideros Academy so that they point into the Ultimate Guide to Gideros (where appropriate) so you don't have to maintain two copies of the same information.
  • @Atilim, am I wrong if I presume that now I can inherit from any class, do not have to inherit from the Sprite class (if the class is non visual)? I guess that would also have some optimisation in terms of memory usage as it would not have all the visual stuff that might not be required. is my understanding right?
    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
  • atilimatilim Maintainer
    @OZApps exactly
    A = Core.class()  --> create a base class
    B = Core.class(A) --> inherit B from A
    C = Core.class(B) --> inherit C from B
  • Then could I assume you can not override the init? by making a new init for a second of 3rd level object?
    Regards
    In fact, it's an unintended feature :)

    At first I think that init functions may not be called correctly, but they're called and with the correct order (first Parts:init, then Head:init)

    After doing more tests, I will update the documentation

    Thank you,
    REAL programmers type copy con filename.exe
    ---------------------------------------
  • ar2rsawseenar2rsawseen Maintainer
    edited March 2013
    @Cyberience no you can't, for that purpose you can use postInit method.
    postInit is called after init method upon creation.
    So you can define in parent class to do something in postInit and then redefine the postInit method in child class.

    Here is a reference to the thread about postInit http://www.giderosmobile.com/forum/discussion/2506/is-it-possible-to-call-subclasss-overwrited-function-in-baseclasss-init-function/p1

    Likes: Cyberience

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.