HOW TO USE SWITCHES AND VARIABLES IN RM2K3

Understanding switches and variables for RM Engines (RM2K, RM2K3, ect)

  • drxeno02
  • 12/22/2011 06:20 PM
  • 10975 views
Greetings everyone! This is DRXeno, developer of JAOD Project, and I am here to teach you about Switches & Variables for the RM Engines (RM2K, RM2K3, RMXP, ect).

-==Introduction==-

One of the first scripting concepts for beginning developers using any of the RM Engines (RM2K, RM2K3, RMXP, ect) is understanding how to use switches and variables. Although not a terribly difficult concept to understand, switches and variables are universally used inappropriately by both novice and advanced developers alike. But fret not, after reading this easy guide you’ll be developing proper, efficient, correct scripts in no time.

-==The Differences Between Switches & Variables==-

Understanding the differences between switches and variables is to understand what switches and variables are. If you are scripting an object that has up to two functions, use a switch. This definition of a switch will later be refined, but for now keep in mind the already mentioned definition. If you are scripting one or more objects that have more than two functions, use a variable. Like the name implies, a switch acts a bit like a light switch that controls lighting. A switch, like a light switch, only has two functions. You turn ON a light bulb or you turn OFF a light bulb. Some common examples where switches are used in videogames are:

1) Turn ON/OFF lights
2) Open/Close chests, doors, closets, cupboards, ect
3) Did an action occur, yes/no?

-==Example of Using a Switch==-

Start by creating one switch. The switch is (0001:Lamp)

** Setup a map event with a lamp charset. Name the event “lamp”. The event needs two pages. The first page should be the initial settings of the lamp and the second page should be the changed lamp function. For this particular example, the initial state of the lamp will be OFF and the changed function will be ON. In other words, make sure that page one of your “lamp” map event has a charset of a lamp turned OFF and page two has a charset of a lamp turned ON.
**Go to switch operations on your list of event-command options to add a switch. You already made switch (0001:Lamp) use this. Now configure the SET to ON. Repeat this process for page two, except you will configure the SET to OFF for that page.

Viola! The lamp will now turn ON and OFF whenever the event is triggered (most likely through push key)

Image example (Part 1): http://i1122.photobucket.com/albums/l532/drxeno02/1-1.jpg
Image example (Part 2): http://i1122.photobucket.com/albums/l532/drxeno02/2-1.jpg

-==DRXeno’s Challenge==-

Successfully scripting various functions is very important, but when it comes to the gaming world the functions your script and added effects go hand and hand when developing an enjoyable, interactive gaming experience. With this in mind, can you come up with a way to improve the light switch example above so that when the light is turned OFF it is darker than when the light is turned ON? Tip: Use tint screen event-command.

Image example (Part 3): http://i1122.photobucket.com/albums/l532/drxeno02/3-1.jpg
Image example (Part 4): http://i1122.photobucket.com/albums/l532/drxeno02/4-1.jpg

-==Variables & Redefined Switch Definition==-

I mentioned previously that I would redefine the definition of a switch, and here it is. A switch is used for scripting one and only one object that has up to two functions. This redefined definition of a switch is important for understanding common misuse of switches and lack of variable usage. Let’s take an example of checking to see what armor a character is wearing. For this example there are three armor types, Silver Armor, Steel Armor and Gold Armor. The common mistake that even advanced developers make is that they will create a switch for each set of armor, that is, they will create three switches each representing the three different kinds of armor, Silver Armor, Steel Armor and Gold Armor. From this point, they will intuitively code if it is the case that the character is wearing any one of these armor types the switch will be turned ON.

This is very poor scripting practices. Remember the redefined definition of a switch. A switch is used for scripting one and only one object that has up to two functions. (Silver Armor, Steel Armor and Gold Armor are three objects! Not to mention any additional armor you may want to add later) Scripting this way with switches also invites errors, because now as a developer you have to tediously track which armor is currently ON so that you can turn ON the next set of armor and, of course, don’t forget to turn OFF the first set of armor. And what happens if you do forget and now you have two sets of armor turned ON? There is a better way of programming this, and it only takes one variable.

Setup a variable and allocate a specific number to each armor set. Let Silver Armor equal 1, Steel Armor equal 2, and Gold Armor equal 3. What this does is eliminate all the back and forth and cross referencing. If this variable is set to 1, then automatically it is not set to any other number and therefore must represent Silver Armor. You do not have to go back and turn OFF any armor switches because a new armor switch was turned ON. Not only this, but you save switches, which are limited in RM2K, RM2K3 and the like!

Image example (Part 5): http://i1122.photobucket.com/albums/l532/drxeno02/5-1.jpg

For any questions or concerns contact me, I’m around here somewhere!
Name: Leonard Tatum A.K.A DRXeno
Email: LJTatum@hotmail.com
AIM: jaquio123
Official Webpage: http://leonardtatum.blogspot.com/
Gamepage: http://rpgmaker.net/games/3708/

Posts

Pages: 1
The distinction is well explained but doesn't the article limits variables to their "switch substitute" function? So much more can be done with them and I think it would be important to mention all of their possible functions to this article's target audience.

Also, using several switches for the same object might make eventcoding easier in certain situations, when having CBS sprites act for instance.
For the example used about what armor is equipped, there are conditional branches and page conditions that check what piece of equipment you are wearing.

So while I agree with the point you are making; that the work of 3 switches can be done with a single variable. It doesn't make sense to use switches or variables to emulate an existing process.

Food for thought.

Also, as a prediction of how you will respond to Avee, if you are making tutorials for beginner's and these are meant to be just the basics it might be a good idea to make that clear in the beginning. Otherwise people will keep pointing out that there are better ways to do things.
NOACCEPTANCE772
And the ability to summon the "Office Whores" to deal damage to the enemies.
553
Ugh......When I wanted this article,It never existed on the net so I had to learn through experience.Anywayz,Thnx!

MErry Christmas!
May your day be filled with joy!
Thank you NOACCEPTANCE772! I thought the same thing after writing this, and it's so funny to think back to when I was first using RM2K3 and was completely confused on how these things worked. Thank you for your comment, and Merry holidays to you too. The party is still rocking on my end! I don't know about you, but my Xmas lasts well into February and for no logical good reason either! Take care!!
Can any one send me some image of that process??
@rauzenkoh, sure! Give me a few days and I'll put together some screenshots for you. Afterall, pictures say a thousand words. Great request, and I'll be sure to add images to any future tutorials.
@rauzenkoh, ok my friend! Images have been added to the tutorial. Now it's up to you to apply the knowledge. Take care, and hopefully all confusion on when it is appropriate to use switches vs variables has been cleared up.
Pages: 1