How to Shorten Certain Script Calls

How to Shorten Certain Script Calls​

This is specially useful for RMXP and RMVX users!

Scripting Level: Intermediate

As you might have already noticed, the script call box is kind of short. Actually, XP's box is the smallest one of all RGSS based engines. So what do you do there? How can you make your script calls fit the small box?

Here is how you can do it.

Instead of calling...

Ruby:
if $game_party.actors[0].weapon_id == 20
  print "It's a bow!"
end

...you use this instead:

For XP
Ruby:
actor = $game_party.actors[0]
if actor.weapon_id == 20
  print "It's a bow!"
end

For VX
Ruby:
actor = $game_party.members[0]
if actor.weapon_id == 20
  print "It's a bow!"
end

What you do is very simple indeed, you just learn how to use a local variable.
What is that?
It's using a character or a whole word and place an equal = sign to its right hand side, followed by a value. (That process is called an assignment.) Here it's number $game_party.actors[0] alias the team leader, namely Aluxes in RMXP.
Then you keep using this new variable only like I did in the if statement, a condition. Since any actor is expected to be able to equip a weapon, we can call its method for the weapon's ID.

Ruby:
actor.weapon_id == 20

That line alone allows you to check if that weapon's ID is 20, supposedly a bow, or not. That dot between actor and weapon_id binds them. The first one is our variable contaning a specific actor while the other one is a method, something that belongs to that actor. Double equal sign stands for comparison, like saying "is it the same or not?" and it can only return a true or false as a value.

If that leader has truly equipped that bow, it will make the engine print a short message on a small pop-up window.

The Script Editor and Modules​

There are scripters that love to nest one or more modules inside a parent module like we can see in the example below:

Ruby:
module Module1
  CONSTANT1 = 0
  module Module2
  end
  module Module3
    module Module3_1
      CONSTANT3_1 = 3
    end
  end
end

It's quite easy to call a constant set in the first module, just use this simple call:

Ruby:
Module1::CONSTANT1

It looks quite easy for anybody right?
But what happens if we actually need to retrieve the CONSTANT3_1's value instead?
Well, in that case you'll need to call it like this:

Ruby:
Module1::Module3::Module3_1::CONSTANT3_1

😅 Yeah, you gotta chain all calls and go as deep as necessary to reach the exact constant you're looking for there.

But wait a second! Didn't I imply above that there could be a way to shorten that call?

Actually, yes my friend, you can do that!

Let's assume you'll soon need it inside a custom class like a window.

Ruby:
class Window_Testing < Window_Base
  def initialize(wx, wy, w, h)
    super
    self.x = Module1::Module3::Module3_1::CONSTANT3_1 + wx
    self.y = wy
  end
end

Is there a short way to achieve the same goal?
Yes, there is and we'll find it in my next sample.

Ruby:
class Window_Testing < Window_Base
  include Module1::Module3::Module3_1
  def initialize(wx, wy, w, h)
    super
    self.x = CONSTANT3_1 + wx
    self.y = wy
  end
end

Did you notice the changes I made there to shorten the call?
There I had to make a call to Module's include method that allows us to include a module inside a class or even another module. The advantage of using it is that you no longer need to call all 3 modules first before you can ever reach the CONSTANT3_1 constant inside.

Is it worth doing that for 1 or 2 constants?
Probably not, but if you do use the include method, Ruby aka RGSS here won't forget about this association we made above. This means that it won't have to go through all those 3 modules to find our dear constant every single time you create that custom window.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-shorten-certain-script-calls.160506/