Chaos Project

RPG Maker => RPG Maker Scripts => Script Requests => Topic started by: Ryex on July 23, 2008, 06:41:35 pm

Title: [RESOLVED] script that Finds Event Ids
Post by: Ryex on July 23, 2008, 06:41:35 pm
I need another short code that takes the Players X & Y position and the direction they are facing and check a certain distance from the actor starting right in fount of the actor for events and returns the first Id it finds.

like this

(http://i15.photobucket.com/albums/a400/ryex/Exsamples/untitled3.jpg)

the red line is the distance that can be given when calling the script and the green line it the order in which it checks the events.
Title: Re: [request]script that Finds Event Ids
Post by: Fantasist on July 24, 2008, 02:27:37 am
I don't know if this is the best way of doing it, but here it is:


class Game_Event
  attr_reader :event
end

def find_first_event(dir=$game_player.direction, range=1)
  id = nil
  found_at = nil
  case dir
  when 2
    (1..range).each {|i|
    return [id, found_at] if id != nil
    $game_map.events.each_value {|ge|
    if ((ge.event.x == $game_player.x) && (ge.event.y == $game_player.y + i))
      id = ge.event.id
      found_at = i
    end}}
  when 4
    (1..range).each {|i|
    return [id, found_at] if id != nil
    $game_map.events.each_value {|ge|
    if ((ge.event.y == $game_player.y) && (ge.event.x == $game_player.x - i))
      id = ge.event.id
      found_at = i
    end}}
  when 6
    (1..range).each {|i|
    return [id, found_at] if id != nil
    $game_map.events.each_value {|ge|
    if ((ge.event.y == $game_player.y) && (ge.event.x == $game_player.x + i))
      id = ge.event.id
      found_at = i
    end}}
  when 8
    (1..range).each {|i|
    return [id, found_at] if id != nil
    $game_map.events.each_value {|ge|
    if ((ge.event.x == $game_player.x) && (ge.event.y == $game_player.y - i))
      id = ge.event.id
      found_at = i
    end}}
  end
  return [id, found_at]
end


Paste this in a script slot anywhere below Game_Event and above Main. Use it in the game by Call Script command:
find_first_event(range, direction)


range - The number of tiles to check. In your example above, range is 3.
direction - The direction to check for events. This is optional, the default is the player's facing direction.

Direction is a number corresponding ti the direction on a numpad.
2 - down
4 - left
6 - right
8 - up

Using it returns an array containing the event ID and the distance where it was found.
Title: Re: [request]script that Finds Event Ids
Post by: Ryex on July 24, 2008, 11:18:03 am
 :D yay this along with one more small piece of code will allow me to release my demo once I get the database stuff done thank you!

oh so you know you did not put spaces between the ' = ' and the words in the method definition. it will pull a snataxt error and when you wrote the call script command you reversed the varyabuls 

(My spelling is horrid today)

and If I just want it to return the Id I modify the bottom of the code like this right?


  end
  return id
end
Title: Re: [request]script that Finds Event Ids
Post by: Fantasist on July 24, 2008, 11:44:28 am
Yeah, but you also need to change all the 'return [id, found_at]'s in all the 4 directions:
(1..range).each {|i|
    return [id, found_at] if id != nil

Basically, the script loops through the range from 1, and returns the data if it's already found.
I thought I'd also return the found_at value since it might come useful, but it's your call :)

EDIT:
Quoteoh so you know you did not put spaces between the ' = ' and the words in the method definition. it will pull a snataxt error and when you wrote the call script command you reversed the varyabuls


No, I edited the main code about 4 times and forgot to change the call script thing. But now I think of it, range and dir seems more appropriate. Just change the method definition to whhatever you like :D

And remember:
QuoteI don't know if this is the best way of doing it


I realized there's a method in Game_Map (called check_event(x, y)) which did the same thing, so I'm guessing it's good enough.