How do I access instance variable by name

Get help using Construct 2

Post » Fri Jan 06, 2017 4:23 am

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
Code: Select all
for x=1 to 10 
   monster.Variable("param" & loopindex) = random(1000)

instead of
Code: Select all
monster.param1= random(1000)
monster.param2= random(1000)
monster.param3= random(1000)
monster.param4= random(1000)
...
monster.param10= random(1000)



Another example:
Code: Select all
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


I'm guessing there is no built-in functionality for this.. Maybe there is a plugin or some workaround?
B
14
S
5
G
1
Posts: 6
Reputation: 1,416

Post » Mon Jan 09, 2017 4:39 pm

I am not sure this is possible, nor do I know of any already existing plugins for it.
https://www.ravenheart.ca/home
Company name changed to avoid Facebook-type shenanigans

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
22
S
6
G
1
Posts: 1,414
Reputation: 4,822

Post » Mon Jan 09, 2017 4:44 pm

You would use an array for this, or even a dicitionary.
ImageImageImage
B
71
S
21
G
221
Posts: 3,649
Reputation: 124,227

Post » Mon Jan 09, 2017 5:19 pm

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.
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Thu Jan 19, 2017 5:40 am

Thank you all for the replies. Using a dictionary with monster oblect might be a good idea, I'll give it a try.
B
14
S
5
G
1
Posts: 6
Reputation: 1,416


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, Casimir, pwrtoppl, resdesign and 9 guests