Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Class system: Is there any instanceOf mechanism? — Gideros Forum

Class system: Is there any instanceOf mechanism?

seppseppseppsepp Member
edited October 2013 in General questions
Hi @all,

simple question as always ;) : In the Gideros class system is there any kind of an instanceOf mechanism? I could save class names or some other similar signature manually. But is this the way to go?

Thanks in advance

Sebastian

Comments

  • @seppsepp it could be like:
    local s = Sprite.new()
    if getmetatable(s) == Sprite then
        -- s is instanceOf Sprite
    end
  • Unfortunately that doesn't work. Metatable is different to the instance =| .
  • jdbcjdbc Member
    Accepted Answer
    Use a class property defined in the constructor:

    NewClass = Core.class(Sprite)

    function NewClass:init()
    ...
    self.class = 'NewClass'
    end
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    yes you are right, it would work only for native classes.

    then probably setting id or name is the way to go
  • I have a base class for all game objects.
    One of his methods - GetType(), which throws an exception if not overridden by a subclass.
  • @asakharov - how do you check if a function is not overridden, can you provide code sample?
  • GameBaseObject = Core.class(Sprite)
     
    function GameBaseObject:init()
        ....
    end
     
    function GameBaseObject:getType()
        error("Undefind class type name. Please override it in appropriate game object.")
    end
    -----------------------
     
    MyObject = Core.class(GameBaseObject)
    function MyObject:init()
        ...
    end
    -----------------------
     
    instance = MyObject.new()
    print(instance:getTypeName()) <-- Exception with text "Undefind class type name. Please override it in appropriate game object."

    Dislikes: bgcis

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