Tips on Working with Game_System

Tips on Working with Game_System

This topic aims to help other people who wants to do a little bit of scripting but is lost on ideas how to. This tutorial gives you tips how to do some basic scripts using Game_System, which are both defaults of the RPG Maker VX Ace.

[IMG]http://i.imgur.com/n6fvBXt.png[/IMG]​

Working with Game_System

What is the Game_System for anyway? According to the header of the script, this is what it does:

This class handles system data. It saves the disable state of saving and menus. Instances of this class are referenced by $game_system.

See how many things you can do with this script? Right now, I know there are many ideas flowing out in your mind already. However, we are in question how are we going to do it. This is what we are going to work on!

In this tutorial, we will be working a lot with attribute accessors (attr_accessors). So, what are attribute accessors? Attribute Accessors does both the job of the Attribute Reader and Attribute Writer. So what does the reader and writer do?

The attr_reader method does just like what it sounds like it will do. It takes any number of symbol parameters and, for each parameter, defines a "getter" method that returns the instance variable of the same name. 

Similarly, the attr_writer method defines a "setter" method for each symbol passed to it. Note that the equals sign need not be part of the symbol, only the attribute name.

Let us make some examples how we are going to make some attribute accessors on Game_System, define them and access them, shall we? First, on your code, do this:

class Game_System attr_accessor :fruit attr_accessor :fruit_color attr_accessor :fruit_tasteendWe created three accessors namely the fruit, fruit color and the fruit taste. There are ways in order to define their value. We can do it this way:

class Game_System attr_accessor :fruit attr_accessor :fruit_color attr_accessor :fruit_taste alias game_system_define_accessors_initialize initialize def initialize game_system_define_accessors_initialize @fruit = nil @fruit_color = nil @fruit_taste = nil end endSo, our initial value for the fruit, fruit color and taste are nil. Now, you'll surely ask me, how can we add values to it then? We should create methods that does so, where we are going to pass variables to it and make it work:

To do that, we have to add a little bit of methods on our script, namely:

class Game_System attr_accessor :fruit attr_accessor :fruit_color attr_accessor :fruit_taste alias game_system_define_accessors_initialize initialize def initialize game_system_define_accessors_initialize @fruit = nil @fruit_color = nil @fruit_taste = nil end def add_fruit(fruit) @fruit = fruit puts @fruit end def add_fruit_color(color) @fruit_color = color puts @fruit_color end def add_fruit_taste(taste) @fruit_taste = taste puts @fruit_taste end endTo explain what we did here, basically we created methods that contains an argument, where when we place the value on that argument, the accessors assigned to it will take that value for themselves. How are we going to see if this script is working or not? Make sure you have your Console ON here first. Now, create an event and add this script call:

$game_system.add_fruit(fruit)$game_system.add_fruit_color(color)$game_system.add_fruit_taste(taste)where fruit = name of the fruit, color the color of the fruit and taste the taste of the fruit. Note that we are going to use strings here.

Example:

$game_system.add_fruit("Apple")$game_system.add_fruit_color("Red")$game_system.add_fruit_taste("Delicious")As you can see on your console, the result would be:

AppleRedDeliciousThat is how the basics of declaring them and accessing them are. Now, if you noticed on Game_System, you have the following accessors by default:

attr_accessor :save_disabled # save forbidden attr_accessor :menu_disabled # menu forbidden attr_accessor :encounter_disabled # encounter forbidden attr_accessor :formation_disabled # formation change forbidden attr_accessor :battle_count # battle countDo you know that we can touch them or access them as well? How? Let us try doing one. On an event, do this script call:

$game_system.save_disabled = trueNotice that after you made this event and accessed it, go to your menu and your save option is disabled.

Do this script call on an event:

$game_system.menu_disabled = trueand when you access your menu after doing this event and activating it disables your menu. You can also try the others. Lastly, let us display our battle count:

On a script call, do:

puts $game_system.battle_count and when you activate this event, on your console displays your battle count.

[IMG]http://i.imgur.com/n6fvBXt.png[/IMG]​
 ​
