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.

Coordinates error..

In my script I have a part where it takes the enemy ands the player coordinates so that it can do some clacules, but It is giving me an error, nil:nilclass

Code:
@enemy = $game_map.events[@eventid]
@enemyx =  @enemy.x => gives me the error
@enemyy =  @enemy.y =>gives me a error too![code]

I don't know why it gives me a error i just want some help, so that i can try to finish my script.

BTW I know that the event ID is not nill....
 

Nava

Member

it has to be in a space because like he said the variable is @event_id not without the underscore. the underscore serves some purpose i just kant think of what it is right now.....
 
Try inspecting what @enemy is, before you call it out.

p @enemy

My best guess, is that you aren't using the correct event id, so it is pulling a nil object from $game_map.events

like
Code:
a = [4, 5, 7]
b = a[5]

a.x -> nil because there is no fifth element in your array

Just make sure your event id's numbers are correct.
 
This is my script,

Code:
class Object_Handler 
  def initialize
    @enemies = {}
    @springs = {}
    @itens = {}
    @pit={}
   @enemy_move_right = false
   @enemy_move_left = false
 end
 
 def update
    @playerx = $game_player.x if $game_player != nil
    @playery =  $game_player.y if $game_player != nil
    return
  end
  
  
  
#------------------------------------------------------------------------------
#this handles the objec selection it gets the definitions from the platformer
#-----------------------------------------------------------------------------

  def object_selector(object_type, id, opt1, opt2, opt3)
    @eventid = id
    if object_type == "Spring"
      @spring_power = opt1
    elsif object_type == "Enemy"
      @enemy_sight = opt1
      @enemy_move_pattern = opt2
      @enemy_life_points = opt3
      enemy
    elsif object_type = "Itens"
      
    end
  end
  
#------------------------------------------------------------------------------
#Enemy Handler
#-----------------------------------------------------------------------------
  
  def enemy
    @enemy = $game_map.events[@eventid]
    p @eventid
    @enemyx = @enemy.x
    @enemyy = @enemy.y
    @distancex = ((@playerx.to_i - @enemyx.to_i)**2).to_i
    @distancey = ((@playery.to_i - @enemyy.to_i)**2).to_i
    @distance = Math.sqrt(@distancex + @distancey).to_i
    p @distance.to_s
    in_sight?
    enemy_move_patern 
  end
#------------------------------------------------------------------------------
#In sight
#---------------------------------------------------
# Check if The player is in enemy sight!
#-----------------------------------------------------------------------------
  
  def in_sight?
    if @distance - @enemy_sight.to_i <= 0
      return true
    else
      return false
    end
  end
#------------------------------------------------------------------------------
#Enemy movimentation
#---------------------------------------------------
# Check if The player is in enemy sight!
#-----------------------------------------------------------------------------
  def enemy_move_patern 
    if @enemy_move_pattern == "Stay"
       @enemy.move_speed = 0
       @enemy_move_right = false
       @enemy_move_left = false
    elsif @enemy_move_pattern == "Horizontal"
      horizontal_moving
    elsif @enemy_move_pattern == "Custom"
      @enemy.move_type = 3
    else
      @enemy.move_speed = 0
    end
    enemy
  end
#------------------------------------------------------------------------------
#Enemy movimentation
#------------------------------------------------------------------------------
  def horizontal_moving
     @enemy.move_speed = 4
     @enemy.move_frequency = 6
     if $game_map.passable?(@enemy.x+1,@enemy.y, 4) or @enemy_move_left == true
       @enemy.move_right
       @enemy_move_right = true
       @enemy_move_left = false
     else
       if $game_map.passable?(@enemy.x - 1,@enemy.y, 4)
         @enemy.move_left
         @enemy_move_right = false
         @enemy_move_left = true
       else
         @enemy_move_right =false
         @enemy_move_left = false
         return
       end
     end
     enemy
   end
 end#class end
 
 #------------------------------------------------------------------------------
#**Class Game Player
#------------------------------------------------------------------------------   
  class Scene_Map
    
    alias chaosg1_enemy_handler_scene_map_update update
    def update
      chaosg1_enemy_handler_scene_map_update
      for i in 0..$object_handler.size
        next if $object_handler[i] == nil
        $object_handler[i].update
      end
    end
  end
end#sdk end


I think it will be easier to u to see what I have done wrong...
 

Nava

Member

@eventid = id i dont understand that it equals the id of what or maybe im just thinking wrong cuz i just woke up and my brain isnt quite functioning yet....
 

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