Sorry for the short/badly explained responses previously, I was on my phone. Now, What you were trying to do, Nio, is have the class interpret a notetag:
Spoiler
module Enemy_Songendclass Game_Enemy < Game_Battler def initialize(index, enemy_id) super() @index = index @enemy_id = enemy_id enemy = $data_enemies[@enemy_id] @original_name = enemy.name @letter = "" @plural = false @screen_x = 0 @screen_y = 0 @battler_name = enemy.battler_name @battler_hue = enemy.battler_hue @hp = mhp @mp = mmp check_music_notetag end def check_music_notetag if enemy.note =~ /<Monster>/ RPG::BGM.new("Battle2",100,100).play if enemy.note =~ /<Human>/ RPG::BGM.new("Battle9",100,100).play else $game_system.battle_bgm.play end end endendmodule BattleManager def self.play_battle_bgm @music = Game_Enemy.check_music_notetag endend
What you are doing here is attempting to call an
instance method without using an instance of the class. This is why you received an error stating that the method is undefined. In order to make a class method, the method must be defined as
self.method. However, you
do not want that here. Trying to do that would still not work. The class itself has no variable named 'enemy', this is a variable that is
local to the
initialize method. Once the object is done being created (i.e. initialize has reached it's last line), the
enemy variable
no longer exists. In order to keep it, you
must make it an instance variable, just like all the other variables in initialize. You may even think that
enemy still exists when the call to
check_music_notetag is run at the end of initialize.
It does not. A local variable only exists in the method that created it. This is called a variable's
scope, which is where it exists.
I will not give you the answer to your problem, I will tell you how to do it and you will learn what you must do. In order for this to work, you must do a few things.
- make enemy an instance variable.
- In order to get a Game_Enemy instance from the BattleManager, you need to go through the $game_troop global variable.
- Consider what happens when there is more than one enemy in the battle, and there re more than one set of notetags for music. What will play in cases like this?
Zalerinian all the enemies get their information from the class RPG::Enemy < RPG::BaseItem. This class should if I understand it correctly make a new instance for each monster in the database. What is more is the above class actually exists outside of class Game_Enemy. My guess is that the notetags just aren't setup correctly where they are being loaded.
(I noticed you crossed the text out, but I still want to try to explain)
RPG::Enemy is a class that has information on a battler that is defined in the database. While you are correct in saying that this is where the notes information would be, this does not create an instance of every enemy. Instances of enemies are only made when a troop is being prepared for battle. For reference on this, look at the Game_Troop script, setup method (starts on line 62). For each enemy in the troop that corresponds to the given ID, we check to make sure an entry exists for the given enemy. If it does not, we cannot create a battler for this enemy, because they do not exist. The Game_Enemy initialize method makes use of the information stored in the RPG::Enemy class. This second line in the loop,
enemy = Game_Enemy.new(@enemies.size, member.enemy_id) is what makes an instance of the class. At this point, we are able to use the
check_music_notetag method on
enemy, because we have an instance, which is capable of running instance methods.