Submit Your Article


 
RPG Maker

Welcome Guest ( Log In | Register )


  Games Resources RPG Maker VX RPG Maker XP Scripts Tutorials Downloads

> [Eventing]Switch & Variable Efficiency, Moving from Amateur to Pro with Switches & Variables.
Titanhex
post Jul 3 2011, 09:13 AM
Post #1


Guru of Water
Group Icon

Group: Revolutionary
Posts: 1,096
Type: None
RM Skill: Masterful
Rev Points: 5




So I was grabbing some help earlier and had to return the favor. I realized what I know is not very common amongst the RPG Maker developer base, and wanted to change that.

There's a million guides for Beginners which teach them how to use switches and variables. But there's nothing that teaches them efficiency once they've learned these. I want to change that.

I see a lot of people here turn very easy, simple systems into bloated code, but only because that's how the beginner tutorials taught them to do it.

I'm here to tell you you're making more work for yourself and complicating your systems. So, lets get into it.

What a Switch REALLY is


You've always thought a switch was an ON/OFF didn't you. Well this is only half true, and sort of limits your vision of switches. I'm here to tell you what a switch REALLY is. A switch is just a boolean. What's a boolean? In programming, a boolean is simply a TRUE/FALSE. Makes sense right, ON/OFF, TRUE/FALSE. The default for switches is always OFF/FALSE on creation.

This means instead of checking if two opposites are on, such as IS PRESENT or ISNT PRESENT, you can just check IS PRESENT = ON(TRUE, Is present) or OFF (FALSE, Not present), removing that ISNT PRESENT switch.

Oh no, but what if there's 3 values. PRESENT, NEAR, and FAR. Don't make 3 switches! Make 1 variable. 0 means PRESENT, 1 means NEAR, and 2 means FAR. (BTW Variable default on creation is 0)

Turning Switches into Variables


Many switches can be condensed into a single variable. Lets say a player must grab a key to progress, then open a door to beat a boss, and pick up a chalice in the boss room by checking piles of junk. GRAB KEY would be a switch, as would KILL BOSS and GRAB CHALICE. But you could turn all of these into 1 variable, MAIN STORY STEPS, and check if MAIN STORY STEPS is higher than a number. I do this for linear plots in a game, which just move forward like steps, in increasing order. This turns upwards of 100 switches into a single variable.

So, instead of turning on GRAB KEY, we'd either set the variable to 1 or do +1. This would replace the line [Turn on Switch, GRAB KEY = ON] with MAIN STORY STEPS =+ 1. Instead of checking if GRAB KEY = ON, we'd check if MAIN STORY STEPS is greater than or equal to 1. Instead of KILL BOSS, we'd do set MAIN STORY STEPS to 2 or +1, checking for MAIN STORY STEPS equal to or higher than 2 instead of KILL BOSS as ON.

It's a good idea to keep a comment nearby telling you what # the variable should be after adding to it.

If you have a mission that requires doing steps in a random order, like collecting items scattered in a dungeon, it's easy. If the variable is at 22 when they get to this point and they have to collect 3 items in any order, each time they find an item that variable increases by 1. Thus, by the end of the collecting, the variable will be 25. So 25 is the number we check to know they have all the items.

When do I use a Switch and when do I use a Variable?


A lot of people use switches when they can be using variables. This is what this tutorial is about. Let me convert your thinking. A switch is an ON/OFF, which if you read early is just a TRUE/FALSE. A true false is really just a 0 or a 1.

What? That makes no sense. Well, what's the difference between the switch HAS DOG = ON/OFF and the variable HAS DOG = 0/1. If we check for 0, then we do the things for HAS DOG being off. That'd be the same thing for checking for OFF. Try it yourself. You'll see.

Variables are great when going through steps. Steps can be numbered. Step 1, 2, 3. That is why variables are great for quest lines, and main story setup. They're just steps.

Variables are great for checking how many items someone has collected. But if you want to check for a specific item, you should use a switch, since they either do or dont.

And that's where switches come in. Very simple black or white, do or dont, true or false. If something needs to be defined by only two values with no sequence switches are good. Like checking for gender. If we're checking for hair color, we'd use a variable as hair color has many values.

================================================================================

Thank you for reading! Underneath I've contained snippets that can help you in other commonly misunderstood areas.

The Common Event


Common events, while nice to shrink long snippets of code, is better used for actual "common" events. Common Events means the event reoccurs more than once in the game. This can make altering and correcting code so much easier. Instead of having to change the code in every event, you just change the code once in a common event. It's especially useful when you must detect an area around an event or a wide passage for a player's approach.

It can be great for creating unity in code as well, allowing for consistency, so that if you typo once you will see it in all the code that calls it.

It's important to note that objects and spells can also trigger Common Events, which can be highly useful to activate special conditions or spells. I myself have made a "HP Damage Field" that increases in power when a boss uses a skill with common events.

Credits to Night5h4d3 for bringing this up. Common Events should also be considered as Global Events. What this means is that the event can be run anywhere in the game, or can be running indefinitely despite what map you're on. When creating an event ran system it's good to keep this in mind, as it can allow a number of things, like poison effects periodically on a character with HP reduction, screen shaking when drunk, or timers.

Debugging


Ever had a problem with your events, and no matter how much you looked at the code you couldn't figure it out? Try DEBUGGING. Put in messages near the problem that say "THIS PART IS HAPPENING" or show values like "\v[1]" so you know what value a variable is at a given time. This will help you know where exactly the problem is happening until finally you pinpoint it.

Suggested by lilcooldude69: If you're playtesting your game through the editor, F9 can open up a nifty switch and variable editor as well.

Page Numbers vs Conditional Branches


I want to get people thinking about page numbers and conditional branches a little differently too. A page number is really just a Conditional Branch. You may say, "But how?!" Well, let me show you.

Think of 3 pages, each checking if a variable is 1, 2 or 3. If it's 1, on the page we put the message "This is Page 1". Repeat for 2 and 3.

Well it's the same as if we had only 1 page, but in that page we had 3 Conditional Branches. The branch checks if the variable is 1, next branch if its 2, next if it's 3, telling us what page number it is. The order the branches are in is the same as the page order. These two events are actually the same thing.

Now where page numbers and conditional branches are different is in the bottom left corner of the page, where you see Autonomous movement, Graphic, Priority, Trigger and Options. When these are not consistent between pages, then page number is necessary.

Loops & Waits


Recently I had a problem where I was running a loop without a wait in that loop. Believe it or not, if a loop doesn't have a wait in it, it could run so rapidly it stops your game from updating the screen. This is true in scripting as well as eventing. Remember when using loops to include a wait somewhere, and/or a break point so it doesn't run infinitely. If your loop isn't working, check these two problems first. It's likely one of those.

What many don't know is that a Parallel Process and Autorun is just the Loop Event with some tweaks and GUI Setup. Parallel Process is the Loop event that lets you still use your hero, and Autorun does not. The loop is broken by moving to a new page number.

Eventing is Scripting


" What?! Ludicrous! I can't script." You might say. Well, maybe I'm exaggerating a little here. Eventing is psuedo scripting. All those events you made, they're turned into script by the game. All you're doing is taking blocks of script that do certain things and putting them in via the events menu. The engine does the hard stuff for you.

But scripting is a lot of what you've already learned. Variables, Booleans, Loops, Ifs (Conditional Branch). Those are what compose the start of scripting. If you start to master these concepts, scripting may just be up your alley.

This post has been edited by Titanhex: Aug 8 2011, 01:38 PM


__________________________
Go to the top of the page
 
+Quote Post
   
 
Start new topic
Replies
Vanit
post Aug 12 2011, 11:35 PM
Post #2


Level 15
Group Icon

Group: Revolutionary
Posts: 291
Type: Developer
RM Skill: Undisclosed




Ahh, so there it is. smile.gif I tried to look up if something like that was happening (I wasn't sure what the deal was with the abstraction), and when I found nothing I assumed it was abstracted like rm2k3.

Reading the thread it seemed like we were talking about eventing, and there was no real mention of this being done in scripts until BigEd himself explicity mentioned Ruby. But if that is infact what we're talking about I stand corrected, despite the thread not mentioning that, and the only screenshots being of an event.


__________________________
Go to the top of the page
 
+Quote Post
   

Posts in this topic
- Titanhex   [Eventing]Switch & Variable Efficiency   Jul 3 2011, 09:13 AM
- - Titanhex   Wanted to add this is my first tutorial for RPG Ma...   Jul 5 2011, 10:57 PM
- - Night5h4d3   I'd say this tutorial was very useful for the ...   Jul 6 2011, 06:19 AM
- - Titanhex   Thank you for bringing that snippet up about commo...   Jul 6 2011, 12:25 PM
- - Kazesui   QUOTE (Night5h4d3 @ Jul 6 2011, 04:19 PM)...   Jul 6 2011, 12:55 PM
- - Night5h4d3   I think I get what you mean, like have your set of...   Jul 6 2011, 01:06 PM
- - Kazesui   Your general idea is correct, apart from the last ...   Jul 6 2011, 01:14 PM
|- - BigEd781   QUOTE (Kazesui @ Jul 6 2011, 02:14 PM) Yo...   Aug 8 2011, 03:20 PM
|- - Vanit   QUOTE (BigEd781 @ Aug 9 2011, 09:20 AM) Q...   Aug 11 2011, 07:57 PM
|- - BigEd781   QUOTE (Vanit @ Aug 11 2011, 08:57 PM) I t...   Aug 12 2011, 09:00 AM
- - Night5h4d3   You're right, that does look simple, I should ...   Jul 6 2011, 01:23 PM
- - Kazesui   Well, technically, you'd need an extra variabl...   Jul 6 2011, 01:48 PM
- - lilcooldude69   on a 2nd note about using rpg maker vx, help with ...   Jul 31 2011, 12:28 AM
- - Vanit   I'm glad that this thread has touched on compo...   Jul 31 2011, 07:52 AM
- - neiljwd   Tutorial is great, aimed exactly at my current lev...   Aug 8 2011, 10:51 AM
- - Titanhex   I'll definitely look into where pictures would...   Aug 8 2011, 01:28 PM
- - Vanit   Again, you completely misunderstood. Obviously in ...   Aug 12 2011, 04:03 PM
- - Kread-EX   I think you're the one misunderstanding here, ...   Aug 12 2011, 11:26 PM
- - Kread-EX   In XP/VX, events merely invoke methods found in th...   Aug 12 2011, 11:55 PM
- - Vanit   Ahh, thanks for clearing that up for me Kread. S...   Aug 13 2011, 12:09 AM
- - Kazesui   QUOTE (BigEd781 @ Aug 9 2011, 01:20 AM) Q...   Aug 13 2011, 05:10 AM
|- - dragonheartman   QUOTE (Kazesui @ Aug 13 2011, 06:10 AM) P...   Aug 25 2011, 02:53 PM
- - lilcooldude69   LOL late posting for me on a reply of the 2 cents ...   Aug 30 2011, 01:53 AM


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 

Lo-Fi Version Time is now: 22nd May 2013 - 07:28 AM
RPG RPG Revolution is an Privacy Policy and Legal
eXTReMe Tracker