Set variable to range based on another variable

● ARCHIVED · READ-ONLY
Started by Sytze 5 posts View original ↗
  1. Hi everyone,

    The title sounds very complex, but I will try to explain as best as I can what I'm looking for.
    In my game the player has a number of troops (from now on called X) I want the number of enemy troops to be randomly assigned, but based on this number of troops the player has.

    What I'm basically looking for is to do this:

    EnemyTroops = PlayerTroops +/- (0 to 0.5) PlayerTroops

    So as an example, if PlayerTroops is 200, I would want EnemyTroops to be anywhere within the range of 100-300
    I know this will somehow require a randomization, but with my limited knowledge of scripting I can't figure out how this would work.

    Edit: Perhaps something like this? But I don't know whether this would work.

    0.5 * $gameVariables.21 + Math.random($gameVariables.21)

    Thanks in advance!
  2. Why use scripts?
    This could be done with default control variable command.
    One variable to get a random percentage to be multiplied with the other variable...
  3. Andar said:
    Why use scripts?
    This could be done with default control variable command.
    One variable to get a random percentage to be multiplied with the other variable...
    Mostly because that would take up another variable spot (I prefer to use them as little as possible). I also don't know how to use percentages in the multiplier, since you can only set variables to whole numbers. If I used your method it would be something like
    set variable 22 to 50-150 and then do set variable 21 to multiply by variable 22, but then it would be a factor 100 too much?
  4. that is why you need another control variable to divide by 100 after the multiplication. But it is important to do that only after the multiplication because the numbers are always rounded down to next integer.

    and what I do for things like that is to define a few variables for temporary mathematical use, using the same variables again as long as their values don't need to be stored beyond the event with the calculation.
  5. Andar said:
    that is why you need another control variable to divide by 100 after the multiplication. But it is important to do that only after the multiplication because the numbers are always rounded down to next integer.

    and what I do for things like that is to define a few variables for temporary mathematical use, using the same variables again as long as their values don't need to be stored beyond the event with the calculation.

    Would it be possible to do in the following way ?
    Set variable(random) to 50-150
    Set variable (random) to multiply with variable(PlayerTroops)
    Set variable(random) to divide by 100
    Set variable(playerTroops) to add variable(random)

    Edit: I got it working by using the following commands:
    Control variables: VRandom = Random0..100
    Control variables: VRandom *= PlayerTroops
    Control variables: VRandom /= 100
    Control variables: EnemyTroops = PlayerTroops
    Control variables: EnemyTroops /= 2
    Control variables: EnemyTroops += VRandom

    This seems like a really long way to do something simple though...