Revisiting the Array Class: Values & Arguments
We already know that Arrays can hold a list of values. That is why other languages call them lists as well.
How do we get access to every single value stored therein?
Let us beginning with the classical method, namely by call them one by one.
We keep calling the Array until we are fed up with it.
Code:
map_id = $game_party.scp[0]
x = $game_party.scp[1]
y = $game_party.scp[2]
direction = $game_party.scp[3]
Since we are just applying assignments to those 4 local variables, nothing weird happens.
Did you know there are other ways to accomplish the same goal?
That piece of code can be reduced to the following:
Ruby:
Code:
map_id, x, y, direction = $game_party.scp
$game_player.reserve_transfer(map_id, x, y, direction)
What happened there was that we used a multiple variable assignment or parallel assignment that will feed all those variables to your left with as many values as the $game_party.scp Array has.
The rest of the variables will get a nil variable if the array to the right is not long enough as to find an actual value. nil is its default return value for Arrays.
Yet, we could even do it this simple way...
Code:
$game_player.reserve_transfer(*$game_party.scp)
What has happened here?
It is easy to explain. There the * asterisk is telling Ruby and RGSS that $game_party.scp should be treated as an Array and not as a single element or argument or parameter. So it gets treated as 4 parameters instead!
That only works that way IF you are passing it as an argument of a script call.
Method Definitions & Arrays as Arguments
This example will show you the exact opposite effect using the same * asterisk in a different context.
Code:
class MyClass
def method1(arg1, arg2, *extra_args_array)
@arg1 = arg1
@arg2 = arg2
@pos1, @pos2, @pos3 = extra_args_array
end
end
As you can see there, the * asterisk treats any extra arguments as an Array that can be accessed the same 2 ways we have seen above. Just don't use an * asterisk in line 5.
"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