# LOCAL SWITCHES IN RM2K3

### How to make switches reusable for different areas by use of variables

- Kazesui
- 07/12/2011 11:51 AM
- 3572 views

This tutorial will teach you how to store a set of switches within variables so that you can reuse the switches in question at other places without having to worry about the state of earlier ones.

It's possible to store as many as 24 independent switches within 1 variable. This can be done by treating the series of switches as a binary number which we convert to a base 10 number and with the sign as an additional switch (i.e. the 24th one).

So to do this, we'll be creating 2 common events: A switch encoder and a switch decoder, with the decoder converting a variable back to switches.

To convert a series of switches to a "normal" number given in variables, you only have to point to the variable you want to use, set it to 0 first, and add 2 to the power of n. Basically this means that if the first switch is on you add 1 (2^0), for the second you add 2 (2^1), for the third you add 4 (2^2) and so on. And that's all you need to encode a series of switches into a variable.

To decode a variable for switches, we first turn off all the switches which we're using (preferably with the range option) and then set a variable "Temp Var A" to the Start ID of the switches you're using. We then upload the value we wish to decode into a temporary variable and we then use an algorithm normally used to switch from "normal" numbers to binary ones (or other bases for that matter).

We take the value of the temporary variable and use modulo 2 on it (binary = base 2). If the modulo operation returns 1, then the current switch pointed to by "Temp Var A" is turned on. Afterwards we increase Temp Var A with 1 to make it point to the next switch. Then we divide by 2 and check if the temporary value is greater than 0. If it is we continue with the loop until it isn't anymore. At the end, it will have turned the variable into a corresponding set of switches.

And now for how to apply them:

In teleport events of your choice you set the pointer variable used in the previous events to the variable ID where you want to store the encoded value. The current switches will be stored there, and you can then set the pointer variable to the variable containing the encoded value for the next area, so that you can decode it and update the local switches to the current area.

So in short, all you have to do is to change a few values and call the two common events whenever you leave an area (with an area being able to consist of more than 1 map).

An example of use could be for treasure chests, Allowing you to copy paste treasure chests without having to worry about making more space for switches.

the picture is taken from a sample project demonstrating the principle. In the sample project only 20 switches is encoded into the variables for the sake of keeping it more tidy, since it's then possible to keep all the switches into a tab of 20.

It's possible to store as many as 24 independent switches within 1 variable. This can be done by treating the series of switches as a binary number which we convert to a base 10 number and with the sign as an additional switch (i.e. the 24th one).

So to do this, we'll be creating 2 common events: A switch encoder and a switch decoder, with the decoder converting a variable back to switches.

To convert a series of switches to a "normal" number given in variables, you only have to point to the variable you want to use, set it to 0 first, and add 2 to the power of n. Basically this means that if the first switch is on you add 1 (2^0), for the second you add 2 (2^1), for the third you add 4 (2^2) and so on. And that's all you need to encode a series of switches into a variable.

To decode a variable for switches, we first turn off all the switches which we're using (preferably with the range option) and then set a variable "Temp Var A" to the Start ID of the switches you're using. We then upload the value we wish to decode into a temporary variable and we then use an algorithm normally used to switch from "normal" numbers to binary ones (or other bases for that matter).

We take the value of the temporary variable and use modulo 2 on it (binary = base 2). If the modulo operation returns 1, then the current switch pointed to by "Temp Var A" is turned on. Afterwards we increase Temp Var A with 1 to make it point to the next switch. Then we divide by 2 and check if the temporary value is greater than 0. If it is we continue with the loop until it isn't anymore. At the end, it will have turned the variable into a corresponding set of switches.

And now for how to apply them:

In teleport events of your choice you set the pointer variable used in the previous events to the variable ID where you want to store the encoded value. The current switches will be stored there, and you can then set the pointer variable to the variable containing the encoded value for the next area, so that you can decode it and update the local switches to the current area.

So in short, all you have to do is to change a few values and call the two common events whenever you leave an area (with an area being able to consist of more than 1 map).

An example of use could be for treasure chests, Allowing you to copy paste treasure chests without having to worry about making more space for switches.

the picture is taken from a sample project demonstrating the principle. In the sample project only 20 switches is encoded into the variables for the sake of keeping it more tidy, since it's then possible to keep all the switches into a tab of 20.

**Sample project**

## Posts

Pages:
1

Question, does the text of this tutorial explain everything shown in the images? or is there some math going on that you would need to understand to grasp WHY this works? Should I be able to understand it just by reading the text? hahah

... But that would probably confuse you even more.

I could give it another go though.

The binary number system consists of 1 and 0, and since it only has 1 and 0, it's digits will increase faster than regular numbers (referred to as base 10 numbers, because there are 10 numbers before each new digit).

Counting in the binary system would work like this: 1, 10, 11, 100

where 100 would equal 4 in base 10.

To convert from a binary number you multiply each digit with 2 to the power of the position of that digit starting at 0 from the right and moving towards the left towards the most significant digit

for the binary number 100 this gives: 1*2^2 + 0*2^1 + 0*2^0 = 4 + 0 + 0 = 4

For going the other way you take the base 10 number in question and divide it by 2. If there's a remainder during that division then that remainder should be placed at the first position, and if the number is bigger than 1 you keep dividing until you end up with a 1, which goes at the last position you reach. taking 4 as an example we get

4/2 = 2 (+ no remainder) = 0

2/2 = 1 (+ no remainder) = 0

1/2 = 0 (+ 1 remainder) = 1

which gives the binary number 100.

The reason why we'd want to convert to the binary system for storing as many switches as possible within a variable is because we could use each digit to tell if a switch is on or off by telling whether it's 1 or 0.

And this is basically what the code up there is doing, just simplified a bit.

since you wouldn't multiply by anything than 1, you can leave that step out of the algorithm when dealing with binary numbers and just pre calculate the 2 to the power of switch position numbers, which is basically just a matter of doubling the last number for each step. And the second event is doing pretty much the same as described above

So hopefully this helped you a little... or you could just shove it off as magic which works

I definitely need to try to work this into my game. Thanks!

Pages:
1