self && @instance_variables
In RGSSx scripts it is not impossible to find calls that begin with a self reserved word.
What is self?
As we had learned long time ago, self simply means the same class or module.
Why is it there?
In modules like Kernel or RPG::Cache alias Cache and the File class, you cannot call certain methods UNLESS you include the actual module or class name somehow.
While defining stuff pertaining to your module or class, you can avoid using its actual name and simply replace it with self. It is way more practical than typing the same name over and over again.
Why can't you call them in any other way?
That is because the method has become what we call a singleton method. Only a single instance will get access to the data it contains at any given moment. What instance? The module or class itself!
HEY! That's not true!
Huh? What do you mean by that?
I've seen it used in other circumstances and it's not referring to the module or class at all.
Oh, you mean those instances like self.contents and self.bitmap found in window scripts.
For starters, yeah...
Don't worry about it! That is easy to explain indeed.
What happened there is that some of those calls are seeking hidden methods, those written in C or C++, and there is no other way to access them or else they could be treated as simple local variables.
Code:
self.contents.font.size = 32
In this example, the use of self is optional. Just use contents and it will work fine.
Is there any difference between using one option or the other?
You got me here. Yes, there is one!
What takes place there is that contents alone forces Ruby to look for any instance of either a local variable OR a method called contents. While self.contents already informs the interpreter that you are looking for a method, be it a class or instance method. (Class methods are singletons by default.)
But there is a catch! There is always a catch!
Code:
contents = Bitmap.new(32, 32)
self.contents = Bitmap.new(32, 32)
The first call would just create a local variable that will cease to exist the moment that method finishes its execution. The latter is ye old and trustworthy self.contents assignment method and it will keep the Bitmap object stored there for future references.
This is What Actually Made Me Write This Review!
There is a caveat that we scripters should know about just in case we still ignored it for any reason.
What happens very often in RGSS1, namely on RMXP, during battle is a very unusual phenomenon in Ruby scripting. This is one of the very few places where this has ever happened in decades.
classes like Game_Battler make several references to methods like self.damage instead of using @damage directly.
Why does this ever happen?
Well, I guess that scripters were not fully aware of how @instance variables were supposed to be used in a script. Thus, what I mean here is that they did something totally weird by calling self.damage instead of simply typing @damage whenever they want to calculate the actual damage or healing any given Game_Actor or Game_Enemy is about to receive at any moment.
But is it something really bad at all?
Hrm, I suppose that depends on what you want to accomplish there.
If you are planning to pre-calculate some basic damage, that might be helpful indeed.
Otherwise using a plain @damage instance variable would suffice there, letting you to hit that bad mobster as hard as you can. Go for it, Arshes!
Other implementations like RGSS3 actually provides us with calls to methods in place of variables to let you calculate base prices, for instance. And nope, they do not use the self pseudo variable at all.
They may or may not have the same name as the actual variable that is going to be used there.
You see, once you choose to use a method there, you are free to name it they way you prefer to your heart's content.
And anybody can later alias the method at will.
The Conclusion
This is one of those cases where there is no way to get quite conclusive about what you are supposed to do now because people can tell you at a certain point that it is a matter of style or taste.
And they might be right about that no matter how weird that might sound like.
"For God has not destined us for wrath, but for obtaining salvation through our Lord Jesus Christ," 1 Thessalonians 5:9
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE
Maranatha!
The Internet might be either your friend or enemy. It just depends on whether or not she has a bad hair day.
My Original Stories (available in English and Spanish)
List of Compiled Binary Executables I have published...
HiddenChest & Roole
Give me a free copy of your completed game if you include at least 3 of my scripts!
Just some scripts I've already published on the board...
KyoGemBoost XP VX & ACE, RandomEnkounters XP, KSkillShop XP, Kolloseum States XP, KEvents XP, KScenario XP & Gosu, KyoPrizeShop XP Mangostan, Kuests XP, KyoDiscounts XP VX, ACE & MV, KChest XP VX & ACE 2016, KTelePort XP, KSkillMax XP & VX & ACE, Gem Roulette XP VX & VX Ace, KRespawnPoint XP, VX & VX Ace, GiveAway XP VX & ACE, Klearance XP VX & ACE, KUnits XP VX, ACE & Gosu 2017, KLevel XP, KRumors XP & ACE, KMonsterPals XP VX & ACE, KStatsRefill XP VX & ACE, KLotto XP VX & ACE, KItemDesc XP & VX, KPocket XP & VX, OpenChest XP VX & ACE