[RESOLVED] script that Finds Event Ids

Started by Ryex, July 23, 2008, 06:41:35 pm

Previous topic - Next topic

Ryex

July 23, 2008, 06:41:35 pm Last Edit: July 25, 2008, 05:37:45 pm by Blizzard
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



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.
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Fantasist

July 24, 2008, 02:27:37 am #1 Last Edit: July 24, 2008, 02:41:18 am by Fantasist
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.
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Ryex

July 24, 2008, 11:18:03 am #2 Last Edit: July 24, 2008, 11:19:46 am by Ryex
 :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
I no longer keep up with posts in the forum very well. If you have a question or comment, about my work, or in general I welcome PM's. if you make a post in one of my threads and I don't reply with in a day or two feel free to PM me and point it out to me.<br /><br />DropBox, the best free file syncing service there is.<br />

Fantasist

July 24, 2008, 11:44:28 am #3 Last Edit: July 24, 2008, 11:48:01 am by Fantasist
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.
Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews