Home
Sitemap
Site News
Downloads
FAQ
Contact
• Basic Tutorials •
Maps
The Database
Events
Graphic Resources
• More Advanced Tutorials •
Maps
Events
The Database
Event Systems
• RGSS •
Script Fixes
Script Systems
• Other •
Using RMXP
Mini Tutorials
Miscellaneous
• Reference •
Reference

In this tutorial you will learn...

• How to turn on switches…
• … and set variables automatically at a new game
• How to set switches automatically at game load


Hi there, and welcome to my fourth script fix tutorial! In this tutorial, I hope to show you how to go about setting switches to come on automatically when the player starts a new game, or whenever they load their file. So, let's get going! Open your project up. Now, you need to make sure that you know the number of the switch that you want to set. I am going to use my Random Battles switch for my random battle system, and I know that it is number 13.

Once you've done that, head into the script editor, and in the script list on the left-hand side, find the script entitled Scene_Title. This is the script that handles all the processing from the title screen - I.e. starting a new game, which is where we want to make some modifications. So, scroll down until you see where the script says * Command: New Game. It should look like this…



Once you've found this, all we need to do is add in an extra line of Ruby script below where it says $scene = Scene_Map.new. Can you find that? Okay, underneath we're going to add this:

CODE:
$game_switches[13] = true


Game switches can be accessed from a script using that syntax. You will notice that rather than having the switch 'on' as such, it is set to true - this is because we are using the scripts within proper Ruby syntax now and Ruby doesn't think in ons and offs - or rather it does, but it calls them true and false. Also, make sure that you subsitute the number for the value that you want to use - it's the number of the switch. Your code screen should now look something like the following…



I've added a little comment so that if I come back to this, I'll know what I've done. This is a good habit to get into; it prevents confusion later on. So now, whenever a new game is started, this code will execute and switch number 13 will be turned on. Also, when the player saves the game, the switch's mode will be saved within the game and loaded when the game is continued. Handy, eh? And you can also manage this switch using events, which will also be saved and remain unchanged.

However, before we move on, there is one more really important thing that we need to add. It's a little confusing to explain why we need this, but if we don't add it, our switches won't work with Common Events, which is really what we want them for. So, underneath where we have added our switch code, we're going to add this:

CODE:
$game_map.refresh


Alright? Make sure that this goes underneath all your automatic switches, otherwise it won't work! Now your code page should look something like this…



Okay, now that we've done switches, how about setting variables to a certain value when the player starts a new game? Well, you can probably guess what the code will look like. Say I want to change variable number 14 to a value of five when a new game is started. Underneath our switch code, we can add the following…

CODE:
$game_variables[14] = 5


Make sure that you add this above the refresh command! That will set the variable number 14 to the value of five when a new game is started.

Note: If you would like to know why we need to add the refresh command, it's because when we start a new game, the Game_Map.update method doesn't contain code that refreshes the common events, it just updates them. Likewise, the update method for Common Events doesn't reset the trigger, it just updates what is already going on. With the refresh method, the common event checks if a parallel trigger is needed, and if it is, creates a new interpreter object to start running the event commands. So, effectively, we're checking if there is a parallel switch and creating a new interpreter for the common event through calling the common event refresh method through the Game_Map.refresh method. Phew! Confusing, huh?

Your code screen should now look something like this…



As with the switch, this value will now function as normal; the value will be saved as normal and you can change the value within the game, and it won't change when the game is loaded.

Right, now we know how to set a switch and a variable when a new game is started, how do we set a switch or a variable when a game is loaded? In the script editor, scroll in the left-hand script list until you find a script called Scene_Load. Click on this, and then scroll down right to the bottom, where you will see a comment saying # Refresh party members. Underneath will be one line of code, and then the method and the class end. It is underneath this line of code that we need to add in our switch and / or variable code, so it looks like the following…



See how it works? And that's it! But wait, I hear you say, we didn't put the refresh method in! That is true - but, because we are loading a game rather than starting a new one, we don't need to refresh the map to get the common events to work with these switches; refreshing the map is part of the loading process. It's a bit tricky to explain, but it's just one of those script things!

Those switches and variables will be set whenever the player loads a game, no matter which game they load. Those switches and variables can be changed in-game using events, but when the game is saved and loaded, they will go back to being their original value. Also, bear in mind that these switches are not set when there is a new game; only when you load a game. So, if you want to have a switch or variable that sets when you start a new game and then every time you load, you need to insert it into both scripts within the editor.

And that's it for my automatic switches tutorial! Hopefully you've learnt about how to make switches and variables change automatically at new game startup or existing game load. If you're having trouble getting this to work, double-check that you've added the Game_Map.refresh method underneath the new game switches. If you're still having trouble, head over to my Contact page and I'll do my very best to help you.

See you in the next tutorial!

:: << Previous Tutorial ::      :: Home ::      :: Next Tutorial >> ::

RPG Maker XP © 2005 Enterbrain Inc.           © 2004 Xiph. Org Foundation           Ruby © 1993 - 2003 Yukihiro Matsumoto
This site, including images and textual content, were created by and belong to me.
© 2006 - 2011 All rights reserved.

This website and tutorials have all been written in my free time (what there is of it!), and hosting can be rather costly, so any donations are very gratefully received - no matter how small - even if you can spare 50p I would be grateful!