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.

NoMethodError...

OS

Sponsor

Hello. I seem to have done something wrong when writing these scripts.

Code:
class Game_NPCs < NPC_Database
  NPC_COUNT = 2
  #--------------------------------------------------------------------------
  # * Object Initialization
  #--------------------------------------------------------------------------
  def initialize
    @data = []
  end
  #--------------------------------------------------------------------------
  # * Get NPC
  #     npc_id : npc ID
  #--------------------------------------------------------------------------
  def [](npc_id)
    if npc_id > NPC_COUNT or @npcs[npc_id] == nil
      return nil
    end
    if @data[npc_id] == nil
      @data[npc_id] = Game_NPC.new(npc_id)
    end
    return @data[npc_id]
  end
end
Code:
class Game_NPC < NPC_Database
  attr_accessor :id
  attr_accessor :surname
  attr_accessor :middle
  attr_accessor :name
  attr_accessor :age #Used in same-sex marriages (Checked for oldest)
  attr_accessor :sex #gender
  attr_accessor :marriage
  attr_accessor :disp #Explained later
  attr_accessor :bounty #Explained later
  attr_accessor :lvl #used in same-sex marriages and battles (Checked for highest)
  attr_accessor :hp #used in battle
  attr_accessor :items #an array that holds up to 5 items.
  attr_accessor :clothes #an array that holds Body, Head, Hand, Feet, and Shield Equiped armour.
  attr_accessor :weapon #a variable that holds 1 weapon
 #attr_accessor :skills #an array holding any (5) skills/spells the NPC knows
 #attr_accessor :mp
 #MARRIAGE_MIN = 18 #The youngest age an NPC can be to get married
 def initialize(npc_id)
   super()
   setup(npc_id)
 end
 
 def setup(npc_id)
  @npc_id = npc_id
  @surname = @npc[i][0]
  @middle = @npc[i][1]
  @name = @npc[i][2]
  @age = @npc[i][3]
  @sex = @npc[i][4]
  @disp = @npc[i][5]
  @bounty = @npc[i][6]
  @lvl = @npc[i][7]
  @hp = 100 #Percentages only
  @items = @npc[i][8]
  @clothes = @npc[i][9]
  @weapon = @npc[i][10]
 end
 
 def id
   return @npc_id
 end
end

I'm getting a NoMethodError, undifined method `[]' for Nil:nilClass on line 3 for this little test when using the above scripts.
Code:
class Scene_NPCTest
  def initialize
    @e = $game_npc[1].id
  end
  
  def main
    print @e
    print $game_npc[0].id
    $scene = Scene_Map.new
  end
end

If somebody could help me, I'd be grateful.
Thankee.
Peace Out!
~Broken
 
1) $game_npc is probably nil did you set $game_npc to an instance of the class Game_NPCs

2) if not one then this is causing it
Code:
@npcs[npc_id]

also what is class NPC_Database?, seems a bit unnecessary to me
 
^^I have Started making a script similar to this one.

It would handle all NPC Interactions, like they would hang together, like some npc dislike other, predifined movements, like go home shop or etc...

BuT my pc crashed and I need to format and I lost everything... Also I would need a new pathfinding routine since it was pretty laggie.
 

OS

Sponsor

My script is just designed so that NPCs can become married, can have bounties, and react to the player based on thier disposition. I am also trying to make it so that NPCs can hold items, armour, and a weapon, and trade with other NPCs, the player, and Containers (after I finish Game_OBJ).

EDIT: @Trickster: NPC_Database is where the Developer creates the NPCs. I accidently forgot to post that script, so here it is, as well as the Scene_Title Edit:

Code:
class NPC_Database
  #NPC_COUNT = 2
  def initialize
   #item# = [five item ids]
    item1 = [1, 1, 1, 2, 3]
   #clothes# = [shield, helmit, body_armour, accessory]
    clothes1 = [1, 10, 22, 32]
   #var = [0last_name, 1middle_name, 2first_name, 3age, 4sex, 5marriage, 6disp, 7bounty, 8lvl, 9items, 10clothes, 11weapon]
    n1 = ["Testons", "Testing", "Testy", 14, 1, [false, ], 50, 0, 16, item1, clothes1, 1]
    n2 = ["Killer", "The", "Im", 17, 0, 0, 1000, 23, [1, 1, 1, 2, 3], [1, 10, 22, 32], 1]
    @npc = [n1, n2]
  end
end
Code:
class Scene_Title
  def command_new_game
    # Play decision SE
    $game_system.se_play($data_system.decision_se)
    # Stop BGM
    Audio.bgm_stop
    # Reset frame count for measuring play time
    Graphics.frame_count = 0
    # Make each type of game object
    $game_temp          = Game_Temp.new
    $game_system        = Game_System.new
    $game_switches      = Game_Switches.new
    $game_variables     = Game_Variables.new
    $game_self_switches = Game_SelfSwitches.new
    $game_screen        = Game_Screen.new
    $game_actors        = Game_Actors.new
    $game_party         = Game_Party.new
    $game_troop         = Game_Troop.new
    $game_map           = Game_Map.new
    $game_player        = Game_Player.new
    $game_npcs          = Game_NPCs.new   #NEW LINE HERE*************************
    # Set up initial party
    $game_party.setup_starting_members
    # Set up initial map position
    $game_map.setup($data_system.start_map_id)
    # Move player to initial position
    $game_player.moveto($data_system.start_x, $data_system.start_y)
    # Refresh player
    $game_player.refresh
    # Run automatic change for BGM and BGS set with map
    $game_map.autoplay
    # Update map (run parallel process event)
    $game_map.update
    # Switch to map screen
    $scene = Scene_Map.new
  end
end

Peace Out!
~Broken
 

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