Home > RGSS Script Reference > Yield
Yield
Description: The "yield" keyword is a special feature of Ruby. It allows you to directly execute a block of code passed to the method. In the code example below, the "command_337" method, the "iterate_battler(@parameters[0], @parameters[1]) do |battler|" statement executes the "iterate_battler" method, with the yield parameter "|battler|". Each time the "iterate_battler" method yields a battler, it is passed back to "command_337" and the block associated with the "do" is executed with that battler as the parameter. This makes it possible to execute a block of code with a variable number of arguments. In the code example, it's possible that the battlers targetted could be one actor, one enemy, all actors in the party, or all enemies.
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
|
|
|