Gw Temp

Menu

Tutorial - 'Switch Multiplexing' by Rast

An item about RPGMaker 2000 posted on

Blurb

How to put lots of switches on a single variable (very useful for CMS/CBS's)

Body

This tutorial explains how to pile lots of switches onto a single variable, something I call switch multiplexing. This is not something most games will need, but if you're using any sort of a custom system that pushes lots of data around in switch form, managing it in this manner makes it much easier to deal with, as it reduces switch clutter as well as allowing you to access your switches using pointer variables rather painlessly. (See my tut on Pointers for more information on this)

To do this, all you need to do is set aside a variable and a block of 17 switches. Sixteen of these switches will be the actual switch data, and the 17th switch and variable are used for a common event.

For your common event, your game will interface it as follows:

1) If it wants to READ switch data out of a variable, put your value in the variable and set the 17th switch to ON. The event will dump out the multiplexed switch data into the sixteen switches.

2) If you want to WRITE switch data to the variable, put your switches into switches 1-16 and set switch 17 to OFF. The event will multiplex the sixteen switches into the variable.

Now for your common event, set it up like this:

FORK (Switch 17 is on) {

We read the switches out of the variable. If you need to preserve the contents of the variable, just copy it into a temporary variable and use it for the purposes of this event.

FORK (Variable ABOVE 32768) {
Set switch 16
Variable - 32768
}

FORK (variable ABOVE 16384) {
Set switch 15
Variable - 16384
}

FORK (variable AVOVE 8192) {
Set switch 14
Variable - 8192
}

FORK (variable ABOVE 4096) {
Set switch 13
Variable - 4096
}

FORK (variable ABOVE 2048) {
Set switch 12
Variable - 2048
}

FORK (variable ABOVE 1024) {
Set switch 11
Variable - 1024
}

FORK (variable ABOVE 512) {
Set switch 10
variable - 512
}

FORK (variable ABOVE 256) {
Set switch 9
Variable - 256
}

FORK (variable ABOVE 128) {
Set switch 8
Variable - 128
}

FORK (variable ABOVE 64) {
Set switch 7
Variable - 64
}

FORK (variable ABOVE 32) {
Set switch 6
Variable - 32
}

FORK (variable ABOVE 16)
Set switch 5
Variable - 16
}

FORK (variable ABOVE 8)
Set switch 4
Variable - 8
}

FORK (variable ABOVE 4) {
Set switch 3
Variable - 4
}

FORK (variable ABOVE 2) {
Set switch 2
Variable - 2
}

FORK (variable ABOVE 1) {
Set switch 1
Variable - 1
}

} ELSE {

We encode the switches into the variable. The variable value can then be stored for later use

Set variable = 0

FORK (switch 1 is ON) {
variable + 1
}

FORK (switch 2 is ON) {
variable + 2
}

FORK (switch 3 is ON) {
variable + 4
}

FORK (switch 4 is ON) {
variable + 8
}

FORK (switch 5 is ON) {
variable + 16
}

FORK (switch 6 is ON) {
variable + 32
}

FORK (switch 7 is ON) {
variable + 64
}

FORK (switch 8 is ON) {
variable + 128
}

FORK (switch 9 is ON) {
variable + 256
}

FORK (switch 10 is ON) {
variable + 512
}

FORK (switch 11 is ON) {
variable + 1024
}

FORK (switch 12 is ON) {
variable + 2048
}

FORK (switch 13 is ON) {
variable + 4096
}

FORK (switch 14 is ON) {
variable + 8192
}

FORK (switch 15 is ON) {
variable + 16384
}

FORK (switch 16 is ON) {
variable + 32768
}

}

As you can see, this event functions by simply encoding the switches in a binary fashion onto a variable.

As I said before, this type of encoding is most useful when combined with pointers and data structures, as this allows you maximize variable efficency when using variables to represent switches. As some real-life examples, character/monster status information in Dragon Destiny II is stored in this manner, as well as all the switches that describe the ways that items and skills can be used