undefined method in personnal script

Status
Not open for further replies.

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,039
First Language
French
Primarily Uses
RMMV
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 

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

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          
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
First Language
English
Primarily Uses
RMMV
Try it like this:

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
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,039
First Language
French
Primarily Uses
RMMV
sadly I obtain this error now 

 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
First Language
English
Primarily Uses
RMMV
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.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,039
First Language
French
Primarily Uses
RMMV
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 
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
934
First Language
English
Primarily Uses
N/A
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.
 

bgillisp

Global Moderators
Global Mod
Joined
Jul 2, 2014
Messages
13,369
Reaction score
14,077
First Language
English
Primarily Uses
RMVXA
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.
 
Last edited by a moderator:

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,039
First Language
French
Primarily Uses
RMMV
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

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 
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
First Language
English
Primarily Uses
RMMV
Okay fixed the no method error though I don't think the notetags are working correctly.

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.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,039
First Language
French
Primarily Uses
RMMV
yes this is help but now the notetag don't work xD 
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
934
First Language
English
Primarily Uses
N/A
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.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,039
First Language
French
Primarily Uses
RMMV
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 ?
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
934
First Language
English
Primarily Uses
N/A
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.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,039
First Language
French
Primarily Uses
RMMV
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 ...
 

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
First Language
English
Primarily Uses
RMMV
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
 
Last edited by a moderator:

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
562
Reaction score
273
First Language
German
Primarily Uses
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.
 
Last edited by a moderator:

Sarlecc

Veteran
Veteran
Joined
Sep 16, 2012
Messages
453
Reaction score
211
First Language
English
Primarily Uses
RMMV
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.
 

Zalerinian

Jack of all Errors
Veteran
Joined
Dec 17, 2012
Messages
4,696
Reaction score
934
First Language
English
Primarily Uses
N/A
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:

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?


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.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,039
First Language
French
Primarily Uses
RMMV
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:

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?


(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 
 
Last edited by a moderator:

bgillisp

Global Moderators
Global Mod
Joined
Jul 2, 2014
Messages
13,369
Reaction score
14,077
First Language
English
Primarily Uses
RMVXA
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.
 
Status
Not open for further replies.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Just for the lols I have taken the Szondi test. That test looked like a load of bull. But for whatever reason it was incredibly accurate.
How can a society function with an entire media establishment that is complicit in publishing partisan slander and easily disprovable lies right alongside each other with no accountability whatsoever? After I live through it, maybe I'll make a game based on it. The level of dishonesty at this point just astounds me though.
If you download a resource, be it art or plugin, please thank the creator for their time. We are all guilty sometimes of just grabbing something without commenting. Now is the time to go through your credit list and visit the threads/site where you got the resource from and leave a heartfelt thank you!
October is officially here. I'm hoping to get a call from Spirit Halloween soon. It didn't happen today, but I've got to keep my head high.
So, what is this "Inktober" everyone's speaking about? Because apparently, I have no idea what happens on this site...

Forum statistics

Threads
103,490
Messages
999,745
Members
135,038
Latest member
CStrife912
Top