How do I access instance variable by name

0 favourites
  • 5 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Is it possible to access object's instance variable by its name?

    Say I have an object monster with instance variables param1, param2, param3, [...] param10

    I want to do something like this

    for x=1 to 10  
       monster.Variable("param" & loopindex) = random(1000)[/code:2fnu5ayr]
    instead of 
    [code:2fnu5ayr]monster.param1= random(1000)
    monster.param2= random(1000)
    monster.param3= random(1000)
    monster.param4= random(1000)
    ...
    monster.param10= random(1000)[/code:2fnu5ayr]
    
    Another example:
    [code:2fnu5ayr]if monster.Shield>0 then MyVar="Shield"
       else if monster.Armor>0 then MyVar="Armor"
             else MyVar="Health"
    
    monster.Variable(MyVar) =  // some complex calculations here to apply damage to monster's shield or armor or base health[/code:2fnu5ayr]
    
    I'm guessing there is no built-in functionality for this.. Maybe there is a plugin or some workaround?
  • I am not sure this is possible, nor do I know of any already existing plugins for it.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • You would use an array for this, or even a dicitionary.

  • You could use a container which would group your "monster" object with a Dictionary object (say "monsterDictionary"). So each instance of "monster" would have its own dictionary.

  • Thank you all for the replies. Using a dictionary with monster oblect might be a good idea, I'll give it a try.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)