Skip to content

Switches and variables

Al'rind edited this page Jul 13, 2015 · 3 revisions

A lighter syntax

Variables and switches are commonly used in RPGMaker's game creatin. Usually, software's windows allows to assign variables to values (for instance, X and Y coordinates of a picture or the hero's teleportation's coordinates). However, their use in a script call is quite long to write down. In order to call a variable and a switches, the standard syntax is the following:

  • $game_variables[id] to retrieve the variable corresponding to the given id
  • $game_switches[id] to retrieve the switch corresponding to the given id

The first goal of this script is to simplify variables and switches' access. Now, you will only have to use the letters V (for Variables) and S (for Switches). Their syntax is the following::

  • V[id] to retrieve the variable corresponding to the given id
  • S[id] to retrieve the switch corresponding to the given id

Variables

Assigning a value to a variable is then easier to write and more readable. For instance, assigning the value 134 to the variable n°98 (in a script's call or in script) would be done like that: V[98] = 134 Given the fact that it is possible to use this syntax everywhere, it is feasible to do this: V[V[12]] = V[1]+V[2]*V[3] Which means that the value of the variable n°12 will be the ID of the variable that we will modify, and that we will assign to it the value of variable n°1 plus the product of the variable n°2 and n°3. Besides + and *, there are other operators. Here is a list (potentially incomplete) of available operators:

Arithmetic operators

  • + Addition, for instance: V[1] = V[2] + V[3]
  • - Subtraction, for instance: V[5] = V[3] - 3
  • * Multiplication, for instance: V[10] = 7 * V[9]
  • / Euclidian division (5/2 = 2), for instance: V[8] = 2/4 + (V[11] / 3)
  • ** Exponentiation (3 ** 2 = 9), for instance: V[6] = V[1] ** V[2]
  • % Modulo, the remainder of the euclidian division, for instance: V[1] = V[2] % 4

Syntax's simplification

There is a little syntaxic shortcut in order to compress peculiar expressions:

  • `x += y` : `x = x + y`
    
  • `x -= y` : `x = x - y`
    
  • `x /= y` : `x = x / y`
    
  • `x *= y` : `x = x * y`
    
  • `x %= y` : `x = x % y`
    

This notation can obviously be much more complex. For instance: V[1] += (10 * (V[2] ** 2)). As for classic arithmetic operations, parenthesis allow you change the operations' priorities.

Even if this extension might seem unnecessary, it will save you a lot of time. We are inviting you to do the previous expression (V[1] += (10 * (V[2] ** 2))) in the classic way of Event-Making. You will see that you will have to type more lines and make you loose time.

Switches

As for variables, switches are get-at-able via a shortcut: S[id]. We are only able to give two values to switches: true (meaning that the switch is activated) and false (meaning that the switch is disabled). Here are some examples:

  • S[1] = true : activates swtich n°1
  • S[2] = false : deactivates switch n°2
  • S[3] = S[1] : assign the value of switch n°1 to the switch n°3

Logical operators

As (numeric) variables have arithmetic operations, switches have logical operators. We can then combine switches like that:

  • S[1] or S[2], that could also been written as S[1] || S[2], returns true if one of these switches is activated.
  • S[1] and S[2], that could also been written as S[1] && S[2], returns true only if the two switches are activated.
  • !S[1], that could also been written as not S[1], returns the opposite expression. If S[1] equals true, it will return false and conversely.

Like arithmetic operators, logical operators can be combinated. For instance, an expression like: (S[1] and S[2]) or (not S[3]) (strictly equivalent to (S[1] && S[2]) || (!S[3])) is perfectly valid.

Building up values for switches

In RPGMaker, we are pushed to only "activate" and "deactivate" switches. To activate (or deactivate) one, we usually use a condition. For instance: "If the variable n°5 is greater than 6, then I activate the switch n°3, otherwise I deactivate it". That's a bit verbose and long to type, whereas the expression "the variable n°5 is greather than 6" is already a boolean expression which can be equals to true or false. Therefore, we can simplify this kind of behaviour with : S[3] = variable n°5 is greater than 6 ?. We will cover, in this section, how to produce valid values for a switch. Combined with logical operators, you will see that you can design an incredible amount of cases and thus, saving conditions !

Comparison operators

In Ruby, there are operators dedicated to compare values. Let's cover some of them:

  • x == y : returns true if x is equal to y; false otherwise
  • x != y : returns true if x is not equal to y; false otherwise (so x != y is the same as !(x == y))
  • x > y : returns true if x is greater than y; false otherwise
  • x < y : returns true if x is less than y; false otherwise
  • x >= y : returns true if x is greater than or equal to y; false otherwise
  • x <= y : returns true if x is less than or equal to y; false otherwise

Through these operators, it is possible to compose complexe expressions. For instance, we want to activate a switch if the variable n°3 is greater than the variable n°12, and that the sum of the variable n°7 and n°8 is an even number, or if the switch is already activated. This is an expression that check the given condition: S[1] = S[1] or ((V[13] > V[12]) and (V[7] + V[8])%2 == 0). As you can see, parenthesis are the only way to express priorities and thus allow us to write complexe expressions.

Note on conditions

A condition (in Ruby or in an event) is processed only if its input is equal to true. Otherwise, it directly goes in the else part. So, as a switch is always equals to true or false, the condition to check if a switch is activated can be resumed as:

If Script : S[1] then
     code to execute if the switch is activated
Else
     code to execute if the switch is disabled
End condition

Everything that has been covered previously is still relevant. For instance, let's express the following condition: If Script : V[1] == 10 and (V[3] + 4 > V[10]) and (not S[5]) Which could be translated as "If the variable n°1 is equal to 10, and the sum of the variable n°3 and 4 is greater than the variable n°10, and if the switch n°5 is disabled; then we execute something".

Pointers

We can specify any valid Ruby expression which returns an integer as the ID of the variable or switch to use. For instance, S[1+2+3] is equivalent to S[6], it points the switch n°6; or S[V[10]] which points the switch whose ID is equal to the value of the variable n°10; or V[(V[10]+V[11])*3] which points the variable whose ID is three times the sum of the variable n°10 and n°11. When we will see commands (in a following section), you will see that some of them return integers. It will be quite eccentric to use them as variable's or switche's ID but it will work. For instance, the command mouse_x (which returns the mouse's X position on screen) could be use like that: V[mouse_x]; but we do not have any cases in mind where such a way of doing could be useful !

Additionnal jutsus

Let's see some peculiar technics that can save you some time in specific cases. These are not "key technics" but they provide a way to better understand some points about RME's expressivity.

  • V[1], V[2] = V[2], V[1] swap values contained in variables n°1 and n°2.
  • S[7..29] = true activate from the seventh switch to the twenty-nineth one.
Clone this wiki locally