Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
What are classes? — Gideros Forum

What are classes?

mejonmejon Member
edited July 2015 in General questions
Hello, I was wondering what are classes in Gideros? What are they used for, and are they always used in projects?

Comments

  • piepie Member
    One real explanation:
    https://en.m.wikipedia.org/wiki/Class_(computer_programming)

    My dummy approach:
    Classes are "objects" to use and reuse, with inheritance capabilities. I think it's almost the same thing in any oop language: gideros has several core classes like Sprite, Bitmap... anything that you init with ".new ()" is a class.
    To create your own class you can inherit properties from other classes or start a blank new one.

    For example, this should work but I didn't test it and it's late here.. :)
    Player = Core.class (Sprite) --inherit Sprite class properties and create Player class
     
    function Player:init (name) --init the class properties, name is supposed to be given when creating this class
     self.name= name --get player name and save it as object property
    --here you can add everything you may need, from textures to "hitpoints"
    end
     
    function Player:displayName () --this is a method of Player class. I choose just to print the name of the player, it could be anything else (hit, jump, growteeth...)
     print ("player name", self.name) --print the name
    end
     
    local playerpuppet = Player.new ("jack") --create a new instance of Player, with name "jack"
    playerpuppet:displayName () --call the method of Player to display its name
    --eventually you can add it to stage or another Sprite (to 'see' it you need to add a texture somewhere, for example in init () )
    stage:addChild (playerpuppet)
    Hope this helps! :)
  • Classes in Lua are just tables with some extra functionality.

    http://lua-users.org/wiki/ObjectOrientationTutorial
  • Some extra hints:
    Don't confuse classes and objects. A class is a blueprint. It's like a plan that says, every house should have an address, 4 windows and an entrance door, and it should be able to take up up to 5 persons.

    Now you can create house objects from the house class. Making changes on the class will affect all objects derived from it, but changing one object will no affect anything else.

    People will tell you over and over again, that there are no real classes in lua, instead there are just tables. Although being correct, I don't think this has any implications for a newcomer game developer. It's the same in Javascript. There you build object from a "mother object" (called prototype). And still, you can do exactly the same things that real classes can do, if not more.

    Just one more thing: Gideros objects are really hard to serialize, contrary to, for example, php.
Sign In or Register to comment.