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?
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :) If you'd like to hire me, please see this post
B
17
S
9
G
103
Posts: 1,469
Reputation: 55,507

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
23
S
6
G
1
Posts: 1,419
Reputation: 4,867

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
267
Posts: 3,807
Reputation: 147,745

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
78
S
32
G
27
Posts: 1,021
Reputation: 20,952

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.
Image

Did I help to fix a problem in your game? You can buy me a cup of coffee :) If you'd like to hire me, please see this post
B
17
S
9
G
103
Posts: 1,469
Reputation: 55,507


Return to How do I....?

Who is online

Users browsing this forum: timcs and 7 guests