How do I use arrays to change more than one variable?

0 favourites
  • 3 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • When you win in a level, I want to show the player 3 random options of stat increases and items they can choose from, some rarer than others. I don't know much about arrays and I am have spent 3 hours trying to think of a way to do this.

    So for example it chooses from this

    30% Coins

    10 coins

    15 coins

    25 coins

    50% health

    +5 health

    +10 health

    20% attack increase

    +20 magic attack

    I think it is possible to use Arrays.At(random(_,_),random (_,)), but I want to display the options the player has.

    I also don't know how to add the amount chosen to the correct variable. For example, if I get 10 health, I can't just add it to a variable because "+10 health" would be a string (I think).

    Another problem is that I don't know how to add it to the correct variable because there are a lot of variables.

    Thank you for reading this. I would really appreciate it if you helped me.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • This is not an easy task.. I have done similar thing using JSON and probability tables.

    You can create a JSON with all upgrade options and their weights/chances (in percents), for example:

    {"Prizes": [
     {
     "prize": "coins",
     "amount": 50,
     "chance": 5
     },
     {
     "prize": "coins",
     "amount": 100,
     "chance": 2
     },
     {
     "prize": "health",
     "amount": 10,
     "chance": 3
     },
     {
     "prize": "health",
     "amount": 15,
     "chance": 1
     },
     {
     "prize": "attack",
     "amount": 1,
     "chance": 7
     },
     {
     "prize": "attack",
     "amount": 2,
     "chance": 3.5
     }
    ]}
    

    So the chances to pick 50 coins as a possible prize is 5%, the chances to pick 10 health is 3% and so on. (Ideally all chances should add up to 100%, but not necessary)

    Then you create a probability table - feed all these items and their weights to the Advanced Random plugin:

    + JSON: For each entry in "Prizes"
    -> AdvancedRandom: Add entry JSON.CurrentKey with weight JSON.Get(".chance")
    

    After that you can pick three random values. They will be weighted, i.e. their chances to be picked will be based on the percentage you specified in JSON.

    | Local string randomPrize1‎ = 
    | Local string randomPrize2‎ = 
    | Local string randomPrize3‎ = 
    
    -> System: Set randomPrize1 to AdvancedRandom.Weighted
    -> System: Set randomPrize2 to AdvancedRandom.Weighted
    -> System: Set randomPrize3 to AdvancedRandom.Weighted
    

    At this point three variables will contain three random indices from JSON.

    You can access each prize in JSON, for example:

    PrizeText set text to JSON.Get("Prizes." & randomPrize1 & ".prize")
    AmountText set text to JSON.Get("Prizes." & randomPrize1 & ".amount")
    

    And based on that decide which variable to increase - gold, health or attack.

    .

    You can use an array to store all those options. I just find JSON easier to work with.

  • Thank you very much dop200. I don't know much about JSON and advanced random, but I'll try to do this.

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