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?
Check out my awesome game Doptrix
B
17
S
9
G
28
Posts: 515
Reputation: 16,329

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
I don't check the forums much anymore, but I will receive an email for PMs.

"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,418
Reputation: 4,830

Post » Mon Jan 09, 2017 4:44 pm

You would use an array for this, or even a dicitionary.
ImageImageImage
B
71
S
22
G
241
Posts: 3,734
Reputation: 134,494

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
71
S
30
G
25
Posts: 984
Reputation: 19,503

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.
Check out my awesome game Doptrix
B
17
S
9
G
28
Posts: 515
Reputation: 16,329


Return to How do I....?

Who is online

Users browsing this forum: Artcadev, dop2000 and 48 guests