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

Reference class by name

HolonistHolonist Member
edited May 2015 in General questions
Hi,

How do I store the name of a class in a variable?

For example I have a class called "Hit".

The player object has the following attribute:
self.skill = Hit

Calling player.skill.new() doesn't work.

I need to store what kind of skill the player has, but don't want to initialize it right away.

Can I store classes in an associative table? I can't imagine how this would look, but it would solve my problem by something like skills[player.skill].new()

Comments

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    Hello @Holonist

    it should have worked the way you stated, saving class in a variable.

    Example that works for me:
    Hit = Core.class()
     
    function Hit:init()
    	print("hitting")
    end
     
    local test = Hit
     
    test.new()
    prints hitting
  • hgy29hgy29 Maintainer
    Accepted Answer
    Calling player.skill.new() should have worked, and yes you should be able to store classes in associative tables. There are no such things as 'classes' in lua, every complex type in lua is a table, so would you are trying to do should defintely work.
    When you say 'doesn't work', what happens exactly ?
  • HolonistHolonist Member
    edited May 2015
    I have two classes which need a skill attribute. Fighter and player. Player basically hands its values to fighter, so the battle action can happen without altering the players permanent stats.

    I actually forgot to add skill to fighter (who then uses it). Therefore it said "try to access a nil value". Kind of a dumb mistake, but thanks for your both responses, saved a lot of wondering on my side!

    Tl;dr: lua ftw
Sign In or Register to comment.