Home > RGSS Script Reference > |Iterator|
|Iterator|
Description: Iterators allow you to call a block of code from within a method by using the "yield" keyword (also described in the syntax documentation). Once the method ends, control is transferred back to the method that called the iterator block's code, right after the "yield" statement. You'll notice that the code sample for iterators is the same as for "yield". This is to highlight their relationship to each other. You can declare an iterator block only on the line where a method definition begins, immediately following the method's last argument. If the method encounters a "yield" statement, then the code in the block will be executed with the yield's parameters. An iterator is always enclosed in pipe characters (|) as shown in the code example.
There are also built-in methods that use iterators implcitly, such as the array method "each", which, when invoked, simply yields each element in the array in succession.
Code Sample:
def iterate_battler(parameter1, parameter2)
if parameter1 == 0
iterate_enemy(parameter2) do |enemy|
yield enemy
end
else
if parameter2 == -1
for actor in $game_party.actors
yield actor
end
else
actor = $game_party.actors[parameter2]
yield actor if actor != nil
end
end
end
end
|
def command_337
iterate_battler(@parameters[0], @parameters[1]) do |battler|
if battler.exist?
battler.animation_id = @parameters[2]
end
end
return true
end
|
|
|