silver wind
Member
So ruby doesn't support multiple inheritance, so I'm told.
I want to make a class inherit both Game_Actor and Game_Enemy.
I made this code, which works fine, but I'm wondering
is there an easier way ?
[rgss]class Enemy_and_Actor < Game_Actor
#=============================================================
# * Inherit_Game_Enemy
#
# The only purpose of this module is to add methods from
# Game_Enemy to this class.
# Instead of doing: @enemy.gold, you can call: gold
# This isn't only for convenience. Other classes will use this class
# instead of Game_Enemy, so they may call actor_enemy.gold
#=============================================================
module Inherit_Game_Enemy
def def_enemy_methods
# get a list of methods in Game_Enemy
methods = Game_Enemy.instance_methods - Game_Battler.instance_methods
# extract methods with arguments
special = [ 'initialize','element_rate','state_guard?','transform' ]
methods = methods - special
# create the methods using eval.
for method in methods
code = "def #{method.to_s}" + "; "
code += "@enemy.#{method}" + "; "
code += "end"
Inherit_Game_Enemy.module_eval(code)
end
end
#=================================
# Define methods with arguments
#=================================
def transform(enemy_id)
@enemy.transform(enemy_id)
end
def element_rate(element_id)
@enemy.element_rate(element_id)
end
def state_guard?(state_id)
@enemy.state_guard?(state_id)
end
end
extend Inherit_Game_Enemy
include Inherit_Game_Enemy
def initialize(id)
super(id)
@enemy = Game_Enemy.new(1,0)
# Once we have an enemy, define enemy methods in the module.
def_enemy_methods
# call a method of game_enemy
p "testing access: enemy's gold = #{gold},"
end
def is_a?(clas)
return (super or clas == Game_Enemy)
end
end
[/rgss]
I want to make a class inherit both Game_Actor and Game_Enemy.
I made this code, which works fine, but I'm wondering
is there an easier way ?
[rgss]class Enemy_and_Actor < Game_Actor
#=============================================================
# * Inherit_Game_Enemy
#
# The only purpose of this module is to add methods from
# Game_Enemy to this class.
# Instead of doing: @enemy.gold, you can call: gold
# This isn't only for convenience. Other classes will use this class
# instead of Game_Enemy, so they may call actor_enemy.gold
#=============================================================
module Inherit_Game_Enemy
def def_enemy_methods
# get a list of methods in Game_Enemy
methods = Game_Enemy.instance_methods - Game_Battler.instance_methods
# extract methods with arguments
special = [ 'initialize','element_rate','state_guard?','transform' ]
methods = methods - special
# create the methods using eval.
for method in methods
code = "def #{method.to_s}" + "; "
code += "@enemy.#{method}" + "; "
code += "end"
Inherit_Game_Enemy.module_eval(code)
end
end
#=================================
# Define methods with arguments
#=================================
def transform(enemy_id)
@enemy.transform(enemy_id)
end
def element_rate(element_id)
@enemy.element_rate(element_id)
end
def state_guard?(state_id)
@enemy.state_guard?(state_id)
end
end
extend Inherit_Game_Enemy
include Inherit_Game_Enemy
def initialize(id)
super(id)
@enemy = Game_Enemy.new(1,0)
# Once we have an enemy, define enemy methods in the module.
def_enemy_methods
# call a method of game_enemy
p "testing access: enemy's gold = #{gold},"
end
def is_a?(clas)
return (super or clas == Game_Enemy)
end
end
[/rgss]