Figuring out the distance from the player?

So, my end goal is to be able to create proximity sounds, but I've run into an issue I wasn't expecting. I'm not experienced with Ruby scripts, but I'd seen a specific couple of lines that I thought should work:
Ruby:
sx = distance_x_from($game_player.x).abs
sy = distance_y_from($game_player.y).abs
But when the event is triggered, it gives the error message:
"Script 'Game_Interpreter' line 1411: NoMethodError occured.

undefined method 'distance_x_from' for #<Game_Interpreter:0xd4c39f4>"

So, anybody able to help me figure out what's wrong?
So, my end goal is to be able to create proximity sounds, but I've run into an issue I wasn't expecting. I'm not experienced with Ruby scripts, but I'd seen a specific couple of lines that I thought should work:
Ruby:
sx = distance_x_from($game_player.x).abs
sy = distance_y_from($game_player.y).abs
But when the event is triggered, it gives the error message:
"Script 'Game_Interpreter' line 1411: NoMethodError occured.

undefined method 'distance_x_from' for #<Game_Interpreter:0xd4c39f4>"

So, anybody able to help me figure out what's wrong?
that means there's no method called distance_x_from or distance_y_from defined in the Game_Interpreter... the line you see working might be from other script that define that method inside Game_Interpreter. you might want to have that script installed if you want it to work...

if you're did not want to have that script... i think you can also get the event x and y that currently executing the event command with:
Code:
ex = $game_map.events[@event_id].x
ey = $game_map.events[@event_id].y
for player you can use:
Code:
px = $game_player.x
py = $game_player.y

but... if i remember correctly we can also store above in variables...
there's a built in function that can grab that gamedata and store it in control variable event command.
you can store them in the variables... and then you can calculate it to know the 'distance'...

i cannot test above because currently my ACE is already uninstalled... but hope this help.

edit: i think the script you see is this:

but using that script... your way of calling the method still wrong though... you need to pass the eventid and the player pos...
so maybe this:
Code:
sx = distance_x_from(@event_id,$game_player.x).abs
sy = distance_y_from(@event_id,$game_player.y).abs

hope this help.
Okay, so I get this:
that means there's no method called distance_x_from or distance_y_from defined in the Game_Interpreter...
but I'm not sure what you mean by
the line you see working might be from other script that define that method inside Game_Interpreter. you might want to have that script installed if you want it to work...
Are you saying that the script is defined by a different base script or are you saying that an added script is interfering? Because I have modified a few scripts, but I haven't added any.
Edit:
Can these be consolidated
Code:
ex = $game_map.events[@event_id].x
ey = $game_map.events[@event_id].y
Code:
px = $game_player.x
py = $game_player.y
into
Code:
dx = $game_map.events[@event_id].x - $game_player.x
dy = $game_map.events[@event_id].y - $game_player.y

edit: i think the script you see is this:
I actually took it directly from the core script Game_Events, lines 140-141, not sure if that will help at all, but it can't hurt.
but using that script... your way of calling the method still wrong though... you need to pass the eventid and the player pos...
so maybe this:
Code:
sx = distance_x_from(@event_id,$game_player.x).abs
sy = distance_y_from(@event_id,$game_player.y).abs
I just tried using that and got the exact same error message. I'm going to try the first method and get back to you.
Edit:
First method definitively works.
Okay, so I get this: but I'm not sure what you mean by Are you saying that the script is defined by a different base script or are you saying that an added script is interfering? Because I have modified a few scripts, but I haven't added any.
Edit:
Can these be consolidated
Code:
ex = $game_map.events[@event_id].x
ey = $game_map.events[@event_id].y
Code:
px = $game_player.x
py = $game_player.y
into
Code:
dx = $game_map.events[@event_id].x - $game_player.x
dy = $game_map.events[@event_id].y - $game_player.y

I actually took it directly from the core script Game_Events, lines 140-141, not sure if that will help at all, but it can't hurt.

I just tried using that and got the exact same error message. I'm going to try the first method and get back to you.
Edit:
First method definitively works.
ah... it's on Game_Events... that's why it doesn't work...

if you script call something using event command... it's inside Game_Interpreter environment... so you cannot call the method from Game_Events without referencing to the events...
ex:
Code:
sx = distance_x_from($game_player.x).abs
will throw error because Game_Interpreter did not have method called distance_x_from
but
Code:
sx = $game_map.events[@event_id].distance_x_from($game_player.x).abs
will work... because $game_map.events[@event_id] is a Game_Events... and we're calling it's distance_x_from method.

Code:
dx = $game_map.events[@event_id].x - $game_player.x
dy = $game_map.events[@event_id].y - $game_player.y
i think this should works in theory... hope this help.
i think this should works in theory... hope this help.
It does and it's working! Thank you!

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/figuring-out-the-distance-from-the-player.128813/