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.

[Resolved] $game_map.events[i].x -> Undefined method

Sorry if this has been asked before, but I didn't find any satisfactory answer with all search means for...an hour? It's getting annoying...for a small thing.

So basicly there is this :

Code:
for i in 0..$numberenemy
      @eventx = $game_map.events[i].x #<--- it crashes here
      @eventy = $game_map.events[i].y
      if $game_player.x - @eventx.abs < 14
        #do something
      elsif $game_player.y - @eventy.abs < 11
        #do something
      end
    end

NoMethodError Occurred

undefined method 'x' for Nil:NilClass

This problem doesn't happen if I replace "i" by an actual number. Which is not a solution.
 
It means $game_map.events is returning nil. Chances are because you are starting with 0. You need to start with 1.

Code:
for event in $game_map.events.values
      @eventx = event.x
      @eventy = event.y
      if $game_player.x - @eventx.abs < 14
        #do something
      elsif $game_player.y - @eventy.abs < 11
        #do something
      end
    end
 

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