Setting a Random Variable in Scripts

● ARCHIVED · READ-ONLY
Started by firevenge007 9 posts View original ↗
  1. I've been trying to make a thieving formula in my game

    The formula is $game_variables[306] = $game_variables[304] + 1 /$game_variables[305], where variable 304 is the thieving level and 305 is a constant.

    Variable 304 is meant to be used as the fail rate maximum.

    Ex: If a player has a thieving level of 19 and the constant is 4, (19+1)/4 = 5. Then, I would create another variable which would be a random one, which would determine whether the player has failed or successfully thieved the NPC.

    It would be a random variable anywhere from 1 to variable 304, the maximum variable. If that variable were to equal Variable 304, then the player would be caught.

    So in the example above, it would be rand(1..4), and if the newest var were to equal 4, then the player is caught.

    Unfortunately, I do not know how to get this to work, as rand(1..$game_variables[306]) does not do what I expected it to do.

    Does anyone know how to go about this?

    Thanks
  2. I've moved this thread to RGSSx Script Support. Please be sure to post your threads in the correct forum next time. Thank you.


    In the formula you have, you'd always end up with $game_variables[304] as the result.


    Following normal rules of operation, the 1 / $game_variables[305] would always be done first, which would result in 0 because they are both integers and the answer will be truncated to an integer. So you're just adding 0 to the starting value.


    I don't follow what you mean by "if the player has a thieving level of 19 and the constant is 4, I would create another variable which would be a random one". Where does the randomness come in? You have a level and a constant. There's nothing random about that.


    Did you open the help file and search for rand?


    rand(4) will give you a random number between, and including, 0 - 3.


    rand($game_variables[306]) will give you a random number between 0 and 1 less than whatever is in variable 306.


    If you want it to be between 1 and that number, you just add one at the end:


    rand($game_variables[306]) + 1
  3. It's a little difficult to explain, but what I want to focus on is the random value.

    I tried out $game_variables[307] = rand($game_variables[306]) + 1, but because I'm using the "script" button in "event commands", the box doesn't have enough space to put everything onto one line and it keeps giving me a Syntax Error because I'm writing it like this:

    $game_variables[307] = 

    rand($game_variables[306]) + 1 
  4. You shouldn't get a syntax error from that. Are you SURE that's how it's written? No typos? What is the exact error?
  5. Yes, I copy and pasted it
  6. I'm not even sure what to do anymore at this point
  7. firevenge007, please avoid double posting, as it is against the forum rules. You can review our forum rules here. Thank you.


    Just edit your last post and change it.


    What does the error message say? The FULL message.


    Can you show a screenshot of your entire event? I do not see how THAT command could cause an error. I'm wondering if you have other script calls that might be causing it. Do you have ANY custom scripts installed?
  8. Thanks, it seems like I just moved some words on the previous line and it decided to work this time. Odd, but resolved. Thanks again.
  9. This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.