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

0 favourites
  • 5 posts
From the Asset Store
Pick Up Items Sound effects for your game, Take them for a ride right now and you will worry no more.
  • 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...

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

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • 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.

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

    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.

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

  • 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!

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