Private Variable Help [Solved]

This forum is currently in read-only mode.
From the Asset Store
Game with complete Source-Code (Construct 3 / .c3p) + HTML5 Exported.
  • Hello CC aficionados!

    I've got a simple question that I can't figure out. I want a sprite to spawn then be given a private variable of "statues points" (let's say 100)

    I want to be able to split those points randomly into five other private variables with it equaling the allocated "statues points" (100).

    for example

    Vit: 15

    Dex: 17

    Str: 35

    Int: 20

    Def: 13

    Which all equals 100

    tl; dr: dispersing a total point value, "X", randomly between multiple private variables that equals "X" when totaled

    Thank you so much for the time!

    Help is greatly appreciated!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Multiply X by a fraction that represents each object variable - the sum of the fractions should add up to 1.

  • Multiply X by a fraction that represents each object variable - the sum of the fractions should add up to 1.

    Hey Colludium!

    Thanks for the answer, but that's not exactly what I had in mind. I used X (100) as an example, but I'd prefer it to be dynamic as well as random. Say numbers like 317, or 61, you know, numbers that might not always be divisible by 5. Say I have something like three Stats, here are some example possible, desired outputs:

    You get 22 points to be assigned (22 can't be divided by 3 cleanly).

    -RNG build number 1-

    Str: 5

    Def: 8

    Dex: 9

    -RNG build number 2-

    Str: 3

    Def: 17

    Dex: 2

    -RNG build number 3-

    Str: 0

    Def: 0

    Dex: 22

    That way if I was to spawn items or mobs, it would scale with rarity. Say I want common objects to spawn with 52 points to use, while extremely rare spawns could use 133 points. If this doesn't make sense I am extremely sorry. If only there was a way to randomly select a private variable on an object I could keep assigning points and loop it until the stock was depleted. That was my original (hack) idea hahaha

  • Surprisingly, I found a work around! Will close this thread on request.

    X = 100

    a = random(0, 1) * X

    b = random(0, 1 - a) * X

    c = (1 - a - b) * X

  • You could use a loop.

    Just use while(stat_points>0) and tell it to add points to a random variable.

    To do this, you can have 2 global "temp" variables. For each loop, set temp1 to a random value, then under the loop, compare temp.

    while

    stat_points>0

    always set temp1 to random(2)

    always set temp2 to random(10)+1

    if temp1=0, set STR to temp2

    if temp1=1, set SPD to temp2

    always subtract temp2 from stat_points

    With this, stat_points could end up being negative and the sum of all stats could be greater than 100. If you want to make sure it equals exactly 100, you could limit the value 'temp2' gets by clamping it.

    ex. set temp2 to clamp(random(10),1,'stat_points')

    Temp1 is the variable it's giving points to. Temp2 is the how many points it gives that stat.

    This will just keep allocating points to different stats until there are no more points left.

    This will scale to any amount of stats, unlike the solution you came up with.

    To make sure this only happens when the object spawns, I use an "initialized" flag on all objects. Make the default value of this = 0. Then test if initialized is 0 before doing the loop. When the loop is over, or anything else regarding initializing the object is done, set initialized to 1.

    Temp variables are very useful because they can be used for anything. Lots of times you just need to temporarily use some variable to count things in a loop.

    You can also point to different variables if you don't want to test temp1, say for example, if you have like 25 different stats, but you shouldn't need to worry about that.

    This is how I'm making random weapons/armor pieces in my game.

  • Great advice, Octopus tophat! This brings it much better into perspective! I think I'll use your method instead.

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