Now, let us work on something nice here. For example, do you know how annoying the default message box choice position is? I know you do. We can't even set up its position where we want the choices are in the events tab, so we are here to do that using the Game_System.

First of all, let us set up the things we need:

  • Choice Width
  • Choice Window Width
  • Message Box Y Position
  • Message Box X Position
  • Choice Height
We can do this via creating attribute accessors on our Game_System. On a new script section, do this:

#==============================================================================# ** Game_System#------------------------------------------------------------------------------# This class handles system data. It saves the disable state of saving and # menus. Instances of this class are referenced by $game_system.#==============================================================================class Game_System #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor :choice_width attr_accessor :choice_window_width attr_accessor :mb_y_pos attr_accessor :mb_x_pos attr_accessor :choice_heightendWe not set up the things we need at the moment. Now, on our initialize method on the Game_System, we need to set up their initial value. Add this on our script inside the Game_System:

#-------------------------------------------------------------------------- # * Alias Listings #-------------------------------------------------------------------------- alias message_choice_initialize initialize #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- def initialize message_choice_initialize @choice_width = 320 @choice_height = 20 @mb_y_pos = 30 @mb_x_pos = 0 @choice_window_width = Graphics.width - @choice_width endRight here, we now placed our initial values.

We are not going to apply the same logic on the ones we did earlier about the passing of arguments. 

#-------------------------------------------------------------------------- # * Change Y Position #-------------------------------------------------------------------------- def self.change_y_pos(y_pos) @mb_y_pos = y_pos end #-------------------------------------------------------------------------- # * Change X Position #-------------------------------------------------------------------------- def self.change_x_pos(x_pos) @mb_x_pos = x_pos end #-------------------------------------------------------------------------- # * Change Window Width #-------------------------------------------------------------------------- def self.change_window_width(w_width) @choice_window_width = w_width end #-------------------------------------------------------------------------- # * Change Choice Height #-------------------------------------------------------------------------- def self.change_choice_height(c_height) @choice_height = c_height endWe have to set a change value method for each of the accessors. This would be the full code now:

#==============================================================================# ** Game_System#------------------------------------------------------------------------------# This class handles system data. It saves the disable state of saving and # menus. Instances of this class are referenced by $game_system.#==============================================================================class Game_System #-------------------------------------------------------------------------- # * Public Instance Variables #-------------------------------------------------------------------------- attr_accessor :choice_width attr_accessor :choice_window_width attr_accessor :mb_y_pos attr_accessor :mb_x_pos attr_accessor :choice_height #-------------------------------------------------------------------------- # * Alias Listings #-------------------------------------------------------------------------- alias message_choice_initialize initialize #-------------------------------------------------------------------------- # * Object Initialization #-------------------------------------------------------------------------- def initialize message_choice_initialize @choice_width = 320 @choice_height = 20 @mb_y_pos = 30 @mb_x_pos = 0 @choice_window_width = Graphics.width - @choice_width end #-------------------------------------------------------------------------- # * Change Y Position #-------------------------------------------------------------------------- def self.change_y_pos(y_pos) @mb_y_pos = y_pos end #-------------------------------------------------------------------------- # * Change X Position #-------------------------------------------------------------------------- def self.change_x_pos(x_pos) @mb_x_pos = x_pos end #-------------------------------------------------------------------------- # * Change Window Width #-------------------------------------------------------------------------- def self.change_window_width(w_width) @choice_window_width = w_width end #-------------------------------------------------------------------------- # * Change Choice Height #-------------------------------------------------------------------------- def self.change_choice_height(c_height) @choice_height = c_height end endNow, there's something we need to tweak before we can get our script going. We need to tweak our Window_ChoiceList because that is where the window for Show Choices command is being created.

Below your script do this:

