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.

Events/Player Facing, Basic Help Needed

Hello, I was looking for a little bit of help, from someone with a little more scripting knowledge than myself.  Basically, I'm looking for something that can check if the player is facing an event, or vice versa.  So, as opposed to having 4 conditional branches in my event that have

Code:
$game_player.direction == 8 and $game_map.events[@event_id].y < $game_player.y

Code:
$game_player.direction == 2 and $game_map.events[@event_id].y > $game_player.y

Code:
$game_player.direction == 4 and $game_map.events[@event_id].x < $game_player.x

Code:
$game_player.direction == 6 and $game_map.events[@event_id].x > $game_player.x

I'd rather have on compressed conditional script that be something like

Code:
$game_player is facing $game_map.events[@event_id]

I'd also like to make one that checks if two events are facing each other.  So instead of four huge conditional branches that looked like this

$game_player.direction == 6 and $game_map.events[@event_id].direction == 4 and $game_player.x < $game_map.events[@event_id].x

I could possibly make a simple conditional branch check

Code:
$game_player, $game_map.events[@event_id] are facing each

I already know how to check these things with events, but I'd really appreciate if someone could help me compress this stuff with a little bit of scripting.  I would do it myself, but I'm still learning the very basics.  Also know that this is for a good cause.  I'll be using this to help simplify the pixel-based ABS I've been working on, so this will definitely help out the community.  Thanks in advance.
 
Just add the following code to your Game_System script:
Code:
def player_facing_event(event)
    if $game_player.direction == 8 and $game_map.events[event].y < $game_player.y
        return true
    elsif $game_player.direction == 2 and $game_map.events[event].y > $game_player.y
        return true
    elsif $game_player.direction == 4 and $game_map.events[event].x < $game_player.x
        return true
    elsif $game_player.direction == 6 and $game_map.events[event].x > $game_player.x
        return true
    else
        return false
    end
end

def player_facing_events(events)
result = []
for i in 0...events.size
    if $game_player.direction == 8 and $game_map.events[events[i]].y < $game_player.y
        result[i] = true
    elsif $game_player.direction == 2 and $game_map.events[events[i]].y > $game_player.y
        result[i] = true
    elsif $game_player.direction == 4 and $game_map.events[events[i]].x < $game_player.x
        result[i] = true
    elsif $game_player.direction == 6 and $game_map.events[events[i]].x > $game_player.x
        result[i] = true
    else
        result[i] = false
    end
end

for i in 0...events.size
    if not result[i]
        return false
    end
end
return true
end

Then whenever you want to check for the player facing one event, use the branch:
Code:
Conditional Branch: $game_system.player_facing_event(id)
Whereas 'id' is the id of the event.

Whenever you want to check for the player facing more events (up to as many as you want), use the branch:
Code:
Conditional Branch: $game_system.player_facing_events([id1, id2, id3])
Make an array with all your events, for instance [1,2,3] would check for events with the id 1, 2 and 3.

I'm not sure if this script is already available in core RMXP, but I think not. Hope this works.
 
Thank you very much FireRaven, your reply was very helpful.  The first part of my problem you fixed perfectly.  I also needed to check when an event and the player were both facing each other, but I actually managed to get this to work on my own by looking off of the script you provided.  It looks a little something like this:

Code:
def player_and_event_facing(event)
    if $game_player.direction == 8 and $game_map.events[event].direction == 2 and $game_player.y > $game_map.events[event].y
        return true
    elsif $game_player.direction == 2 and $game_map.events[event].direction == 8 and $game_player.y < $game_map.events[event].y
        return true
    elsif $game_player.direction == 4 and $game_map.events[event].direction == 6 and $game_player.x > $game_map.events[event].x
        return true
    elsif $game_player.direction == 6 and $game_map.events[event].direction == 4 and $game_player.x < $game_map.events[event].x
        return true
    else
          return false
    end
end 

Not the prettiest code, but it works!  Thank you for all the help, this topic is resolved.
 

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