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.

Oh, The Things You Can Do With Random Variables

Random Variables[/FONT]


This tutorial is designed to show you the different things you can do with random variables. More specifically, this tutorial will show you how to create random chest items, random conversation and random battles.

Prior Knowledge Required
  • Creating a new event
  • Working with conditional branches
  • Creating a working chest
  • Creating enemies and troops
What exactly is a variable?
A variable is something that stores a number.

1. Open up your event commands and select control variables. It's the second one in the right-hand column of the first page.
2. Click on the drop down box next to Single at the top. This will open up a new window showing all of your current variables.
3. Leave the Set option marked under Operation.
4. Select an empty slot (should be blank) and type in a name in the bottom box next to Name: You should use a meaningful name. I have named mine Random. The best part is that you can use the same variable for all your random variable needs.
5. Now look under the options listed under Operand. Click on the circle next to Random. Doing this will show you two boxes, each currently filled with 0. This function allows you to determine how many items to choose from.
For example, if you select 0-3, the variable will select the commands associated with 0, 1, 2, or 3 at random. So really, 0-3 gives you four options because it counts the 0!

http://i145.photobucket.com/albums/r228 ... es/1-3.png[/IMG]
1. Create a chest event. After the turn left, turn right, turn up commands, click on Control Variables.
2. Select the random variable you had created previously.
3. Now it's time to decide how many items you would like your chest to choose from. Remember, 0-5 would give you 6 items, not 5. I want my chest event to choose from 4 items to give. So, I need to set the random operand to 0-3.
4. Next, you need to create a conditional branch for every possible item your chest can give. Since I had selected 0-3, I will have to create 4 conditional branches. Please note, if you do not create a conditional branch for one of the random operands, no commands will run since there aren't any.
  • Okay, so first, I need to create a conditional branch for when the random variable equals 0. Go ahead and create an item to give.
  • After you do that, create another conditional branch for when the random variable equals 1 under the else statement. Keep doing this until you have created items for all of your possible random variables.
What this basically does is tells the program to give item 0 if operand 0 is selected or give item 1 if operand 1 is selected and so on. And since you have it set to random, it will choose one of those items randomly.

http://i145.photobucket.com/albums/r228/yisuz/Random%20Variables/2-3.png[/IMG]
1. Create an npc event.
2. Click on control variables and select your random variable.
3. Decide how many different messages you want your npc to choose from and set the random operand accordingly. I want my npc to choose from 6 different things to say, so I've set my random operand to 0-5.
4. Create 6 different conditional branches each with different text messages.

http://i145.photobucket.com/albums/r228/yisuz/Random%20Variables/3-3.png[/IMG]
This is intended to be used with games in which the enemies are visible.
1. First, you need to have already created different enemies and troops. I have used the goblin enemy and have created 5 different troops for the goblin.
2. Click on control variables and select your random variable.
3. Decide how many troops you want your program to choose from and set the random operand accordingly. I want my program to choose from 5 different troops so I've set my random operand to 0-4.
4. Create 5 different conditional branches each with different battle processing commands for the different troops.

http://i145.photobucket.com/albums/r228/yisuz/Random%20Variables/4-3.png[/IMG]

And now, a DEMO.

I hope this tutorial has showed you some of the different things you can do with random variables. If you have another use for the random variable and would like for me to add it to this tutorial, please post a detailed reply. Thanks and enjoy.
 
Yeah, working with random variables is nice. I made a "Gambler" job class that uses dice for certain effects (like the Corsair job from FFXI, except the numbers are from 1-6).
Anything above 3 (4,5,6...) will give the said effect to all your party members, but anything below 4 would remove them.
 
I Also like using Random variables...

I?ve made a Potion Creation System using them.
The Character need some Items to use the Ability, when it?s used it creates a chance of doing the standard potion (high chance); Missing the creation (high chance); Creating a more powerfull potion of same kind (low chance) and creating a Less Powerful potion of Same kind (low chance).

I kinda like this One ^^.

So, good job with the systems! =D

[forgive my bad english, i?m brazillian]
 
Good Tut; straight to the point

I've a question that's a little off topic: How is random variables created in terms of programming? Is it the output of some algorithm?
 
Aye, that was pretty much a general question thrown out there. :eek: I'm just curious since if it is from an algorithm, then what is the input variable?
 
it uses a pseudorandom number generator. I don't know what the exact one it uses is, but normally they operate on a formula and a seed. Some pseudorandom number generators are based on time, so it could be something like this:

def random_number (max)
t = Time.now.to_f / (Time.now.to_f % Time.now.to_i)
random_seed = t * 1103515245 + 12345;
(random_seed / 65536) % max;
end

I don't know how this one in particular works, but generally, you just try to find a seed and a formula which produces an even distribution when it runs. I don't know if the one I posted is any good, but anyway, that basically how random numbers are generated in programming
 
This is a good and very easy to use tutorial.

A tip though. Those who plan on implementing random chest items should keep the possibility to save and reload in mind.
 
@alexia
Great tutorial, it's really simple and useful.

Just have to say that you can use some non-uniform probability distribution, using a 0~100 random variable, and replacing the condition for <= comparison. In this screenie, you can see how I'm making a chest with 60% of chance to gain 100 gold, and 20% to gaining a sword.
http://img241.imageshack.us/img241/7963/probchestud9.th.png[/IMG]
You just gave me an idea. I'll search for some statistical Ruby library, so we can use some distributions. I have Poisson in mind, because it's used for count data.

Good work, again.

@Crystalgate
What do you mean with this "save and reload" thing?
 
tibuda;218666 said:
@Crystalgate
What do you mean with this "save and reload" thing?

The player can save the game, open the chest and then reload the game for another try if the chest doesn't spawn what he/she wants. This is especially likely to happen if the player can save anywhere and chests tends to yield significant rewards.
 
Crystalgate;218725 said:
The player can save the game, open the chest and then reload the game for another try if the chest doesn't spawn what he/she wants. This is especially likely to happen if the player can save anywhere and chests tends to yield significant rewards.
Ah yeah, now I understand your point.
 
Crystalgate;218725 said:
The player can save the game, open the chest and then reload the game for another try if the chest doesn't spawn what he/she wants. This is especially likely to happen if the player can save anywhere and chests tends to yield significant rewards.

This can be remedied by not letting the player save everywhere, and by not placing save points near any place that would have chests that could yield high rewards.

A simple solution to a simple problem I suppose.

Thanks for this tutorial, it helped me realize the potential that variables have. ^^
 
My favorite random variable was a lightning timer I made for a thunderstorm event. It was totally awesome. (First I randomized the variable, then I looped a wait command and a variable-1 command till it reached 0, then randomized the variable again to determine which lightning type would play)
 
Sorry if I'm taking the thread in a different direction, but.
I've had that save & reload problem with a game i made before.

Is there any other way to combat it, rather than just limiting save access?
 
That would work but I would also recommend not allowing random chests to yield valuable items. (Otherwise you a forcing a save for every random chest you create.) When I create a random chest, the items all comparable in value. For example, the player might receive 2 potions, 1 potion or some gold. I wouldn't throw in a useful weapon into that chest. Chests containing special items would stand out in some way and would not give random items.
 

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