How do I use a text global variable to reference a numerical

0 favourites
  • 8 posts
From the Asset Store
Globals 2.0
$3.99 USD
Globals 2.0 stores and group variables. You can also load and save data (variables) from/to JSON files.
  • I am trying to make it so that when you have the resources available to buy a building, that a "!" becomes visible over the build icon. To do this I have a few global variable, but the issue that I am having is that the text variable will not reference the numeric variable.

    Here is my example.

    Global Variable

    Wood (#)

    UpgradeQuantity (#)

    UpgradeResource (TEXT)

    My code goes like this





    UpgradeQuantity <= UpgradeResource Set "!" to visible

    How to I make it so that when it reads "UpgradeResource" that it see's "Wood" and then references the Wood Variable?

  • I guess you could put the requirement on the building object itself as an instance variable. This is how I would do it:

    So on the building object, an instance var :

    WoodRequired : 1

    Then global variables :

    TotalWood : 1

    Then to upgrade :

    For building, if WoodRequired <= TotalWood, Set ! to visible

  • I will try this, thanks.

  • I tried your solution and I don't think it works for my game. The issue is that I want to be able to have the global variable tell the formula which resource is being used. This way when it does upgrade I Can change what resources it needs to get to the next level.

    When the building levels it is going to change what resource and quantity is required for next level, so right now "UpgradeResource" is set to wood, but next level it will be turned into say, water. So I really need to figure out a way that when the compare variable reads "UpgradeResource" it actually points to the "Wood" Global variable.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Use a Dictionary. Add keys, assign values to keys.

  • Never used dictionary, now sure how that will work.

  • Can I use dictionary text string to call a global variable? Cause the resources are all global variables.

  • Global variable UpgradeResource (text) = "Wood" (or whatever is required for a level)

    Global variable NeededResources (number) = required amount

    Add all the resources as keys to an dictionary, assign values, and update them while gathering resources.

    Now you can ...

    Dictionary > Compare value > Key = UpgradeResource ... > .. Value = NeededResources

    __________action ... level up

    That, ofcourse, works fine until you need more 'NeededResources' to level up. At one point you could feel the need to level up when there is 500 wood and 1200 iron gathered.

    Then you come back, and i will advise you to not use that logic that you so really want to use at this point. Just bring it all in 1 array.

    Alternative. You can make a global for each resource, holding whats needed to upgrading.

    Global 'wood' = 20

    Global 'iron' = 0

    Global 'meat' = 55

    Using a dictionary .....

    Dictionary > Compare value > Key = "wood" (a string) ... > .. Value = 'wood' (the global) - 1

    Dictionary > Compare value > Key = "iron" ... > .. Value = 'iron' - 1

    Dictionary > Compare value > Key = "meat" ... > .. Value = 'meat' - 1

    _______actions whatever you want to do to perform a level up

    Arrays & Dictionaries are just, and not more then, a variable holding more values. You should not shy away from them.

    You will need them for 100 % sure at one point. Can as well start using now in a not to complicated logic.

    Want to stay with the concept of globals. No problem. Just cant do that logic that you so weirdly want to do.

    3 Globals.

    1 for each resource, holding whats needed to upgrading.

    Global 'LvUwood' = 20

    Global 'LvUiron' = 0

    Global 'LvUmeat' = 55

    3 Globals.

    1 for each current gatherd resource.

    Global 'CuRwood' = updating while gathering

    Global 'CuRiron' = updating while gathering

    Global 'CuRmeat' = updating while gathering

    System compare 2 values > 'Curwood' > 'LvUwood' -1

    System compare 2 values > 'CuRiron' > 'LvUiron' -1

    System compare 2 values > 'CuRmeat' > 'LvUmeat' -1

    _____________Actions .... level up

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