RMXP Switches + Local Switches by Dubealex

0 Members and 1 Guest are viewing this topic.

Level 90

This script was made by Dubealex at www.dubealex.com

Quote from: Dubealex

RMXP Switches & Local Switches Demystified

(1) Introduction

This class will teach you absolutely everything that you need to know to understand and efficiently uses RMXP Switches and Local Switches.

(2) Introduction to Switches in general[/b][/color]

What is a switch ? In RPG Maker XP, a switch is a kind of "trigger" that is used to set something ON or OFF depending on the situation. To start up clear on something you must know, I must say a switch is in fact a type of variable. In RPG Maker XP point & click environment (the Event Commands), you will not really notice that; as you can just set a switch ON or OFF.

But a switch, in the background, is seen as a normal variable. Here an extract of my variable class:

- Definition of "Variable": able or liable to change.

Variables are used in every games and softwares; they store information, information that is then used by the system. We could say that a variable is a box in wich we want to put something for later use. What kind of information ? This can be divided into 4 major groups:

1 - Numbers
2 - Text
3 - Boolean
4 - Arrays

"Boolean" variable are triggers, they can only be TRUE or FALSE. Meaning they can contains only 1 value, and you have only the choices between TRUE or FALSE. In RPG Maker XP, a "Switch" is actually a "Boolean" variable; it can be wether ON or OFF.

So remember that, since we will talk about switches in Ruby in later chapters. Now let's continue in for the simple stuff:

What is a "Local Switch" then ? A normal switch can be set ON or OFF from any events you created on the map, and a Local Switch is a switch available to use only in one particular event. Every event that you create in RPG Maker XP will have 4 local switches; we could say that those local switches are actually "Private Switches" that can be modified only from within the events that they exist in. Those "Local Switches" are named A, B, C and D. (You cannot change their names like you changes normal switch.)

So, in brief, remember this:

Normal Switch:
A normal switch is available for every events in your game, and can be modified from any events in your game. This is a "Global Switch".

Local Switch:
A local switch exist only within a particular events, and each events have their own private switches, 4 to be exact.

Here's a diagram about switches in RMXP (Wich explains the structure of switches and local switches in your project):


So, as you can see, in RPG Maker, every normal switches are available to every events in your game. This means that if you set a precondition on 100 events on 100 different maps, and you choose the same switch for all of them, setting this switch ON will activate all those events. You can also see that local switches are confined to only one event, and that every event have its own "pack" of local switches.

(3) RMXP Switches System[/b][/color]


Switches in RPG Maker XP are recognized by the system only by their ID numbers, not by their names. Meaning that you could have 2 switches having the same name, but never the same ID. When naming a switch, always try your best to name it with something that would reflect it's usage. I.E: If you are using a switch to end a story that was auto-started by an event, you could call it "Story-End".

1) Specific Switch
Using this option will allow you to choose one switch from your database with the selector to the right. (If you want more switches, click on the button "Array Size" to the bottom left of the window that appears when you choose a switch, and enter the number you want. Maximum is 5000.)

2) Switch Range
This option will allow you to modify many switches at the same time. When you choose this, you must enter the switches ID in the selection box to the right. Meaning that if you write switch ID 15 in the first and switch ID 25 in the second, the operation you will do will affect switches 15 to 25 included.

3) Operation
This is pretty simple, as we have said earlier, a switch can only be ON or OFF, so you choose here if you want to turn it ON or OFF.

(4) RMXP Local Switches System[/b][/color]


1) Local Switch
You can choose wich local switch you want to modify, as we said earlier, every event have their own 4 local switches known as A, B, C and D.

2) Operation
This is pretty simple, as we have said earlier, a switch can only be ON or OFF, so you choose here if you want to turn it ON or OFF.

(5) How to use switches & local switches[/color]

In this chapter we will learn how to manipulate switches, and we will also see some example on basic stuff like chest and events activation. First thing first, you must be able to activate an event with a switch; once you understood how this works, you will understand pretty much anything else related to switches.

A) Event Activation[/color]

First of all, we will name our 2 events so we know wich is wich: The event that will activate the other will be named "Caller" and the one who will be activated by "Caller" will be named "Sleeper". (Pretty straigh-forward...)

In our case, we will create the event "Sleeper" first. I won't do a complete Event tutorial here, but I will explain what you must know to understand switches now:


1) Name
This is where you write the name of the event; it is only relevant to you, RPG Maker will never uses this information. So our first event is named "Sleeper."

2) Preconditions
That section covers the preconditions. This means that you can choose here the conditions needed for this event page to be "active". For the present situation, we will talk about the Switch precondition. If you choose a switch here, it will have to be ON in order for the events to be active.

3) Event Commands
That section will contains all the actions the event will do when activated. In our situation, the first page is empty because by default, we want "Sleeper" to be inactive and "Sleep".

Event Pages:
As seen in the picture above, we used 2 page for "Sleeper". You can create a maximum of 99 pages per events, each pages being a different variation of the same event. (More about that in later chapters.) You can then set different "Preconditions" and "Event Commands" in many different pages.

So our first page is empty, and our second will contains the actions we want "Sleeper" to do when activated. Look:


As you can see, we simply created a second page, added a message in the Event Commands, and set the preconditions to a switch that we named "Wake-Up". (Because its purpose is to wake sleeper up.)

By default, every switches are OFF, so our event "Sleeper" will do nothing until our switch "Wake-Up" is set ON. Now is the time to create our event "Caller". Create a new event, and in the event commands, do a Switch Operation and set the switch "Wake-Up" ON.

