Scripting question

I'd like to write a script that checks if the region id of the tile directly in front of an event is higher than the region id of the tile 2 tiles in front of that event.

This is what I came up with (inside a conditional branch script, not pure script):

var d = $gameMap.event(this.eventId()).direction();
var x1 = $gameMap.roundXWithDirection($gameMap.event(this.eventId()).x, d);
var y1 = $gameMap.roundYWithDirection($gameMap.event(this.eventId()).y, d);
var x2 = $gameMap.roundXWithDirection(x1, d);
var y2 = $gameMap.roundYWithDirection(y1, d);
$gameMap.regionId(x1, y1) > $gameMap.regionId(x2, y2)

It doesn't seem to work however, so I'm wondering if I'm doing something wrong? the same script did work when I only checked the tile in front so I was hoping this is possible too.
You can't declare variables and make multiple code statements inside of a conditional.

The syntax for an if statement in JavaScript is if (condition) - you can have multiple conditions if you're connecting them via boolean operators, but what you're doing above is going to result in a syntax error.

You can either save that as a function in a plugin and call the function in your conditional branch, or you can use event commands to set game variables for the first 4 lines of code then just put the last part that's comparing region IDs in your conditional branch referencing those game variables (e.g. $gameMap.regionId($gameVariables.value(5), $gameVariables.value(9))...etc)
That works OK for me in Conditional Branch -> Script. Did you put it all onto a single line before copy+pasting into the single-line script field? If not, only the first line will be checked.
Spoiler: my test event setup
◆If:Script:var d = $gameMap.event(this.eventId()).direction(); var x1 = $gameMap.roundXWithDirection($gameMap.event(this.eventId()).x, d); var y1 = $gameMap.roundYWithDirection($gameMap.event(this.eventId()).y, d); var x2 = $gameMap.roundXWithDirection(x1, d); var y2 = $gameMap.roundYWithDirection(y1, d); $gameMap.regionId(x1, y1) > $gameMap.regionId(x2, y2) ◆Text:None, Window, Bottom : :The ID of the region 1 step in front of me : :is higher than : :the ID of the region 2 steps in front of me. ◆ :Else ◆Text:None, Window, Bottom : :The ID of the region 1 step in front of me : :is NOT higher than : :the ID of the region 2 steps in front of me. ◆ :End
Other ideas:
  1. Make sure you're not mixing up the target tiles. Events that aren't Direction Fixed will automatically "lock" to face the player if started via button or collision. The tiles you're checking are those in front of "This Event".

  2. Weird stuff might also happen if you're using map transfers mid-event: your setup explicitly references the event on the current map with ID equal to the event whose commands are being run.

  3. You can tidy it up a bit, e.g.

    var ev = this.character(0); var d = ev.direction(), x1 = $gameMap.roundXWithDirection(ev.x, d), y1 = $gameMap.roundYWithDirection(ev.y, d); var x2 = $gameMap.roundXWithDirection(x1, d), y2 = $gameMap.roundYWithDirection(y1, d); $gameMap.regionId(x1, y1) > $gameMap.regionId(x2, y2)
    I've used this.character(0) here, which is the standard "This Event" reference used by event commands. It will be null if the running event is not on the current map. May be helpful for diagnosing bad event setups~

  4. This would be doable solely with event commands: store event data in variables (Control Variables -> Game Data -> Map X|Y, Direction), +1|2 to X or Y depending on direction and range, then use Get Location Info to extract the region IDs.

@ATT_Turan - all Script fields in RM (including the damage formula, for example) are evals, therefore they are treated as single, contained units of code. Having multiple statements within the eval is valid: only the output from the eval is used to determine the interpreter's response.
I tried this instead with variables:


var d = $gameMap.event(this.eventId()).direction();
var x = $gameMap.roundXWithDirection($gameMap.event(this.eventId()).x, d);
var y = $gameMap.roundYWithDirection($gameMap.event(this.eventId()).y, d);
$gameVariables.setValue(4, $gameMap.regionId(x, y));

var d = $gameMap.event(this.eventId()).direction();
var x1 = $gameMap.roundXWithDirection($gameMap.event(this.eventId()).x, d);
var y1 = $gameMap.roundYWithDirection($gameMap.event(this.eventId()).y, d);
var x2 = $gameMap.roundXWithDirection(x1, d);
var y2 = $gameMap.roundYWithDirection(y1, d);
$gameVariables.setValue(5, $gameMap.regionId(x2, y2));

didn't work :/ both variables are still at 0

Did you put it all onto a single line before copy+pasting into the single-line script field?

Yes 🙂 I did it exactly the way you phrased it inside the script line. it worked perfectly fine with all my other scripts but for some reason this one is failing
the console log doesn't show me any syntax error either
For testing, try something like this in a standalone Script command:
JavaScript:
var d = $gameMap.event(this.eventId()).direction();
var x = $gameMap.roundXWithDirection($gameMap.event(this.eventId()).x, d);
var y = $gameMap.roundYWithDirection($gameMap.event(this.eventId()).y, d);
alert("d: " + d + ", x: " + x + ", y: " + y);
This will show you the values of d, x, and y, allowing you to check them against your expected values.

If the values are all correct, double-check that those variables aren't being used elsewhere, e.g. some Parallel event. Tools -> Event Searcher from the editor's menu bar may help with that.

Edit: that said, the outputs should be correct irrespective of where those variables are being used. You might also want to check your plugins...I guess it's possible that a movement-based plugin (e.g. half-tile or pixel-based movement) would affect this?
all Script fields in RM (including the damage formula, for example) are evals, therefore they are treated as single, contained units of code. Having multiple statements within the eval is valid: only the output from the eval is used to determine the interpreter's response.
Good to know, I hadn't thought about it that way.
Arrrgh, I went over all the commands again and found the problem xD

It was something I copied incorrectly from another event, had nothing to do with this but somehow interfered with it. it's working now. thanks a lot anyway!

Original thread: https://forums.rpgmakerweb.com/threads/scripting-question.152438/