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.

i cant solve this error

here is the error i get: "undefined method 'switch' for nil:NilClass" but when you go to my class there it is. the code is duplicate except for a few minor changes, i cant find problem.

this line works:
Code:
if $game_switches[Map_Items::Towns[@pindex].switch] == true
But this one doesnt:
Code:
if $game_switches[Sub::Towns[@pindex].switch] == true
Here are the classes:
Code:
 

module Map_Items

  Towns = {}

end

module Sub

  Towns = {}

end

class Data::Object

  @@ids = {}

  @@containers = {}

  attr_accessor :id

  def initialize

    @@ids[self.class] = 0 unless @@ids.has_key?(self.class)

    @@ids[self.class] += 1

    @id = @@ids[self.class]

    if @@containers.has_key?(self.class)

      @@containers[self.class][@id] = self

    end

  end

  def self.add_container(class_name, container)

    @@containers[class_name] = container

  end

end

class Map_Items::Town < Data::Object

  Data::Object.add_container(self, Map_Items::Towns)

  attr_reader :screen_x

  attr_reader :screen_y

  attr_reader :map_id

  attr_reader :map_x

  attr_reader :map_y

  attr_reader :direction

  attr_reader :icon

  attr_reader :town_name

  attr_reader :switch

  def initialize(screen_x, screen_y, map_id, map_x, map_y, direction, icon, town_name, switch)

   super()

    @screen_x = screen_x

    @screen_y = screen_y

    @map_id = map_id

    @map_x = map_x

    @map_y = map_y

    @direction = direction

    @icon = icon

    @town_name = town_name

    @switch = switch

  end

  def self.screen_x

    return @screen_x

  end 

  def self.screen_y

    return @screen_y

  end 

  def self.map_id

    return @map_id

  end 

  def self.map_x

    return @map_x

  end 

  def self.map_y

    return @map_y

  end 

  def self.direction

    return @direction

  end 

  def self.icon

    return @icon

  end 

  def self.town_name

    return @town_name

  end 

  def self.switch

    return @switch

  end 

end

 

class Sub::Town < Data::Object

  Data::Object.add_container(self, Sub::Towns)

  attr_reader :upper_level

  attr_reader :screen_x

  attr_reader :screen_y

  attr_reader :map_id

  attr_reader :map_x

  attr_reader :map_y

  attr_reader :direction

  attr_reader :icon

  attr_reader :town_name

  attr_reader :switch

  def initialize(screen_x, screen_y, map_id, map_x, map_y, direction, icon, town_name, switch, upper_level)

   super()

    @screen_x = screen_x

    @screen_y = screen_y

    @map_id = map_id

    @map_x = map_x

    @map_y = map_y

    @direction = direction

    @icon = icon

    @town_name = town_name

    @switch = switch

    @upper_level = upper_level

  end

  def self.screen_x

    return @screen_x

  end 

  def self.screen_y

    return @screen_y

  end 

  def self.map_id

    return @map_id

  end 

  def self.map_x

    return @map_x

  end 

  def self.map_y

    return @map_y

  end 

  def self.direction

    return @direction

  end 

  def self.icon

    return @icon

  end 

  def self.town_name

    return @town_name

  end 

  def self.switch

    return @switch

  end 

  def self.upper_level

    return @upper_level

  end 

end
 
Looks like you're trying to read the switch property from a Town that doesn't exist yet.
The problem isn't in the class definition, it's in the code (scene) where you are using those classes. Check the value of @pindex, and make sure you have a Town in the Towns array for that index.
 
the value of pindex is 1. and there are three objects defined.
Code:
Sub::Town.new(380,130,1,8,4,2,"town3","Black Smith",1,"Saar")

Sub::Town.new(100,150,1,8,4,2,"town3","Shop",1,"Saar")

Sub::Town.new(100,150,1,8,4,2,"town3","Castle",1,"Saar")
 
is there more info you need brew? or did you simply not read my reply?
edit: i threw in some print(@pindex.to_s) and i get a blank window. does that mean @pindex = nil? because i set it to 1 when i init.

edit2: gah, i finally managed to trace it out. i had a method i forgot to change Map_Items::Towns to Sub::Towns. so pindex was getting set wrong after i set it to 1.
 

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