Group: Member
Posts: 47
Type: Developer
RM Skill: Skilled
This script is done by Gubid, not me so credit him. I am simply posting it.
This script is something I found when I was making my game and it's a nice little script. It is really easy to use as well. Basicly it will make an enemy chase you, or run at you when your in their view. You can change the speed of the enemy and how far they can see and more in the script. You can also make them return to there origonal position and continue any "patrols" or watever that you set.
To make the enemy react when they see you just simply name the event 'enemy'
sorry, I haven't got a screen shot yet. If you want a demo i could send you one. Just email me at leozer0@hotmail.com
#-------------------------------------------------------------------------- # Enemy Detection System # by Gubid - 8/12/07 # v 1.2.2 Updated 10/2/07 #-------------------------------------------------------------------------- #Description: #This system allows events that are named something including "ENEMY" then #that event will be told to approach Game_Player, until Game_Player has gotten #a specified distance away. At that time event will return to its original #position if desired. If you happen to get close enough to the returning event #it will begin to follow you again. # #Thanks to Near Fantasia for supplying the slightly modified path finding script. #-------------------------------------------------------------------------- #if there are any questions as to the use of this script or #modifications/additions/etc, please post it to the thread on CreationAyslum.net #--------------------------------------------------------------------------
#-------------------------------------------------------------------------- #Update_Detect - How to Stop Detection if desired #-------------------------------------------------------------------------- #1. Within a event, add a SCRIPT (3rd page) #2. type "update_detect(EVENT_ID, TRUE/FALSE)" -Note: it defaults to false
#-------------------------------------------------------------------------- #RETURN TO POSITION #-------------------------------------------------------------------------- #Use this to determine if you would like the event to return to its original #position or not. #-------------------------------------------------------------------------- RETURN_TO_POSITION = true
#-------------------------------------------------------------------------- #CHASE_SPEED and ENABLE_SPEED_CHANGE #-------------------------------------------------------------------------- #Use this to set the speed in which enemies will approach after spotting you #and whether or not their speed will change. #-------------------------------------------------------------------------- CHASE_SPEED = 5 ENABLE_SPEED_CHANGE = true
#-------------------------------------------------------------------------- #VIEW_RANGE #-------------------------------------------------------------------------- #Use this to set the distance in which you must be in order to start the detection #process, or escape to stop. #-------------------------------------------------------------------------- VIEW_RANGE = 5
#-------------------------------------------------------------------------- #ESCAPE_RANGE #-------------------------------------------------------------------------- #Use this to set the distance in which you must escape to stop the detection process #-------------------------------------------------------------------------- ESCAPE_RANGE = 9
#-------------------------------------------------------------------------- #Other Notes #-------------------------------------------------------------------------- #If the event has a custom move route, they will resume that route when finished #chasing you, but if you dont set RETURN_TO_POSITION, then they will resume #their custom route at the location they stopped chasing you.
class Game_Map #-------------------------------------------------------------------------- # * Frame Update #-------------------------------------------------------------------------- alias en_detect_gm_update update def update en_detect_gm_update
# Update map event for event in @events.values if !event.name.include?("enemy") next else if event.character_name != "" if event.stop_detect? next end if check_distance(event.id) == true if event.exclamation == false event.old_type = event.move_type event.move_type = 2 if ENABLE_SPEED_CHANGE event.old_speed = event.move_speed event.move_speed = CHASE_SPEED end event.exclamation = true event.animation_id = 98 #98 happens to be the exclamation animation event.f_route.clear end else if event.exclamation == true event.exclamation = false if RETURN_TO_POSITION == true if event.x != @map.events[event.id].x or event.y != @map.events[event.id].y route = get_route(event, @map.events[event.id].x, @map.events[event.id].y) event.run_route(route) end else event.restore_route end end end end end end end
def check_distance(id = nil) unless id == nil event = $game_map.events[id] range = ($game_player.x - event.x).abs + ($game_player.y - event.y).abs
if range <= VIEW_RANGE and range > 0 return true elsif range <= ESCAPE_RANGE and range > 0 and event.exclamation return true end end end
def get_route(event, x=0, y=0) target_x = x target_y = y max = (target_x - event.x).abs + (target_y - event.y).abs + 5 position = [[event.x, event.y]] route = [[]] more_step = [0] for i in more_step x = position[i][0] y = position[i][1] if !position.include?([x, y + 1]) and event.passable?(x, y, 2) position.push([x, y + 1]) route.push(route[i] + [2]) if route[i].size + 1 < max more_step.push(route.index(route[i] + [2])) end end
if !position.include?([x - 1, y]) and event.passable?(x, y, 4) position.push([x - 1, y]) route.push(route[i] + [4]) if route[i].size + 1 < max more_step.push(route.index(route[i] + [4])) end end
if !position.include?([x + 1, y]) and event.passable?(x, y, 6) position.push([x + 1, y]) route.push(route[i] + [6]) if route[i].size + 1 < max more_step.push(route.index(route[i] + [6])) end end
if !position.include?([x, y - 1]) and event.passable?(x, y, 8) position.push([x, y - 1]) route.push(route[i] + [8]) if route[i].size + 1 < max more_step.push(route.index(route[i] + [8])) end end end if position.index([target_x, target_y]) == false return [2] else return route[position.index([target_x, target_y])] end end end
alias en_det_update update def update if @f_route.size > 0 run_path unless self.moving? if @f_route.size == 0 restore_route end end en_det_update end
def run_route(route) @f_route = route @step = 0 end
def run_path if @f_route.size > 0 if @f_route[@step] == nil @f_route = [] end action = @f_route[@step] @step += 1 case action when 2 self.move_down when 4 self.move_left when 6 self.move_right when 8 self.move_up end end end end
class Interpreter def update_detect(id, set = false) $game_map.events[id].stop_trigger = set end
def spotted? is_true = 0 for event in $game_map.events.values if event.exclamation is_true += 1 end end if is_true > 0 return true else return false end end
def change_switch(id = 0, value = false, type = 0, switch = "A") if id > 0 if type == 0 #for use of main switches #id is switch_id and value should set to true or false $game_switches[id] = value elsif type == 1 #For use of self switches #id is event.id and switch is the desired selfswitch. It must be text! #value is true or false key = [$game_map.map_id, id, switch] # Updates self switch $game_self_switches[key] = value end # Refresh map $game_map.need_refresh = true return true end end end
Group: Revolutionary
Posts: 166
Type: Artist
RM Skill: Skilled
Can't believe no one didn't say this yet.
Anyway, thanks a lot, this really helped me a lot with my lag problem, because I was doing this by events. The script works very well, it's meant to make a event follow, leaving the rest up to us. Which just the way I like it.
__________________________
The Huhja High School martial arts club's captain, known as one of the strongest martial artist captains has passed away last year. Prove of his death, and rumors about the ordinary club members leaving had surfaced to the other schools that are in rivalry to them. They begun taking up the Huhja's turfs and claiming to take over the school next.
The Huhja's martial arts club has now disbanded after a crushing defeat at their turfs.The school's morale quickly started disappearing... Till an quiet first year girl joins the school. This young, quiet, small girl may look weak and helpless, but her martial art style is unseen. With her help, Huhja High must revive the club, gain new allies, discover hidden fighting arts, restore the school's morale, take back their turfs, and defeat their powerful rivals.
A game inspired by: Ranma 1/2, Persona, and Slice-of-Life Animes.
Yes this script seems like it should be great. I'm in the early stages of development in my game and have just finished the intro with all the starting cutscenes and waste of time speech. When I get to more parts of the game when I know I'll require this, I'll be using your script. So good job.
So basically all you gotta do is make like a monster event and name it enemy and it will follow you around if you go near it?
__________________________
The final release of Project Viral is finally here!
I was just wondering whether it would be possible to set different detection/escape areas for different enemies, as well as different chase speeds. I thought of using $event.animation_id = whatever for different things, but as it would change the whole script that would probably affect all the enemies in the vicinity as well, if it worked at all. (I didn't try it XD)
But if anyone knows how this could be worked out, like altering the script so you can set different things in comments or something, that would be really helpful.
Very sorry for the double post, but I would really appreciate it if someone could edit (or even just tell me how) the script so that you can use Call Script or Comments to set different values for different events. It would help me out immensely and would undoubtedly make this great script even greater
I'm putting enemies on the map that either wander randomly or follow a custom path, but I want them to move at different speeds and change to different chase speeds. If it's possible, I'd like to be able to change the Chase Frequency for them as well. The path finding and return to position things work fine, as does the detect on/off thing (forgive my lack of the appropriate terminology).
Group: Member
Posts: 47
Type: Developer
RM Skill: Skilled
QUOTE (NekoMage @ May 4 2009, 08:05 AM)
Very sorry for the double post, but I would really appreciate it if someone could edit (or even just tell me how) the script so that you can use Call Script or Comments to set different values for different events. It would help me out immensely and would undoubtedly make this great script even greater
I'm putting enemies on the map that either wander randomly or follow a custom path, but I want them to move at different speeds and change to different chase speeds. If it's possible, I'd like to be able to change the Chase Frequency for them as well. The path finding and return to position things work fine, as does the detect on/off thing (forgive my lack of the appropriate terminology).
Hey. Sorry that I haven't replied for SOOO long. I forgot about this script until i needed it for my new game. Anyway . . . I can't help you there, I'm afraid. I didn't make this script, I'm just posting it here cause I thought someone would like to use it. All credit should go to Gubid. If you have questions, you can find and ask him/her. Sorry.
Maybe the ones that needed it knew how to do it but never posted it because no one asked for it.
I've added frequency to function the same as move speed. I'd like to be able to customize the speed and freq for the specific event, but the design doesn't make it very easy to accomplish.
Additional constants: CHASE_FREQ, ENABLE_FREQ_CHANGE Pretty self-explanatory. Frequencies are from 1 (lowest) to 6 (highest).
Added frequency update for restore_route method Additional if clause for the update method.
Updated with frequency
CODE
#-------------------------------------------------------------------------- # Enemy Detection System # by Gubid - 8/12/07 # v 1.2.2 Updated 10/2/07 # v 1.2.3 Updated 11/09/28 #-------------------------------------------------------------------------- #Description: #This system allows events that are named something including "ENEMY" then #that event will be told to approach Game_Player, until Game_Player has gotten #a specified distance away. At that time event will return to its original #position if desired. If you happen to get close enough to the returning event #it will begin to follow you again. # #Thanks to Near Fantasia for supplying the slightly modified path finding script. #-------------------------------------------------------------------------- #if there are any questions as to the use of this script or #modifications/additions/etc, please post it to the thread on CreationAyslum.net #--------------------------------------------------------------------------
#-------------------------------------------------------------------------- #Update_Detect - How to Stop Detection if desired #-------------------------------------------------------------------------- #1. Within a event, add a SCRIPT (3rd page) #2. type "update_detect(EVENT_ID, TRUE/FALSE)" -Note: it defaults to false
#-------------------------------------------------------------------------- #RETURN TO POSITION #-------------------------------------------------------------------------- #Use this to determine if you would like the event to return to its original #position or not. #-------------------------------------------------------------------------- RETURN_TO_POSITION = false
#-------------------------------------------------------------------------- #CHASE_SPEED and ENABLE_SPEED_CHANGE #-------------------------------------------------------------------------- #Use this to set the speed in which enemies will approach after spotting you #and whether or not their speed will change. #------------------------------------------------------------------------- CHASE_SPEED = 4 ENABLE_SPEED_CHANGE = true
#-------------------------------------------------------------------------- #CHASE_FREQUENCY and ENABLE_FREQUENCY_CHANGE #------------------------------------------------------------------------- #Use this to set the frequency in which enemies will approach after spotting #you and whether or not the frequency will change. #------------------------------------------------------------------------- CHASE_FREQ = 6 ENABLE_FREQ_CHANGE = true
#-------------------------------------------------------------------------- #VIEW_RANGE #-------------------------------------------------------------------------- #Use this to set the distance in which you must be in order to start the detection #process, or escape to stop. #-------------------------------------------------------------------------- VIEW_RANGE = 5
#-------------------------------------------------------------------------- #ESCAPE_RANGE #-------------------------------------------------------------------------- #Use this to set the distance in which you must escape to stop the detection process #-------------------------------------------------------------------------- ESCAPE_RANGE = 7
#-------------------------------------------------------------------------- #Other Notes #-------------------------------------------------------------------------- #If the event has a custom move route, they will resume that route when finished #chasing you, but if you dont set RETURN_TO_POSITION, then they will resume #their custom route at the location they stopped chasing you.
class Game_Map #-------------------------------------------------------------------------- # * Frame Update #-------------------------------------------------------------------------- alias en_detect_gm_update update def update en_detect_gm_update
# Update map event for event in @events.values if !event.name.include?("enemy") next else if event.character_name != "" if event.stop_detect? next end if check_distance(event.id) == true if event.exclamation == false event.old_type = event.move_type event.move_type = 2 if ENABLE_SPEED_CHANGE event.old_speed = event.move_speed event.move_speed = CHASE_SPEED end if ENABLE_FREQ_CHANGE event.old_frequency = event.move_frequency event.move_frequency = CHASE_FREQ end event.exclamation = true event.animation_id = 98 #98 happens to be the exclamation animation event.f_route.clear end else if event.exclamation == true event.exclamation = false if RETURN_TO_POSITION == true if event.x != @map.events[event.id].x or event.y != @map.events[event.id].y route = get_route(event, @map.events[event.id].x, @map.events[event.id].y) event.run_route(route) end else event.restore_route end end end end end end end
def check_distance(id = nil) unless id == nil event = $game_map.events[id] range = ($game_player.x - event.x).abs + ($game_player.y - event.y).abs
if range <= VIEW_RANGE and range > 0 return true elsif range <= ESCAPE_RANGE and range > 0 and event.exclamation return true end end end
def get_route(event, x=0, y=0) target_x = x target_y = y max = (target_x - event.x).abs + (target_y - event.y).abs + 5 position = [[event.x, event.y]] route = [[]] more_step = [0] for i in more_step x = position[i][0] y = position[i][1] if !position.include?([x, y + 1]) and event.passable?(x, y, 2) position.push([x, y + 1]) route.push(route[i] + [2]) if route[i].size + 1 < max more_step.push(route.index(route[i] + [2])) end end
if !position.include?([x - 1, y]) and event.passable?(x, y, 4) position.push([x - 1, y]) route.push(route[i] + [4]) if route[i].size + 1 < max more_step.push(route.index(route[i] + [4])) end end
if !position.include?([x + 1, y]) and event.passable?(x, y, 6) position.push([x + 1, y]) route.push(route[i] + [6]) if route[i].size + 1 < max more_step.push(route.index(route[i] + [6])) end end
if !position.include?([x, y - 1]) and event.passable?(x, y, 8) position.push([x, y - 1]) route.push(route[i] + [8]) if route[i].size + 1 < max more_step.push(route.index(route[i] + [8])) end end end if position.index([target_x, target_y]) == false return [2] else return route[position.index([target_x, target_y])] end end end
alias en_det_update update def update if @f_route.size > 0 run_path unless self.moving? if @f_route.size == 0 restore_route end end en_det_update end
def run_route(route) @f_route = route @step = 0 end
def run_path if @f_route.size > 0 if @f_route[@step] == nil @f_route = [] end action = @f_route[@step] @step += 1 case action when 2 self.move_down when 4 self.move_left when 6 self.move_right when 8 self.move_up end end end end
class Interpreter def update_detect(id, set = false) $game_map.events[id].stop_trigger = set end
def spotted? is_true = 0 for event in $game_map.events.values if event.exclamation is_true += 1 end end if is_true > 0 return true else return false end end
def change_switch(id = 0, value = false, type = 0, switch = "A") if id > 0 if type == 0 #for use of main switches #id is switch_id and value should set to true or false $game_switches[id] = value elsif type == 1 #For use of self switches #id is event.id and switch is the desired selfswitch. It must be text! #value is true or false key = [$game_map.map_id, id, switch] # Updates self switch $game_self_switches[key] = value end # Refresh map $game_map.need_refresh = true return true end end end
This post has been edited by Tsukihime: Sep 28 2011, 10:09 AM
If it were me I'd at least put the freq and speed in the event name so that if such values exist those would be used, otherwise use the defaults specified by the constants.
I could probably change it so you could specify move speed and frequency in the event name since there are other scripts that look at the name, but then it would change the requirements of the name. I'd probably want something like ENEMY(3, 6) to indicate that that event will have speed 3 at frequency 6.
This post has been edited by Tsukihime: Sep 30 2011, 09:21 AM