Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Gideros class — Gideros Forum

Gideros class

zoolaxzoolax Member
edited January 2013 in Game & application design
I know in lua you can fake OOP by doing

--regular way
--This is what I am used too.
local player ={}
--Do your methods here
return Player

--Gideros:
Player = Core.class(Sprite)
function Player:init()
--Some vars
end
--Create some methods

Is making the class in Gideros way will make the app more optimized?
Thanks again in advance
www.zoolax.com
Tagged:

Comments

  • john26john26 Maintainer
    edited January 2013 Accepted Answer
    Here's my understanding of what you call the regular way
    function newdog(c)
      local dog={colour=c, speak=function(self) print ("woof! My colour is:",self.colour) end}
      return dog
    end
     
    fido=newdog("blue")
    fido:speak() -- output: woof! My colour is blue
    So newdog is a factory function that returns dogs. You can also pass initialisation info through. In this case the dog's colour.

    However, with this inheritance is not so easy. You could do
    function newAlsatian(b,c)
      local alsatian=newdog(c)
      alsatian.breed=b
      alsatian.speak=function(self) print("How do you do? My breed is",self.breed) end
      return alsatian
    end
    which is maybe not so elegant but it does give you very explicit control over initialisation. In another thread people are confused about the automatic initialisation in Gideros so maybe this way is better, more verbose but less confusing..?

    But in the Lua manual chapter on OO, it recommends a different way using metatables and I think that's what Gideros is using. I've never really understood what metatables do and not sure why this is better than the way just described...?
  • @john,
    Thanks john:)
    www.zoolax.com
  • BJGBJG Member
    I wish I understood this stuff more clearly. I'll have to stare at that example for a while.
  • PlatypusPlatypus Member
    edited April 2013
    Using two full stops instead of commas seems to make the spacing nice.
    function newdog(c)
      local dog={colour=c, speak=function(self) print ("(Woof!) I am a "..self.colour.." dog.") end}
      return dog
    end
     
    fido=newdog("blue")
    fido:speak() -- output: (Woof!) I am a blue dog.
    function newdog(c)
      local dog={colour=c, speak=function(self) print ("(Woof!) I am a",self.colour,"dog.") end}
      return dog
    end
     
    fido=newdog("blue")
    fido:speak() -- output: (Woof!) I am a	blue	dog.
    Edit:
    Caution, beginners (like me)! - Please read OZApps' comment below.
    Kate's Catalogue of Travelling Theatre Centres :
    Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
    She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.
  • @Platypus, the only issue with the '..' which is the concatenation operator in Lua is that if one of the value is 'nil' then it will throw an error, however with a coma ',' it will print is irrespectively.

    Likes: Platypus

    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
    +1 -1 (+1 / -0 )Share on Facebook
  • Oh! I see.

    Thanks, @OZApps.

    Kate's Catalogue of Travelling Theatre Centres :
    Meet Kate. Grey is her favourite colour. Maths is her favourite subject. Decency is her favourite type of behaviour.
    She definitely does not like jewellery or modelling, but loves aeroplanes and other machines made of aluminium.
Sign In or Register to comment.