Defining re-usable script calls

It is common practice to define methods in the Game_Interpreter so others can make script calls easily

class Game_Interpreter # Return a random weapon def get_random_weapon $data_weapons.sample endendHowever, this means that you can't re-use your method if you want to call it from another class.What are some ways to re-write your script calls so that your methods are more re-usable, while still making it easy for users to call from script boxes?
You could technically call the method from '$game_map.interpreter.{method}' within the class. Or '$game_troop.interpreter.{method}'. Although I am not sure if that is what you are really looking for. I am a little too rushed at the moment to come up with something different/elegant.

EDIT : Yeah, that was not what you were wanting. My apologies.

What about 'extend'?

Code:
module CodeRepository  def useful_method; endendclass YourScript  extend CodeRepository # unless YourScript.extend_code?  # └ Extends useful_method into class    # Script Code ...endclass Game_Interpreter  extend CodeRepository # unless YourScript.extend_code?  # └ Extends useful_method into class  # Game_Interpreter Code ...end
don't define it in Game_Interpreter

just write down

def get_random_weapon $data_weapons.sampleendor

class Object def get_random_weapon $data_weapons.sample endendand it will be usable anywhere
don't define it in Game_Interpreter

just write down

def get_random_weapon $data_weapons.sampleendor
Code:
class Object  def get_random_weapon    $data_weapons.sample  endend
and it will be usable anywhere
That is true, but then the code is ... everywhere. That sort of clutters up every single object created.
That is true, but then the code is ... everywhere. That sort of clutters up every single object created.
Yeah, true. Actually, I prefer not to do it if the script call isn't so important to use in another object.

You could technically call the method from '$game_map.interpreter.{method}' within the class. Or '$game_troop.interpreter.{method}'. Although I am not sure if that is what you are really looking for. I am a little too rushed at the moment to come up with something different/elegant.

EDIT : Yeah, that was not what you were wanting. My apologies.

What about 'extend'?

module CodeRepository def useful_method; endendclass YourScript extend CodeRepository # unless YourScript.extend_code? # └ Extends useful_method into class # Script Code ...endclass Game_Interpreter extend CodeRepository # unless YourScript.extend_code? # └ Extends useful_method into class # Game_Interpreter Code ...end
According to this article http://www.railstips.org/blog/archives/2009/05/15/include-vs-extend-in-ruby/

Extend will make it as a class method. Not an object method. So in order to use, you have to write the script like ClassName.method_name which is too long I think.
Mm, my code was a little off ...

Code:
module ScriptRepository  def useful_method    p "Test"  endendclass YourScript  extend ScriptRepository # unless YourScript.extend_code?  # └ Extends useful_method into class  # ... or via initialize ...  def initialize    extend ScriptRepository # unless YourScript.extend_code?    # └ Extends useful_method into class  end    def other_method    useful_method  end  # Script Code ...endclass Game_Interpreter  alias initialize_old initialize  def initialize(*args, &block)    extend ScriptRepository # unless YourScript.extend_code?    # └ Extends useful_method into class    initialize_old(*args, &block)  end  # Game_Interpreter Code ...end
Now you can call 'useful_method' via :
  • $game_map.interpreter.useful_method
  • $game_troop.interpreter.useful_method
  • useful_method (Script Command)
  • YourScript.useful_method (without 'initialize')
  • useful_method (with 'initialize'; within YourScript)
Uhm, what if you use this method?

class Game_Interpreter    include MODULENAMEendIDK if it can take classes though... but for modules, it seems to work nice... now users can just use the method names for event calls... for you just use the default Module.method call in your other scripts...

like:

module Adik    def game_over        Scene_Manager.goto(Scene_GameOver)    endend class Game_Interpreter    include Adikend
so users can do in their events

Script: game_over
while if you're doing it in scripts, you just need to 

Code:
class XD  def huh    #do something else    Adik.game_over  endend
Where a method should live depends on what it does.

In this example, you might create a custom class with a static method for "random_weapon".  This method could be called off of your class type from anywhere in another script or event command.  Or you could still add a short-cut method to the interpreter and have that method call the static worker method on your class.

If you are exposing functionality that needs its own reference available for the lifetime of the application then you'll probably expose an instance of your object as a global variable and that would let you call instance methods from anywhere (now your work methods do not have to be static).

For example, in the RSQS script, I expose a $game_reedo_quests global variable which contains all of the player's know quests as well as helper methods for working with the quest system.  From within an event script command, I use local variables to hold references to my custom object instances and then call methods off of the local variables.  So long as I don't need too many lines of code, this works well to keep the length of a single line within the limits of the script box.... like this:

q = $game_reedo_quests.get_quest(2)
r = q.current_stage.requirements[0]
$game_variables[2] = r.kills_left(1)
In your example you already have $data_weapons.sample which being a method on a global variable is accessible. No change needed.

The names confused me a bit. I know sampling is used within statistic, but I still prefer $data_weapons.random_weapon.

Having the class managing the collection of RPG::Weapon in the game pick one at random can work conceptually. You can look at it different and say it is simply data so you must have some other entity look at the data and pick one. A question of whether you want to separate that concern.

I enjoyed reading the meta-programming examples (thanks ExHydra 😀 )

As a heuristic I would suggest you start by looking at the conceptual model and play around with the design.

*hugs*
.... I know sampling is used within statistic, but I still prefer $data_weapons.random_weapon....
Actually, isn't $data_weapons just an array?  I'm pretty sure all of the $data_ objects are just arrays of their corresponding types.

So that being the case, a "get_random" method which returns a random item from an array would be the most logical addition to the array class.  Then you could call the method on any array object.  And this makes sense, too, since getting a random entry from an array is an operation that does not care what kind of objects the array contains.

$data_weapons.get_random()

$data_armors.get_random()

$data_items.get_random()

etc, etc, etc.

All of these become available by defining the logic in the most appropriate place.

You can follow OOP principle when working with RGSS3.  It will always be safe to extend any existing class with new functionality if said functionality in no way utilizes or impacts existing functionality.  The only extra care required in Ruby is unique naming.  It would probably be best to add your new method using a prefix unlikely to be repeated:

Array.hime_abc_get_random()

Where abc are some initials for the script.  This way if some other script also adds a "get_random" method to Array which does something else, your script will continue to use your implementation of the "get random" method.

So again, the best place to put a particular piece of code will depend on what that particular piece of code is and does.
I was wrongly considering Ruby 1.8.x where I should have considered 1.9.x: http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-sample


I assumed the method used special knowledge its contents to retrieve a weapon according to a particular distribution. (non-uniform)


In that case it would be preferable either to have it directly on the container or a separate class.


Another question is whether the distribution can change over time. Should you place it closer to the static data or the dynamic game classes.

So again, the best place to put a particular piece of code will depend on what that particular piece of code is and does.
I wholeheartedly agree.
*hugs*


- Zeriab

Original thread: https://forums.rpgmakerweb.com/threads/defining-re-usable-script-calls.18466/