How do I pick global variables using objects' instance vars?

Get help using Construct 2

Post » Thu May 18, 2017 8:10 am

Hi! I've got a problem that has pretty much stumped me and I feel like I'm missing something obvious. In my game, the player clicks buttons to purchase permanent upgrades (governed by a global variable that says 'yes, they have this upgrade'). That sounds pretty simple, but what I can't figure out is how to make it so that the specific button gives the specific upgrade.

I've done this...

Image

...with the buttons, where 'UpgradeToAdd' governs which upgrade to give the player. Where I am stumped is, I don't know a quick and easy way to translate this to the corresponding global variable ('Upgrade_x') being set from 0 to 1. Basically, what I want to have happen is this:

When the player clicks the button (and can afford the price) -> The upgrade that is called the same as 'var_UpgradeToAdd' is set to '1'.

But I can't do that using 'Set value' from what I can figure out because 'Set value' requires you to pick one variable specifically. What am I missing?
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Thu May 18, 2017 9:37 am

@CJacobsSA

You either need to do a whole series of sub-events:
System: Compare var_UpgradeToAdd = "ThisUpgradeName" (string that matches global variable name)
Set ThisUpgradeName to 1

Or, you can use a Dictionary instead of Global variables, which should let you reference values by a variable:
Set key var_UpgradeToAdd to 1

That would also make it easier to save all upgrades to Local Storage for persistence, using the Dictionary's AsJSON functionality to save a single value rather than all Global variables separately.
Vote for all the Construct 2 games on Steam Greenlight - Find them here
B
33
S
9
G
7
Posts: 450
Reputation: 6,780

Post » Thu May 18, 2017 2:33 pm

The dictionary thing does seem like a better idea, but I can't get it to work as you suggest.

Image

When I do this, it won't allow me, saying that the key value has to be a string... but it can't be a string because then it won't work for referencing the value of the button's variable, it will literally reference the string "obj_button.var_upgradetoadd". I'm not sure how to make it function in the way you've suggested.
B
18
S
9
Posts: 103
Reputation: 1,817

Post » Thu May 18, 2017 2:54 pm

The "Key" is your variable name, and a variable name is a string. Are you trying to affect the variable "Upgrade_X", where "X" is a variable number ? Then you would need to set the Key "Upgrade_" & obj_button.var_upgradetoadd to 1.
B
69
S
28
G
24
Posts: 977
Reputation: 18,739

Post » Thu May 18, 2017 3:02 pm

Ah, I figured out where I was going wrong. The button's 'upgradetoadd' variable was a number, not text. Upon changing it to text, Magistross' clarification works just fine. Thank you both for the help! :)
B
18
S
9
Posts: 103
Reputation: 1,817


Return to How do I....?

Who is online

Users browsing this forum: AndreasR, tarek2 and 12 guests