Doubts about acessing $data_weapons

Hi everyone, I have some doubts about $data_weapons. I was trying to do a simple script that read the note-tag of the data base and save it in a Hash or something like that. But the problem is that I always end up falling in the same error:  I use $data_weapons.each do |weap| to pass through all the weapons, but every time I tried to run the script, it gives me the error message of 'undefined method 'each' for nil:NilClass'. I tried something like this:

class RPG::Weapon < RPG::EquipItem

  #--------------------------------------------------------------------------

 

      def value

          $data_weapons.each do |weap|

                weap.note.split (etc etc).each do |something|

                      etc

 

and a lot of others class to acess the $data_weapons, but I always got the same error. 

 

It's a simple question... If anyone could help me \o

 

Byee
hi there,

You should define weap beforehand because its not a instance variable, like putting this right below the comment:

weap = 0

i would also recommend to copy the $data_weapons array into a temporary array like @my_weapons = $data_weapons

because than you can "work" with that data easier, like shuffling it or whatever

hope this helps!
Hi! If i put this @my_weapons = $data_weapons above and then calls the each method $my_weapons.each do |weap|, I still getting the same error "undefined method 'each' for nil:NilClass" )):

But thanks, anyway!
Where are you putting the code? If you're putting it somewhere where $data_weapons has not yet been defined/created, then yes, it will crash.


If what you have in the first post is exactly the code you've got, then yes, it will crash. $data_weapons is made up of RPG::Weapon items, and is not defined within the item. You can't refer to it there.


Grab some of the scripts that already read note tags and see how they do it.
This is the proper way of reading notetags.

Code:
class << DataManager  alias :kocka_random_alias_load_database_2bX9 :load_database  def load_database    kocka_random_alias_load_database_2bX9    $data_weapons.each{|w|w.load_notetags}  endend class RPG::Weapon < RPG::EquipItem  attr_accessor :durability  def load_notetags    # Here, you load notetags    # The notetag is accessed through self.note    # For example, if you want to load the tag <durability:1>, you may want to use :    self.note.scan(/<\s*durability\s*:\s*(\d+)\s*>/im)    # (This will also load even if notetag is "<       durability     :      1      >"    # Now the result is stored in the variable $1    # If there is no such notetag, $1 would be nil.    # Now, let's store it to the durability if it has results, and if it has not then durability would be -1    @durability = $1 ? $1 : -1    # For more reference about regular expressions search the Help File  endend
Admiralac summed it pretty well, but you can still get an error with that code, unless you add a 'nil' check into the iteration itself, like this:

Code:
$data_weapons.each{|w| next if w.nil?; w.load_notetags}
Oh, yes, I forgot the first item is always nil 🙂

Or he/she can use

$data_weapons.each{|w|w.load_notetags if w}but that's pretty much the same
Ohh,  now I see. Thanks guys. I just didn't get how to access the durability (in this example) for each weapon. Since each weapon will do the load_notetags function, wouldn't the variable durability have only the last number read?
To Sixth and Kocka

Spoiler
I wouldn't suggest loading notetags on load_database as that is only ran during new games, that's the reason why a lot of scripts especially older ones doesn't work on save files made before the script was added... This is of course only for things saved into the save file, like actors and such (assuming you'd be using $game_actors).A more friendly way in that sense of doing it is something like

class YOUR_CLASS def load_durability @durability = @note =~ /<durability🙁.*)>/ ? $1 : 0 end def durability load_durability if @durability.nil? return @durability endendThen of course, always call it via the durability method instead of the variable directlyThat way, the variable will be initialized if it isn't yet initialized by the time it's used making sure that it will be initialized even for save files made before the script was added.

This also makes sure that you cannot edit the variable outside of the class

Of course for $data_whatever the normal way would work fine, but it's also a good practice to use the other method so that you have uniformity in the way you do things... 🙂
@OP - nope, @durability is an instance variable (notice the @ before the name?), that means every instance of RPG::Weapons has one copy of that variable and is independent of the others.

example:

@durability of $data_weapons[1] is a different variable than @durability of $data_weapons[2]
Oohhhh, everything makes sense now! Thanks, Adiktuzmiko. 🙂
By the way, how can i get the id of the weapon that a x character it's wielding? I was trying something like this $game_actors[1].weapons, but isn't working ):
Well, characters usually have 5 equips : Weapon, Accessory, Headgear, Bodygear and Helmet.

The data abou them is in $game_actors[x].equips

So, for the weapon would be $game_actor[x].equips[0]
I need declare the game actor class to get these values? Cause here in my class, if I tried to print the $game_actor[0].equips[0] or if I tried to put the value get like this

"id_weapon = $game_actor[0].equips[0]",

appears undefined method '[]' for nil:Nil:Class
Sorry, it's $game_actors, not $game_actor.

It depends on what are you trying to achieve.
I wanted to call this $game_actors every time a battle begins, but didn't figure out yet where to put it. I tried something like that:

class Game_Battler < Game_BattlerBase

  alias pheryus_gamebattlerbase_1as3s initialize

    

  def initialize

    get_weapon_from_actor

    pheryus_gamebattlerbase_1as3s

  end

    

  def get_weapon_from_actor

    id_weapon = $game_actors[0].equips[0]

    msgbox(id_weapon)

  end

end

 

When the game starts, a message shows a error of undefined method 'equips'.
Did you try to change that 0 in $game_actors[0].equips[0]?

$game_actors[0] is always a nil:nilClass
ohhh

Now it's giving "SystemStackError occured. stack level too deep"

I think it's because I'm calling the Game_Battler script. My alias it's wrong? I can pass all my code if necessary

[Edit 2]: Now i did it! Fix the errors. The only problem now it's that when I print the $game_actors[1].weapon[0], what really print is the address of the weapon, and not it's ID. Any idea of how can I convert the address to ID?
Um ... there is a method to remove/change equipment. Why don't you just call that, and it will do everything for you?


Look at Game_Interpreter and see what it does when you use the Change Actor Equipment command. Then call the same method in your script.
Oohh, sure, sure.  I'm trying to create my first script by myself, but it's been more harder than I guess hahah I stopped the last error putting a .id in the end of weapon[0]. The only problem now it's that I want to know the weapon that a character is wielding during the Battles, so I'm changing a method in the Game_Battler :

  def make_damage_value(user, item)

    

    BattleManager.idheroatual(BattleManager.actor.id)

    

    dano = item.damage.eval(user, self, $game_variables)

    array_aux = $dano_armas[$id_weapon.to_s]

    for i in 1..array_aux[0].to_i

       dano += Random.rand(array_aux[1].to_i) + 1

     end

    @result.make_damage(dano.to_i, item)

  end

 

What really matters here is the call to the idheroatual method that I created here above:

 

  

class << BattleManager

  def idheroatual(x)

    $id_weapon = $game_actors[x].equips[0].id - 1

  end

end

 

It takes the id of the character that is acting to get the weapon he's wielding. But this isn't really working :\ He was working before I add this x parameter (only geetting the game_actors[1]). I tried change the parameter to BattleManager.actor.index or BattleManager.actor.id.to_i, but didn't succeeded.

 

Thanks for helping me, guys

Original thread: https://forums.rpgmakerweb.com/threads/doubts-about-acessing-data_weapons.43770/