Now, if you test play that game, if you go talk to "Sleeper", nothing happen. Go talk to "Caller", the switch "Wake-up" is now ON -- Wich means "Sleeper" has been activated, if you go talk to him, he will tell you something.

B) Creating a treasure chest[/color]

By creating a treasure chest, you will see how to use local switches. Local switches are very useful for small event like doors, chest and interactive NPC who tells different things. They are useful because it saves normal switches for "real" events; just imagine having 500 treasure chest in your game, and imagine you would need 500 switches for them... Who could remember where his chest #54 was ? Local Switches are an efficient way of doing those kind of event that repeats themselves a lot in your game.

We don't need a picture for this, since you can clearly see what we will use in the picture used above. In the first page, we will make the treasure chest opens, give 100 gold pieces to the player, and activate a local switch that will prevent the player from opening the chest another time.

To make the chest opens, I uses "Move Events"... Because a chest is in a character set in the default resources of RPG Maker XP. The move event will look like this:


As you can see, this event must be set as "Lock Facing", this is because RPG Maker will, by default, turn the graphic of an event toward the player. If you talk to the treasure chest from the left, the chest will try to face you, and will thus face left. We do not want this to happen because it would change the way the chest look like, so we checked the option "Lock Facing".

The move event contains an SE for the chest that opens, and we set "Lock Facing" to OFF while we changes the graphics of the chest; otherwise it would not look like its opening. Then, after the move event, we must set "Lock Facing" back ON.

The "Wait 10 frames" will make a small pause between the end of the move event and the message "Received 100 GP !". (10 frames = 1 sec)

We played another SE that simulate a "success" song because you gained something, we used a message event to tell the player what he gained, and we did a "Change Money" operation to give the player 100 GP. Lastly, we set the Local Switch A to ON.

On our second page, we have set the graphic to the one of an opened chest, and we have set the preconditions to the Local Switch A. Then, the player won't be able to open it again.

(6) Events Page Priority[/color]

In RPG Maker, every events can have up to 99 pages, each being able to have different event commands and preconditions. There is a priority order in those pages. Here's how it works:

The higher page number have priority on all those below them. Meaning that Page #2 have priority on Page #1, and that Page #3 have priority on Page #1 and 2.

All this means that if you create an event with 4 pages, and set the same switch precondition on all of the pages, RPG Maker will uses the highest page number available. It also means that if you used switch #1 as precondition on page #1, and that you used switch #2 as precondition on page #2, if both switches are ON, only page #2 will be active as it have priority on the other page below.

(7) Switches within Ruby Script[/color]

You can set your switches ON and OFF within the Script Editor or in a Call Script using the following syntax:
Code: [Select]
$game_switches[id#] = flag value  

Where [ID#] represent the ID of the switch you want to manipulate, and flag value is wether "true" or "false" -- ON is "true" and OFF is "false". So, it would look like this:

Code: [Select]
$game_switches[1] = true  

Will set the game switch ID# 1 ON. You can found more infos about those syntax in my RGSS Reference.

(8) Local Switches within Ruby Script [/color]

You can also set any local switches of any events on any map ON or OFF from any event on any map using the following syntax in a call script, or the script editor:

Code: [Select]
$game_self_switches={[MAP-ID, EVENT-ID ,"LOCAL-SWITCH"] =>flag}
$game_map.need_refresh = true

Where MAP-ID is the ID of the map where the event is, EVENT-ID is the actual event ID, and LOCAL-SWITCH is wether A, B, C or D. flag is wether true or false, as for normal switches.

So, as example, if you want to set the local switch A for events ID#12 on map ID# 2 to ON, you would write this in a call script:

Code: [Select]
$game_self_switches={[2, 12 ,"A"] =>true}
$game_map.need_refresh = true

The line "$game_map.need_refresh = true" is there to ensure that the map is refreshed after changing the value in the hash (the local switch) because if we do not, the changes made to a local switch from an event on the current map would not show instantly.

Have fun with that ! Now you have all the power in your hands, so go create a good game !

(9) Conclusion [/color]

Feel free to post any questions or comments !

Also, this could transfer over to 2k, 2k3 if you don't use local switches or such.

Although I have not read this tutorial I have heard it was really good, and I will answer any questions or concerns... until I forget this exists.

? ? ? ? ? ? ? ? ? The nice kind of alien~
Level 92
Martian - Occasionally kind
About this:
Quote from: Dubealex
Local Switches Syntax & Example:
Code: [Select]
$game_self_switches={[MAP-ID, EVENT-ID ,"LOCAL-SWITCH"] =>flag}
$game_map.need_refresh = true

Doing this would clear any other local switch as you're setting the $game_self_switches to be that one defined switch thereby deleting any other self-switch present.
If you only want to change or add, not remove anything else, it should be like this:
Code: [Select]
$game_self_switches[[MAP-ID, EVENT-ID, "LOCAL-SWITCH"]] = flag value
$game_map.need_refresh = true

Level 90
Heh I never noticed this seeing as I never finished my learning on RGSS

Anyway, it would be cool if this was moved to the Tutorial Databases =/

Level 91
no it wouldn't be cause:
a. there is a switching tut there
b. you are a moron not to understand switches in the first place
c. the quality of tut db is.. ya know what? everything there smell so bad that this fits right in
and yet i am not sure if adding more junk is a good thing, i'd think the crap should simply be removed
holy shit my sig was big!

Level 90
Heh I understand switches.  I learned them like a week after starting with RPG Maker when my friend showed me his small game.  It also taught me how to make NPCs... heh.

And sorry, I'm blind =/.  Besides having different versions of certain tutorials is always helpful.  Like me, I can't really learn anything from reading... i need to see it visually, whereas other people will learn by reading.