What is a variable?

From OHRRPGCE-Wiki
Jump to navigation Jump to search

Concept[edit]

A variable (in plotscripting) is a numerical value that varies (in other languages, variables do not have to be numerical). You can store values in them, and then use them later in calculations, or to pass a value to a function.

When you write in your script file:

global variable(3, var)

you are saying 'the name of global variable 3 is var'. So, when you write:

var := 100

or

setvariable(var, 100)

You are assigning the number 100 into the global variable named var, which you will use to refer to it later. The text 'var' is not the variable -- it is the name of the variable.

In OHRRPGCE, variables are stored in particular (constant) memory locations; you can change the content, but not the location.

When you use a variable name in an expression, it means 'the value stored in this variable'. Like this:

var2 := var + 5

The value of var is currently 100. So this is like saying 'var2 := 100 + 5' (but it would vary depending on what value var held, naturally.)

Constants[edit]

By contrast a constant is something you can't change, -- for instance, the name 'var' -- once you define it, it is the same forever. The name itself is a constant; the value it indirectly refers to can vary.

You can also create constants for your scripts, defining a constant numerical value to a name, like this:

define constant (10000, ten thousand)

Now you can use it in other expressions like a variable, but you can not change it. The follow would not be allowed:

ten thousand := 10001

But you could write

var2 := ten thousand + 5

So if you can't change constants, why would you want to use them? The answer is that they make it easier to keep track of what numbers mean and to change them if you have to. Let's say you have fifty scripts that revolve around a battle against three Cleaning Fluid Thieves (formation number 10) and your hero's speech to the Great Contact Lens (text box number 10), and then you decide you instead want the hero to keep fighting a single Eye Doctor (formation number 37) instead. If you've just typed out 10 every time, now you have to go through and figure out which 10s you need to turn into 37s and which ones need to stay 10s. On the other hand, if you instead defined constants "lens speech" and "annoying repetitive battle" as both equal to 10, then you can just go up and change "define constant (10, annoying repetitive battle)" to "define constant (37, annoying repetitive battle)". Not only that, but if you typo 10 as 01 somewhere in there, HSPEAK won't catch it and you'll end up rummaging through your HSS trying to find out why your plotscripts aren't working correctly, whereas HSPEAK will notice if you type "lnes speech" and will point it out for you. These sorts of things happen more often than you'd think.

Uses of variables[edit]

Variables are indeed extremely useful. Without them, your script would be unable to remember anything.

Suppose you want to know where the hero stands. You can create two global variables

global variable(1, hx)
global variable(2, hy)
global variable(3, map)

For the hero x and y values, and then assign values to them in a script.

hx := hero x (me)
hy := hero y (me)
map := current map

Now if you want to move the hero back to that point later, you can 'pass' the variable as a value to some functions.

teleport to map (map, hx, hy)

More uses[edit]

You can do a lot of things with variables. Lets say you want it to pick a variable randomly.

variable(prize)
setvariable(prize,random(1,3))

That would chose a number from 1 to 3.But now we want to make it do something.

variable(prize)
setvariable(number,random(1,3))

if (prize==1) then (get money(5))#that would make you get 5 money
if (prize==2) then (get item (17,1))# you would get item 17 1 times
if (prize==3) then (get item (7,3))#you would get item 7 3 times

Using that script, the player will either get 5 money, item 17 1 time, or get item 7 3 times. Then you could make a lottery by making the player pay. Or you could use tags inside the scripts. a lot of things can be done with variables.

More on how to use variables[edit]

If you're having trouble with the concept of variables, check out Mike Caron's series of Flash movies explaining all about variables:

See Also[edit]