#==============================================================================# ** Window_ChoiceList#------------------------------------------------------------------------------# This window is used for the event command [Show Choices].#==============================================================================class Window_ChoiceList < Window_Command #-------------------------------------------------------------------------- # * Update Window Position #-------------------------------------------------------------------------- alias window_choice_update_placement update_placement def update_placement window_choice_update_placement self.width = $game_system.choice_window_width self.height - $game_system.choice_height self.x = $game_system.mb_x_pos self.y - $game_system.mb_y_pos end endWe substituted the width and height to our current choice height and width to our window_width. We are using $game_system because that is the instance of our class. We created this under the update_placement because, that is where it shows the window and updates its measurements. If we ever change the measurements, that is what the update_placement will read and set as the measurement of the window.

How are we going to use this script then? Here are the script calls:

Code:
$game_system.change_y_pos(y_pos) 
changes the y position of the choice list, where y_pos is the number.

Code:
$game_system.change_x_pos(x_pos)

changes the x position of the choice list, where x_pos is the number.

Code:
$game_system.change_window_width(w_width)

changes the width of the choice list window, where w_width is the number.

Code:
$game_system.change_choice_height(c_height)

changes the window height of the choice list, where c_height is the number.

 

[IMG]http://i.imgur.com/n6fvBXt.png[/IMG]​
 

And that's exactly it! See how many possibilities you can do with Game_System? Enjoy coding everyone! Game_System is a good start for scripting, since it opens ideas that you might have had in mind but you don't know how to start them. Hope this tip helps and help you create your own scripts. Until next time!
Nice. But is it possible to add more choices for a Choice window this way?
Yes, but you need to re-build the list of choices to include the additional choices.


My large choices implementation simply uses Game_Message's existing approach.
Yes, you can set up the choices right on Game_System, but its much easier to set them up on the Show Choices command itself on Game_Interpreter.

Spoiler
#-------------------------------------------------------------------------- # * Show Choices #-------------------------------------------------------------------------- def command_102 wait_for_message setup_choices(@params) Fiber.yield while $game_message.choice? end #-------------------------------------------------------------------------- # * Setup Choices #-------------------------------------------------------------------------- def setup_choices(params) params[0].each {|s| $game_message.choices.push(s) } $game_message.choice_cancel_type = params[1] $game_message.choice_proc = Proc.new {|n| @branch[@indent] = n } end 
But if you mean only at a certain time will you have them changed on maximum numbers, that's also probable in Game_System.
Hello, first I'm sorry if this kind like a necropost or perhaps not because it's only June(I know that shaz will kick my ass, so I'm sorry). I'm just curious if using game_system for a place to store data, then could it possibly make the game lag if for example I'm storing a lot of data to game_system(I mean very lot of data)?

thank you all, peace.
AFAIK, it would, esp if you made them global.
Ah I should've known it. So it's not really a good idea using game_system for storing data I believe? So if we can, we should avoid using it then? Sorry I'm pretty new to this scripting things. I'm just curious if maybe for the worst, it may cause a very great lag in-game and probably even to the state of freeze for several seconds. And I bet the RAM and CPU memory plays a great role here, so if your RAM and CPU performance is very good, then it will helps, is that right? 

Well, thank you very much for the reply.. 🙂
Simply storing data within an instance that is globally accessible will not degrade your performance at all, unless you start running up against actual physical limitations, which would be ludicrous. Storing things like strings, fixnums, hashes, arrays or even full structures containing these objects should be orders of magnitude away from that amount of mem usage you would need to cause any slowdown. All current objects are exposed globally either way (via ObjectSpace). The reason using globals as a whole is discouraged is more of an organization thing than a performance thing (variables set up to be changed from anywhere can and WILL be changed from anywhere).


Basically, it depends on what data you want to store. If you are just storing a few things you need to be persistent (say, an array or hash of encountered monster ids for a monster log or some such), Game_System is fine. If you need an entirely new data structure, it is probably best create your own class or module (again, mostly so you don't pollute the Game_System namespace too much), but if you want that data to be persistent across saves, you'll need to alter the DataManager methods make_save_contents and extract_save_contents to include your data, unless the object you have created is already included in one of the objects already saved.
Mithran has explained everything. 🙂

Original thread: https://forums.rpgmakerweb.com/threads/tips-on-working-with-game_system.26160/