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.

Player Detection with Events only

Clad

Member

Hi
I am starting an action rpg game project and I have a question for Rpgmaker XP:

Enemy will detect if Player is 3 tiles away from it in any direction and will move towards Player if Player is within this range. Like in Secret of Mana, Legend of Mana or Secret of Evermore, when the enemies "sees" the hero at a particular distance, they rush to attack him. Can anyone help on how can I make the Enemy calculates and detects Player? I would like to do this only with Events without any scripts.


Thank you for your help!!
 

Clad

Member

the enemy will move at random direction and once player is out of its range, it just stop following player and continue its random movement. Is there a script demo or example that you know of?
 
I made one, but all my progress was lost when my other computer crashed. I'll recreate it for you.

Edit. Here it is:
Code:
 

class Game_Event < Game_Player

    alias tdks_orig_init initialize

    def initialize(map_id, event)

        super(map_id, event)

        if @event.name.sub!(/\(chase\, \d+\, \d+\, \d\)/,"")

            @s_range = $1

            @e_range = $2

            @move_change = $3

        elsif @event.name.sub!(/\(chase\, \d+\, \d\)/,"")

            @s_range, @e_range = $1, $1

            @move_change = $2

        end

    end

    

    alias tdks_orig_update update

    def update

        if @move_change and !@stored and @move_type != @move_change

            dx = (@x - $game_player.x).abs

            dy = (@y - $game_player.y).abs

            distance = Math.sqrt(dx ** 2.0 + dy ** 2.0)

            if distance <= @s_range

                @move_type, @stored = @move_change, @move_type

            end

        elsif @move_change and @stored

            dx = (@x - $game_player.x).abs

            dy = (@y - $game_player.y).abs

            distance = Math.sqrt(dx ** 2.0 + dy ** 2.0)

            if distance > @e_range

                @move_type, @stored = @stored, nil

            end

        end

        super

    end

end

 

To use, put:
Code:
(chase, range, new_move_type)
in the event name, where range is the range from the player at which the event's movement should change, and new_move_type is the movement type to change to (0 - fixed, 1 - random, 2 - approach player).
 

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