undefined method in personnal script

● ARCHIVED · READ-ONLY
Started by nio kasgami 20 posts View original ↗
  1. Hi folks! 

    I begin to do a really simple script who permit to have specific music by enemy  

    the notetag work !

    is the calling via the BattleManager

    here the error 

    Spoiler
    TY0ylaG.png
    so here the whole scripts and I highlight the principal line who cause me problem...

    Spoiler
    Code:
    =beginNio Kasgami Hope EngineEnemy Specific Battle Song=endmodule 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          
  2. Try it like this:

    Spoiler
    Code:
    class RPG::Enemy < RPG::BaseItem 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 = enemy.check_music_notetagendend
  3. sadly I obtain this error now 

    Spoiler
    41GL5TF.png
  4. Okay forgot no definition of enemy in the BattleManager. :(

    Been thinking too if you got this working what if you had a troop with one enemy with the <Monster> tag and another with the <Human> tag?

    Sadly I have some work to do atm so I can't continue working on this problem.
  5. Sarlecc said:
    Okay forgot no definition of enemy in the BattleManager. :(

    Been thinking too if you got this working what if you had a troop with one enemy with the <Monster> tag and another with the <Human> tag?

    Sadly I have some work to do atm so I can't continue working on this problem.
    haha don't worry and well I not fixed that when you have both enemy and monster in it this will call the regulard theme 
  6. def self.play_battle_bgm @music = Game_Enemy.check_music_notetag endyou are trying to use the check_music_notetag method on the class itself. The method, however, is only valid on an instance of the class (so a value returned by Game_Enemy.new). To make te method usable to the class which it won't really work, since the class has no notetags itself), you'd need to do 

    def self.check_music_notetagNote that 'self.method' makes the method usable by calling ClassName.method.
  7. You have your ends in the wrong spot. In ruby, you need an end after every if, else it is going to keep going. You need something like this:

    if enemy.note =~ /<Monster>/

    RPG::BGM.new("Battle2",100,100).play

    end

    Without that, you are saying you want the second if nested inside this if. Also, this is messing up your definition so it does not exist as a definition, hence the undefined. Try adding the ends in the right spot, then it should work.

    Also, in your attempt at redefining the code, you need to alias the original method. That is why there is no enemy class.
  8. bgillisp said:
    You have your ends in the wrong spot. In ruby, you need an end after every if, else it is going to keep going. You need something like this:

    if enemy.note =~ /<Monster>/

    RPG::BGM.new("Battle2",100,100).play

    end

    Without that, you are saying you want the second if nested inside this if. Also, this is messing up your definition so it does not exist as a definition, hence the undefined. Try adding the ends in the right spot, then it should work.

    Also, in your attempt at redefining the code, you need to alias the original method. That is why there is no enemy class.
    I changed the end and the alias not work at all this provoke bug is why I do a overwrite

    Zalerinian said:
    def self.play_battle_bgm @music = Game_Enemy.check_music_notetag endyou are trying to use the check_music_notetag method on the class itself. The method, however, is only valid on an instance of the class (so a value returned by Game_Enemy.new). To make te method usable to the class which it won't really work, since the class has no notetags itself), you'd need to do 

    def self.check_music_notetagNote that 'self.method' makes the method usable by calling ClassName.method.
    I changed via a self method but still have error 
  9. Okay fixed the no method error though I don't think the notetags are working correctly.

    Spoiler
    class RPG::Enemy < RPG::BaseItem def self.check_music_notetag if @note =~ /<Monster>/ RPG::BGM.new("Battle2",100,100).play end if @note =~ /<Human>/ RPG::BGM.new("Battle9",100,100).play else $game_system.battle_bgm.play end @note endendmodule BattleManager def self.play_battle_bgm@music = RPG::Enemy.check_music_notetagendend
    I am still working so I am writing this on the fly hope it helps.
  10. yes this is help but now the notetag don't work xD 
  11. What error are you getting now? They will be different, since the method is defined now.

    Also, the reason jotetags aren't working is because you are use the method on the class itself. The class has no notetags. You need to use the method on an instance of the class. This means you need to remove the 'self.' part, and then change where you do Game_Enemy.check_music_notetag. The method MUST be run on an instance of the class for it to work right. Trying to do it on the class means that 'enemy' is not going to be defined.
  12. Zalerinian said:
    What error are you getting now? They will be different, since the method is defined now.

    Also, the reason jotetags aren't working is because you are use the method on the class itself. The class has no notetags. You need to use the method on an instance of the class. This means you need to remove the 'self.' part, and then change where you do Game_Enemy.check_music_notetag. The method MUST be run on an instance of the class for it to work right. Trying to do it on the class means that 'enemy' is not going to be defined.
    arf in simple for be able to ''use '' my notetag I need to transpose them into a  speudo class ?
  13. No. Nonono. You need to make an instance​​ of the class.

    myEnemyInstance = Game_Enemy.new(0, 1)myEnemyInstance.check_music_notetagThe instance of the enemy would have the notes, if anything. Even that I think may be wrong. But it won't work the way you're using it because the class doesn't have 'enemy' defined without 'initialize' being called.
  14. okai if I understand 

    I don't use the proper class I think the more proper class is the Game_Enemy right? 

    class Game_Enemy < Game_Battleralias hope_initialize initializedef initialize( )supercheck_notetaghope_initializeenddef check_notetage#ectendnow if I initialize in this way this will be good or i am again not in the good way? 

    sorry i am getting more and more confusing now ...
  15. 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. 

    And I have to run again sorry if I don't make much sense. :p
  16. Zalerian is right here, don't confuse a class object with the instances created from it.

    Game_Enemy and RPG::Enemy are instances of the Class class. They specify how their created instances respond by default, but don't replace them.

    A class instance itself usually knows nothing about the instances created from it.

    When you want to access an enemies notebox you will face the problem that there could be hundreds of enemies currently in the system or even not a single one. You will have to find a way to determine which of all those enemies you want to access.

    At the beginning of a battle, the Game_Enemy instances the party will fight against are usually stored in $game_troop.members. You can fight against up to 8 enemies by default, so you have to determine which of them determines the battle music.
  17. Yea realized this a bit later been running around a lot so haven't had much time to sit down and really think this out. :/

    Still working on it though.
  18. 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. 

    1. make enemy an instance variable.
    2. In order to get a Game_Enemy instance from the BattleManager, you need to go through the $game_troop global variable.
    3. 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?


    Sarlecc said:
    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.
  19. Zalerinian said:
    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 
    Spoiler
    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. 

    1. make enemy an instance variable.
    2. In order to get a Game_Enemy instance from the BattleManager, you need to go through the $game_troop global variable.
    3. 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?


    (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.

    Now I understand I worked in the bad class all the begin?? 

    for the instance variable i am not sure of what you mean 

    and thanks people for have taking your time to answer me and i am sorry to not be able to not be confuse in all this stuff ._.

    3) : the answer this will be : if the script detect ''two'' notetag who are initialize this will simply call a another battle theme already set 
  20. Here is what you need to read the notetag:

     $data_enemies[enemy.enemy_id].note

    After note add your code to parse it. See, the notetag is saved in data_enemies, then you enter your enemy it is supposed to check (enemy.enemy_id saves that info so it checks the enemy id in the battle). This should work for finding the notetag.

    If I see right you are trying to write code to create a script to change the battle music in special circumstances? If so I think DP3 has a script for this? If not he shows how to do it in his videos. I can post the code he shows in his videos to do this if you need it.