Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

@weapon_id not being recognised within an array

Taylor

Sponsor

This is a rather complicated question, because I am not quite sure what the issue is.

Code:
<span style="color:#000080; font-style:italic;">=begin

<span style="color:#000080; font-style:italic;">╔═════════════════════════════════════════════════════════════════════════════╗

<span style="color:#000080; font-style:italic;">║ Stamina_BATTLE                                      Weapon and Skill System ║

<span style="color:#000080; font-style:italic;">╚═════════════════════════════════════════════════════════════════════════════╝

<span style="color:#000080; font-style:italic;">=end

 

module Jaabi

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

  # * Custom Actor Data

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

  # Use:

  # Defines what type of weapon an actor uses

  # Format:

  # Actor ID = "type"

  ACTORW_TYPE   = { 1 => "Ring",  

                    2 => "Arrow", 

                    }

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

  # * Custom Weapon/Skill Data

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

  # Use:

  # Defines weapon types

  # Format:

  # Weapon ID = "type"

  WEAPON_TYPE   = {   1 => "Ring",

                      2 => "Ring",

                     13 => "Arrow", 

                     }

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

  # * Weapon Skill Sets

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

  # Use:

  # Defines what skills are learnt from weapons

  # Format:

  # Weapon ID => [ Skill ID, Skill ID]

  WEAPON_SKILLS = { 1 => [   1,   2,   3,   4,   5,   6,],

                    2 => [   7,   8,   9,  10,  11,  12,],

                    }

  # Use:

  # Defines when the skills are learnt, based on the skills

  # above. As such the EXP must be ordered the same as the

  # skills above.

  WSKILL_EXP    = { 1 => [   0, 100, 300, 500, 800, 900,],

                    2 => [   0, 100, 300, 500, 800, 900,],

                    }

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

  # * Defaults

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

  ACTORW_TYPE.default = { 0 => 0, }

  WEAPON_TYPE.default = { 0 => 0, }

  WEAPON_SKILLS.default = { 0 => [ 0, 0, 0, 0, 0, 0,], }

  WSKILL_EXP.default = { 0 => [ 0, 0, 0, 0, 0, 0,], }

end

 

#==============================================================================

# ** Game_Actor

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

#  This class handles actors. It's used within the Game_Actors class

# ($game_actors) and referenced by the Game_Party class ($game_party).

#==============================================================================

 

class Game_Actor

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

  # * Object aliasing

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

  alias jaabi_sbbs_actor_init initialize

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

  # * Public Instance Variables

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

  attr_reader :weapon_ms

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

  # * Weapon Mastery

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

  def initialize(actor_id)

    jaabi_sbbs_actor_init(actor_id)

    @weapon_ms = { 0 => 0,}

  end  

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

  # * Get Skill Object Array

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

  def skills

    result = []

    for i in @skills

      result.push($data_skills[i])

    end

    unless @weapon_id == 0

      if defined? @weapon_ms[@weapon_id] == false

        @current_weap = { @weapon_id => 1, }

        @current_weap.merge!(@weapon_ms)

      end

      for w in 0..5

        if @weapon_ms[@weapon_id][w] >= Jaabi::WSKILL_EXP[@weapon_id][w]

          result.push($data_skills[Jaabi::WEAPON_SKILLS[@weapon_id][w]])

        else

          result.push(nil)

        end

      end

    end

    return result

  end

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

  # * Determine Usable Skills

  #     skill : skill

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

  def skill_can_use?(skill)

    #return false unless skill_learn?(skill)

    return super

  end

end

 
This is a custom skill learning system I have been working on for my project. It's pretty straight forward - weapons give skills and certain skills are granted depending on a weapon's "mastery".

@weapon_ms = { 0 => 0,} is the "mastery" array.

I want to add a weapon to the array when an actor equips it, thus initializing a character's mastery of a weapon.
if defined? @weapon_ms[@weapon_id] == false
@current_weap = { @weapon_id => 1, }
@current_weap.merge!(@weapon_ms)
end
This is assume is how I go about doing it.

However I get an error on this line:
if @weapon_ms[@weapon_id][w] >= Jaabi::WSKILL_EXP[@weapon_id][w]
Undefined method '[]' for nil:NilClass.

I suspect that it's trying to add @weapon_id => 1 to the array, rather than the result of @weapon_id to the array. Like 1 => 1,.
Alternatively, if defined? @weapon_ms[@weapon_id] == false is seeing @weapon_ms as being defined, when I want to check if the current @weapon_id is already in the array.

Creating the separate mastery points gaining system sounds easy, but if my second theory is true, I am not sure how I am meant to "update" the appropriate array item's value. (e.g 1 => 1, weapon 1's mastery to 1 => 21 - adding 20 points.)

Note: This worked before when I used normal exp. However I remembered that I wanted a character to be able to master weapons individually (mastery is passed between same weapons intentionally - e.g bronze swords; not swords as a whole). So I tried creating an array that I hoped would add to itself.

The actor and weapon "type" system is not yet sorted out. Ignore it for now.


EDIT: Oh yeah. This is RMVX. Forgot to mention that. XD;
 
Since it is a nilclass error, and I noticed that you didn't check to see if @weapon_ms[@weapon_id][w] is nil, you might want to check that. Currently, I see no way it could be anything other than nil, since your test hash contains an integer instead of an array. First, I would recommend doing one or both of two things. First, set up an unless statement to check to see if @weapon_ms[@weapon_id] is set to 0, and have it skip if that is true. Second, put the following lines as the first few lines within the loop where you are encountering your error:

Code:
next unless @weapon_ms[@weapon_id].is_a? Array

next if @weapon_ms[@weapon_id][w] == nil

The first line will check for the specific error you appear to be encountering, and skip that iteration of the loop of that is the case. The next line I added is to cover the next potential error you would encounter, where you are looking for an array value that does not exist. Also, I would recommend using .to_s.to_i to convert the array values to integers, if you receive an error regarding that. (It looks like you might, but I'm not entirely certain at this point)
 

Taylor

Sponsor

Thanks for that. It turns out there were a few more issues too.

I turned attr_reader :weapon_ms into attr_accessor :weapon_ms, and swapped around the merge! line. (I was merging the old array into the new one, rather the new into the old.)

In the end I found I didn't need the two 'next' lines.

I'll need to remember about the attr_ commands next time. = -=a
 
Jirbytaylor":2ajl9ddm said:
Thanks for that. It turns out there were a few more issues too.

I turned attr_reader :weapon_ms into attr_accessor :weapon_ms, and swapped around the merge! line. (I was merging the old array into the new one, rather the new into the old.)

In the end I found I didn't need the two 'next' lines.

I'll need to remember about the attr_ commands next time. = -=a

Actually, the second of the next lines is still recommended, in case someone uses the script and doesn't set it up properly. You can't assume that everyone knows what they're doing, and have to assume the worst.
 

Taylor

Sponsor

True yes, but I don't intend to release the script any time soon. It's mainly for my personal CBS.

For one, if I do release it some lines need seeing to. Specifically for w in 0..5. I can't assume all weapons will give at the most six skills. ^^;
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top