Are these a valid script calls?

Parallax Panda

Got into VxAce ~2014 and never stopped...
Veteran
Joined
Oct 29, 2015
Messages
803
Reaction score
1,075
First Language
Swedish
Primarily Uses
RMMV
So I have events on the map and I want to check when the player is standing next to them (not diagonally, just north, west, east, south).

So for this I figured of running a series of nested conditional branches with these script calls:

$gamePlayer.x === $gameMap.events(this._eventId).x +1 && $gamePlayer.y === $gameMap.events(this._eventId).y
$gamePlayer.x === $gameMap.events(this._eventId).x -1 && $gamePlayer.y === $gameMap.events(this._eventId).y
$gamePlayer.x === $gameMap.events(this._eventId).x && $gamePlayer.y === $gameMap.events(this._eventId).y +1
$gamePlayer.x === $gameMap.events(this._eventId).x && $gamePlayer.y === $gameMap.events(this._eventId).y -1

(I'm kinda unsure if the "+1" and "-1" parts are valid expressions)
 

ct_bolt

Creator
Veteran
Joined
May 3, 2012
Messages
665
Reaction score
195
First Language
Javascript
Primarily Uses
RMMV
So I have events on the map and I want to check when the player is standing next to them (not diagonally, just north, west, east, south).

So for this I figured of running a series of nested conditional branches with these script calls:

$gamePlayer.x === $gameMap.events(this._eventId).x +1 && $gamePlayer.y === $gameMap.events(this._eventId).y
$gamePlayer.x === $gameMap.events(this._eventId).x -1 && $gamePlayer.y === $gameMap.events(this._eventId).y
$gamePlayer.x === $gameMap.events(this._eventId).x && $gamePlayer.y === $gameMap.events(this._eventId).y +1
$gamePlayer.x === $gameMap.events(this._eventId).x && $gamePlayer.y === $gameMap.events(this._eventId).y -1

(I'm kinda unsure if the "+1" and "-1" parts are valid expressions)
looks good to me :)
 
Last edited:

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,522
Reaction score
12,054
First Language
English
Primarily Uses
RMMV
If you are doing this as a script call, you could do this:

Code:
Math.abs($gamePlayer.x - this.character(0).x) + Math.abs($gamePlayer.y - this.character(0).y) === 1
 

MushroomCake28

KAMO Studio
Global Mod
Joined
Nov 18, 2015
Messages
2,346
Reaction score
3,652
First Language
English
Primarily Uses
RMMV

I've moved this thread to Learning Javascript. Please be sure to post your threads in the correct forum next time. Thank you.

 

Parallax Panda

Got into VxAce ~2014 and never stopped...
Veteran
Joined
Oct 29, 2015
Messages
803
Reaction score
1,075
First Language
Swedish
Primarily Uses
RMMV
@Shaz
Oho!

I am Indeed doing it as a script call in a conditional branch. The purpose being to see if the player is standing next to this event. The check will probably run in a parallel event with a wait command at the end.

I basically know nothing about javascript (which I guess the script calls really are), so it would be nice if someone could break down the snippet and explain what each part does for me.

I'm guessing the "Math.abs()" is just calling a math function, something for calculating math. And that the "=== 1" at the end means that everything that's been written before this is true (although I might be wrong about this)?

What I guess I really need to understand in-depth is what the parts in orange REALLY means. I understand it compares the players x and y to the event's, but in what way?

Math.abs($gamePlayer.x - this.character(0).x) + Math.abs($gamePlayer.y - this.character(0).y) === 1

--------------------------------
I'm not sure this is the best way but this is how I thought I'd want to do this check (this is not written with real script commands, just text to explain what I want to check):
_____________________________________________
___________PARALLEL EVENT BELLOW___________

Code:
◆If:Script:if player's x = this event's x then...
  ◆If:Script:if player's y = this event's y - 1 then...
    ◆Comment:If this is true the player should be standing on the tile
    :       :to the NORTH of the event.
    ◆
  :Else
    ◆If:Script:if player's y = this event's y + 1 then...
      ◆Comment:If this is true the player should be standing on the tile
      :       :to the SOUTH of the event.
      ◆
    :Else
      ◆
    :End
    ◆
  :End
  ◆
:Else
  ◆If:Script:if player's y = this event's y then...
    ◆If:Script:if player's x = this event's x - 1 then...
      ◆Comment:If this is true the player should be standing on the tile
      :       :to the WEST of the event.
      ◆
    :Else
      ◆If:Script:if player's x = this event's x + 1 then...
        ◆Comment:If this is true the player should be standing on the tile
        :       :to the EAST of the event.
        ◆
      :Else
        ◆
      :End
      ◆
    :End
    ◆
  :Else
    ◆
  :End
  ◆
:End
◆Wait:5 frames
--------------------------------
is true.

[EDIT; It appears that the indented text was removed automatically by the forum which makes the copy paste text much harder to read. I'm sorry about that. In a nutshell, the conditional branches are nested though. Fixed! Thank you @Kes ]

[EDIT2; Also, here's a picture if for some reason my explanation above is not enough of what I'd like to achieve with eventing and script calls]
 
Last edited:

Kes

Global Moderators
Global Mod
Joined
Aug 3, 2012
Messages
21,633
Reaction score
10,931
First Language
English
Primarily Uses
RMVXA
@Parallax Panda The problem with indenting text can be solved by using the 'code' option. Click on the icon that looks like 3 dots on the post tool bar and you'll see Code in the dropdown menu.
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,522
Reaction score
12,054
First Language
English
Primarily Uses
RMMV
Sure. Here's the formula:

Code:
Math.abs($gamePlayer.x - this.character(0).x) + Math.abs($gamePlayer.y - this.character(0).y) === 1
When you're running a script call in an event, this is the interpreter attached to the event, so you can say this. followed by any function in the Game_Interpreter class to run that function.

Now, Game_Interpreter has a neat function called character(x) and if you pass -1 to that function, it returns the player object. If you pass a number > 0, it returns the event object of the event with that id. And if you pass 0, it returns the event object that this command was run from. So this.character(0) is the same as saying $gameMap.event(this._eventId).

So what we're doing here is taking the difference between the x position of the player and of the event, and the difference between the y position of the player and the event.

Math.abs() converts a number to the absolute value - it removes the negative sign. So if player.x is 5 and event.x is 4, then player.x - event.x will be 5 - 4, or 1. But if player.x is 4 and event.x is 5, then player.x - event.x will be -1. Math.abs() just ditches the negative sign, so in both cases, the difference would be just 1. Same for the y comparison.

Finally, we add the two numbers (difference in x and difference in y) together to see how many tiles apart they are. If the player is immediately above, below or beside the event, the difference will be 1 (1 + 0 or 0 + 1, because either the x values or the y values will be the same). If the player is touching diagonally, the difference will be 2 (1 away horizontally plus 1 away vertically - 1 + 1 = 2).

The === 1 is the javascript comparison. The Math.abs(...) + Math.abs(...) returns a number. The === 1 just compares that returned number with 1 (the distance you want the player to be from the event), and returns true if they match (the result is 1) and false if they don't (the result is not 1).

The only positions the player could be in, to return true from that calculation, would be directly above, below, or beside the event, which are the positions you've highlighted in red and green on your screenshot. Touching diagonally will return false, as will any position further away.

Do you follow that? If, not, I'll draw some pictures :)


If you just copy that and paste it into your Script box in the conditional branch, it will work. However, it will only tell you that the player is one tile away from the event. It will NOT tell you which tile the player is on - above, below, to the right or left. So if north/south/east/west is important, the above will not tell you that. If you need to know exactly where the player is in relation to the event, then your method above is the one you will have to use. I made the assumption that you were only interested in knowing if they were one tile apart, and didn't care about the direction, which is probably not a good assumption to make ...
 
Last edited:

Parallax Panda

Got into VxAce ~2014 and never stopped...
Veteran
Joined
Oct 29, 2015
Messages
803
Reaction score
1,075
First Language
Swedish
Primarily Uses
RMMV
@Shaz
Thank you. That was a pretty in-depth and easy to understand explanation. I understand what the script command does now. It does work like you say. MV's collisions is a bit wonky if I have both events walk around. Sometimes it'll trigger if the player stands diagonal to the event and both move north at the same time, but I guess that can't be helped since it has to do with the grid and how MV reads the location of objects I guess.

I'm actually not sure if I absolutely need to know if the player is to the north, south, west and east. It depends on how I design everything, But I'm considering having the player and event face each other when the condition triggers, in which case it might be useful to know where they stand in relation to each other. But I don't know if there's a script command or something similar for making the player face a certain event?

Furthermore, while I'm actually testing this in 2D I later plan to port everything over to Dread_Nyanak's 3D environment which will work a little differently in 1st person perspective which I'm using. At least when it comes to turning the player since that'll mean I'll be rotating the camera instead.

For now though, I'm still just doing this in a 2D environment and I'm actually looking for a solution that would work in both (or two separate solutions).
 

Shaz

Veteran
Veteran
Joined
Mar 2, 2012
Messages
38,522
Reaction score
12,054
First Language
English
Primarily Uses
RMMV
Unfortunately the character's x and y positions might be updated when they start to move, which might mean it triggers as they're about to step into the correct position.

You could try one of the following, to make sure either the player, or the event, or both, have finished taking a step:

Player and event must have both finished their moves
Code:
!$gamePlayer.isMoving() && !this.character(0).isMoving() && (Math.abs($gamePlayer.x - this.character(0).x) + Math.abs($gamePlayer.y - this.character(0).y) === 1)
Player has finished their move
Code:
!$gamePlayer.isMoving() && (Math.abs($gamePlayer.x - this.character(0).x) + Math.abs($gamePlayer.y - this.character(0).y) === 1)
Event has finished its move
Code:
!this.character(0).isMoving && (Math.abs($gamePlayer.x - this.character(0).x) + Math.abs($gamePlayer.y - this.character(0).y) === 1)
To make them face each other, you can do this:
Code:
this.character(0).turnTowardCharacter($gamePlayer);
$gamePlayer.turnTowardCharacter(this.character(0));
 

Parallax Panda

Got into VxAce ~2014 and never stopped...
Veteran
Joined
Oct 29, 2015
Messages
803
Reaction score
1,075
First Language
Swedish
Primarily Uses
RMMV
@Shaz
Fantastic! You've provided me with so many useful script calls. I think I'll be able to manage work everything out with this.

Thanks again, you're the best :kaoluv:
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Here's a sample of some pixel edits I made.

Maybe for use as sidewalks.
If there are any mods or admins online, could you please see about the re-translation pages for RPG Maker MV? They are all down :rsad:
Werewolf cons: you lose control of yourself once a month and could accidentally kill your friends. Vampire cons: no garlic bread. The choice is simple. Werewolf for sure.
2 huge market drops in 2 days. So glad I sold my non-dividend stocks about 2 weeks ago. And if the dividend ones drop heavily, hey more cheap stocks for me!
My tutorial is live! It covers using Yanfly's weapon unleash to replace the basic attack skill with a more dynamic version depending on which weapon type the actor has equipped. It also includes how to import a custom function used to generate a random number within supplied minimum and maximum values, used in the replaced skills, to give weapons a set damage range.

Forum statistics

Threads
94,524
Messages
921,597
Members
124,376
Latest member
Darkyyy
Top