Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] Additional Variable Operands

Kipe

Member

Additional Variable Operands

RMXP

Detailed Description:
A script that's able to set a variable to a random number between two other variables.
This is sort of like the random number command that variable's can already do. But instead of choosing two specific numbers, you'd be able to choose two variables as your random constants.
Example: Variable 1 is equal to 350, and Variable 2 is equal to 490. The script should be able to randomly pick a number between 350 and 490, whenever I call the script, and assign the randomized value to Variable 3.

Screen Shots:
k9ds1c.png


Other Scripts I am using (in order):
I'm using AWorks by vgvgf, which is uses the SDK.
I'm also using Zeriab's Anti Event Lag System
 

Atoa

Member

You can freely control variables via script calls.

$game_variables[Variable ID]

Now just make any operation you want

Eg.:
$game_variables[12] = $game_variables[15] + rand($game_variables[18] - $game_variables[15])

This will make variable 12 be equal an value between variable 15 and variable 18. (if variable 18 is higher than 15).
You cab build up your operations the way you want
 

Kipe

Member

I have tried the script you posted, and multiple variations of it, and the equation makes sense to me, but for some reason I'm getting decimal values, and the number is always 15.xxxxxxxxxxxxx. It never goes to 16, 17, or even 18.

First I talk to the guy on the right, to confirm that the values are correct, and they are.
2wrqal0.png


Next, I talk to the guy on the left, which contains the script:
Code:
$game_variables[12] = 

$game_variables[15] + rand

($game_variables[18] - 

$game_variables[15])

After I talk to the guy on the left, I go back to confirm that the value of variable 12 has been changed.
5bqx5e.png

edit: It doesn't seem to matter what variable 18 is set to. I set it to 200, and it's still a 15.xxxxxxxxxxxx number. :sad:
 
All right. First off, your main problem is that any kind of line break in the call script function counts as a new line, with no continuation from the previous line except for already established variables. The next problem you have is that it is automatically using a float, instead of an int. To solve this problem, you'll need a .to_i command on the end of the parentheses for the rand() method. Not to mention the fact that, since the rand() method "creates a random integer in the range 0 ≤ integer < max", you'll need to increase the number in the parentheses by 1 to give it the ability to generate the integer stored in the larger variable. (In this case, Variable 18) This means you'd have to do the following, using the previous example:

Code:
v15 = $game_variables[15]

v18 = $game_variables[18]

v12 = v15 + rand(v18 - v15 + 1).to_i

$game_variables[12] = v12

As for how the formula works, it's fairly simple. The rand() method will generate a random integer greater than or equal to 0, and less than the number given as a parameter. So, the formula will find the range of numbers to generate within by subtracting the larger number from the smaller and adding 1. Then, to make sure that that the number generated is actually within the specified range, you add the smaller of the numbers to the newly generated number to get the final result. With our example, you want a range from 15 to 18, which is a total of 4 integers. (15, 16, 17, and 18) Subtracting 15 from 18 results in 3, which you then add 1 to so that you can generate a range of four numbers. (0, 1, 2, and 3). Then, you add 15 to the resulting integer, which will give you one of four final results. (15, 16, 17, or 18) You can try that with any set of integers you want, and you'll find that it will always work that way, so long as variable 15 (for our example) is always less than variable 18.
 

Kipe

Member

It works :smile:

So, an integer is a number which has no fraction
and a float is a number which does have a fraction.
And to get an integer I need to do:
v15 = $game_variables[15]
instead of just:
$game_variables[15]

I don't completely understand how .to_i works, but I'm guessing that it has something to do with referring to another line, or integer?
Anyway, I'm sure none of what I just said was right, but..

Thanks!
 
Kipe":2yihe1to said:
It works :smile:

So, an integer is a number which has no fraction
and a float is a number which does have a fraction.
And to get an integer I need to do:
v15 = $game_variables[15]
instead of just:
$game_variables[15]

I don't completely understand how .to_i works, but I'm guessing that it has something to do with referring to another line, or integer?
Anyway, I'm sure none of what I just said was right, but..

Thanks!

.to_i will convert a float that you append it to into an integer. So, if $game_variables[15] is a float, and you want it to be an integer, you do the following:

Code:
$game_variables[15] = $game_variables[15].to_i

This will set Variable 15 to the integer value of itself, by truncating the decimal value

If you wanted to set the same variable to the integer value of Variable 18, you would do the following:

Code:
$game_variables[15] = $game_variables[18].to_i

Or, if you wanted to set Variable 12 to the integer value of the sum of Variables 15 and 18, you would do the following:

Code:
$game_variables[12] = ($game_variables[15] + $game_variables[18]).to_i

In the last example, it would add the two together, convert the value into an integer through truncation, and then set Variable 12 to equal that value.\

As for the code I gave you in my last post, I'll explain what I did. First, I set two temporary variables to the value of Game Variables 15 and 18. Then, I subtracted v15 from v18, used that value to generate a random number between 0 and 3, converted that to an integer with .to_i (which, in retrospect, wasn't necessary due to the fact that the rand() function only generates integers in the first place), and added it to 15. In the same line, I set v12 to equal the output of that calculation. Finally, I set Game Variable 12 to equal the value of v12.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top