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.

Problem with 8 directional movement script

I'm using a 8 way directional movement script in my game, but I just want the player to be able to move diagonally. So I edited the script to only allow diagonal movement. SO far it works, but i have a problem, I can't activate events when walking diagonally. Specifically I can't talk to npc's, events that are set below player can be activated with no problem, but events in front of the player (diagonally) can't be activated when talking to them.
This is the script I'm using.
#==============================================================================
# 8 Way Directional Movement
#------------------------------------------------------------------------------
# Kylock
# 21.3.2008
# Version 1.1
#==============================================================================
# Special thanks to Arrow-1 for the script request and supplying sprites for
# testing purposes.
#==============================================================================
# Instructions:
# Add this script towards the top, since methods are rewritten and not
# aliased. Once you add it and run your game, the script will
# automagically enable 8-way directional movement for your hero.
# Features:
# * Added movement functionality to allow your hero to move in diagonal
# directions
# * Corrects directional issues resulting from trying to activate events
# while facing a diaganol direction. (could be better implemented,
# but does work as it is)
#==============================================================================
# Changelog
# 1.0 Initial Release
# 1.1 Added support for no custom sprites and added compatibility with
# Anaryu's Anti-Lag script (must be loaded above the anti-lag script)
#==============================================================================

#==============================================================================
# ** Script Configuration
#==============================================================================
module KMDM
DIRSPRITE = false # set to true if you are using a custom
# character sprite for diagonal movement
end

#==============================================================================
# ** Game_Player
#------------------------------------------------------------------------------
# rewrote def move_by-input to enable extra directions
#==============================================================================

class Game_Player < Game_Character
def move_by_input
return unless movable?
return if $game_map.interpreter.running?
case Input.dir8
#when 1; move_lower_left
when 2; move_lower_left
#when 3; move_lower_right
when 4; move_upper_left
when 6; move_lower_right
#when 7; move_upper_left
when 8; move_upper_right
#when 9; move_upper_right
end
end
end

#==============================================================================
# ** Game_Character
#------------------------------------------------------------------------------
# Corrects direction so events will respond when hero is facing diagonal.
# Modifies: def move_lower_left, def move_lower_right, def move_upper_left,
# and def move_upper_right
#==============================================================================

class Game_Character
#--------------------------------------------------------------------------
# * Move Lower Left
#--------------------------------------------------------------------------
def move_lower_left(turn_ok = true)
set_direction(3)
if (passable?(@x, @y+1) and passable?(@x-1, @y+1)) or
(passable?(@x-1, @y) and passable?(@x-1, @y+1))
@x -= 1
@y += 1
increase_steps
@move_failed = false
else
@move_failed = true
end
end
#--------------------------------------------------------------------------
# * Move Lower Right
#--------------------------------------------------------------------------
def move_lower_right(turn_ok = true)
set_direction(7)
if (passable?(@x, @y+1) and passable?(@x+1, @y+1)) or
(passable?(@x+1, @y) and passable?(@x+1, @y+1))
@x += 1
@y += 1
increase_steps
@move_failed = false
else
@move_failed = true
end
end
#--------------------------------------------------------------------------
# * Move Upper Left
#--------------------------------------------------------------------------
def move_upper_left(turn_ok = true)
if KMDM::DIRSPRITE == true
set_direction(5)
else
set_direction(5)
end
if (passable?(@x, @y-1) and passable?(@x-1, @y-1)) or
(passable?(@x-1, @y) and passable?(@x-1, @y-1))
@x -= 1
@y -= 1
increase_steps
@move_failed = false
else
@move_failed = true
end
end
#--------------------------------------------------------------------------
# * Move Upper Right
#--------------------------------------------------------------------------
def move_upper_right(turn_ok = true)
set_direction(9)
if (passable?(@x, @y-1) and passable?(@x+1, @y-1)) or
(passable?(@x+1, @y) and passable?(@x+1, @y-1))
@x += 1
@y -= 1
increase_steps
@move_failed = false
else
@move_failed = true
end
end
end

#==============================================================================
# ** Sprite_Character
#------------------------------------------------------------------------------
# Adds functionality for diagonal character sprites.
# Modifies: def update_src_rect
#==============================================================================

class Sprite_Character < Sprite_Base
#--------------------------------------------------------------------------
# * Update Transfer Origin Rectangle
#--------------------------------------------------------------------------
def update_src_rect
if @tile_id == 0
index = @character.character_index
#if the character is facing diagonally, use the second spriteset
if @character.direction % 2 != 0 && KMDM::DIRSPRITE == true
index = @character.character_index + 1
end
pattern = @character.pattern < 3 ? @character.pattern : 1
sx = (index % 4 * 3 + pattern) * @cw
sy = (index / 4 * 4 + (@character.direction - 2) / 2) * @ch
self.src_rect.set(sx, sy, @cw, @ch)
end
end
end

Can anyone help me and tell me why I can't talk to npc's I hope someone can help me... :(
Or if someone knows of any opther 8 directional movement that can activate events by standing in frong of them to an npc.
 
try this code below your 8 directional script:
[rgss]class Game_Player
  #--------------------------------------------------------------------------
  # * Front Event Starting Determinant
  #--------------------------------------------------------------------------
  def check_event_trigger_there(triggers)
    result = false
    # If event is running
    if $game_system.map_interpreter.running?
      return result
    end
    # Calculate front event coordinates
    new_x = @x + (@direction == 6 ? 1 : @direction == 4 ? -1 :
                  @direction == 1 ? -1 : @direction == 3 ? +1 : @direction == 7 ? -1 :
                  @direction == 9 ? +1 : 0)
    new_y = @y + (@direction == 2 ? 1 : @direction == 8 ? -1 :
                  @direction == 1 ? +1 : @direction == 3 ? +1 : @direction == 7 ? -1 :
                  @direction == 9 ? -1 : 0)
    # All event loops
    for event in $game_map.events.values
      # If event coordinates and triggers are consistent
      if event.x == new_x and event.y == new_y and
         triggers.include?(event.trigger)
        # If starting determinant is front event (other than jumping)
        if not event.jumping? and not event.over_trigger?
          event.start
          result = true
        end
      end
    end
    # If fitting event is not found
    if result == false
      # If front tile is a counter
      if $game_map.counter?(new_x, new_y)
        # Calculate 1 tile inside coordinates
        new_x += (@direction == 6 ? 1 : @direction == 4 ? -1 :
                  @direction == 1 ? -1 : @direction == 3 ? +1 : @direction == 7 ? -1 :
                  @direction == 9 ? +1 : 0)
        new_y += (@direction == 2 ? 1 : @direction == 8 ? -1 :
                  @direction == 1 ? +1 : @direction == 3 ? +1 : @direction == 7 ? -1 :
                  @direction == 9 ? -1 : 0)
        # All event loops
        for event in $game_map.events.values
          # If event coordinates and triggers are consistent
          if event.x == new_x and event.y == new_y and
             triggers.include?(event.trigger)
            # If starting determinant is front event (other than jumping)
            if not event.jumping? and not event.over_trigger?
              event.start
              result = true
            end
          end
        end
      end
    end
    return result
  end
end
[/rgss]

This is taken directly from my 8 way movement script. It is how I was able to interact with events from all eight directions.

--Draycos Goldaryn
 

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