Script Call / Javascript Help

I am sorry I am really not smart with this type of logic. So please do not be too harsh with me. I am trying to understand this, really.
I don't see how anything I said is harsh. As a general concept in life, telling someone something doesn't work without demonstrating the problem can't help you solve it (try taking your car to the mechanic, saying "It doesn't work," and leaving it there 🤣 ).

And when I tell you that your plan is an inefficient way to do something, that's because you're new to coding things and I'm pointing out flaws and offering a better method.

Because when I am in front of the Wall I can still hop onto it regardless of the steps I do.
You continue to put in your script call if ($gameMap.regionId(x2, y2)<105)

The symbol < in math means less than. You have a border around your map which uses region 105, which is not less than 105.

You have no regions marked inside that border, so a nonexistent region is not less than 105. None of the spaces you're looking at will evaluate to true.

If you want region 105 to block your movement, you need to ask "is the region on this tile equal to 105," not "is it less than 105". So if ($gameMap.regionIs(x2, y2)==105)
I don't see how anything I said is harsh. As a general concept in life, telling someone something doesn't work without demonstrating the problem can't help you solve it (try taking your car to the mechanic, saying "It doesn't work," and leaving it there 🤣 ).

And when I tell you that your plan is an inefficient way to do something, that's because you're new to coding things and I'm pointing out flaws and offering a better method.


You continue to put in your script call if ($gameMap.regionId(x2, y2)<105)

The symbol < in math means less than. You have a border around your map which uses region 105, which is not less than 105.

You have no regions marked inside that border, so a nonexistent region is not less than 105. None of the spaces you're looking at will evaluate to true.

If you want region 105 to block your movement, you need to ask "is the region on this tile equal to 105," not "is it less than 105". So if ($gameMap.regionIs(x2, y2)==105)
I think it's hopeless. I do not understand. So yes, I might just leave my car there and buy a new one.

No but seriously. I am more confused than before now. Let's please break this down.
Code:
var x1 = $gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction);
var y1 = $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction);
var x2 = $gameMap.roundXWithDirection(x1, $gamePlayer.direction);
var y2 = $gameMap.roundYWithDirection(y1, $gamePlayer.direction);

With this I am supposed to get the players x and y position as well as the direction the player is facing, and possibly a scan / vision of 2 tiles ahead right?
Then we add this if statement:

Code:
if($gameMap.regionId(x2, y2) < 105)
{
$gamePlayer.setThrough(true);
ANY OTHER MOVEMENT ACTIVITY;
}

So my understanding with that is: On any Region with Region number 0 - 104 I can jump around, get ''through'' everything and do whatever without worries. Until we reach this part:

Code:
else
{
$gamePlayer.setThrough(false);
}

Now I basically have the else statement and everything at Region 105 and above will not allow me to get through anymore.
And what I believed was, that the function Shaz gave me, allowed the player to see 2 tiles in front of him whether the Region with 105 is in range or not. And if so, the else statement would trigger and ''through'' would be disabled (false).

So again what I wish to accomplish is to NEVER EVER be able to get on Region number 105.
So that the ''through'' will be disabled all the time when I face the Region in front of me by 1 or 2 or 3 tiles.
Basically Region 105 means death. That's how brutal it would be to get on it!
No jokes aside, if I am not capable of understanding what you guys are trying to teach me, there is no hope!
But thanks a lot for your time and help. I might bother people with something else in the future again! so look forward to it (And another car you can keep) :LZSsmile:
So my understanding with that is: On any Region with Region number 0 - 104 I can jump around, get ''through'' everything and do whatever without worries.
Sorry, I misread the order, you are correct.

One problem is that you're only checking the tile two spaces away. You're checking the regionId of x2 and y2. If you're only moving one space at a time, I don't know why you're looking at those at all. You only care about the space immediately in front of you, x1 y1.

For that matter, there's nothing in there to move the player a second space, so I'm not sure what your goal is with this. It seems to me that what you want to be doing is:
Code:
if ($gameMap.regionId(x1, y1) < 105)
{
    $gamePlayer.setThrough(true);
    $gamePlayer.moveForward();
    if ($gameMap.regionId(x2, y2) < 105)
        $gamePlayer.moveForward();
}

That will check the tile in front of the player, move onto it if possible, and then check the tile that was two spaces away and also move onto it if possible.
If you want to check 2 tiles away, do this:
Code:
var x1 = $gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction);
var y1 = $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction);
var x2 = $gameMap.roundXWithDirection(x1, $gamePlayer.direction);
var y2 = $gameMap.roundYWithDirection(y1, $gamePlayer.direction);
if ($gameMap.regionId(x2, y2) < 105) { ... }
Nearly, but $gamePlayer.direction, without brackets, is a function reference: it this case you want the result of evaluating the function, which would be $gamePlayer.direction().

I think I'd personally just use event commands, e.g. "store the region ID for the tile 1 step in front of the player":

◆Control Variables:#0001 PlayerX = Map X of Player ◆Control Variables:#0002 PlayerY = Map Y of Player ◆If:Player is facing Down ◆Control Variables:#0002 PlayerY += 1 ◆ :Else ◆If:Player is facing Left ◆Control Variables:#0001 PlayerX -= 1 ◆ :Else ◆If:Player is facing Right ◆Control Variables:#0001 PlayerX += 1 ◆ :Else ◆Control Variables:#0002 PlayerY -= 1 ◆ :End ◆ :End ◆ :End ◆Get Location Info:Variable 3, Region ID, ({PlayerX},{PlayerY})
Like Turan says, if you want to check multiple tiles (e.g. 1, 2, and 3 steps ahead) then you'll need to do this kind of thing once per tile.

Edit: also, I'd recommend sticking to move routes for moving map characters. Otherwise you may see them "teleport" if you tell them to move while they're busy with other movement.
I Evented a dirty example to better plan the logic, the region thing works, but the logic to check if
a Event is on the spot we want to Jump is not calculated yet.
But maybe it helps. Iam not fluent with the Syntax so i used Eventing for the Prototype.
All the Scriptlines the others posted will help you make this shorter or more flexible and easier for copy paste.
Spoiler
◆If:Button [Pageup] is pressed down
◆Control Variables:#0001 player_x = Map X of Player
◆Control Variables:#0002 player_y = Map Y of Player
◆Control Variables:#0003 magic_x = 0
◆Control Variables:#0004 magic_y = 0
◆Control Variables:#0006 jumpdistance = 0
◆If:Player is facing Down
◆Control Variables:#0004 magic_y = 1
◆
:End
◆If:Player is facing Left
◆Control Variables:#0003 magic_x = -1
◆
:End
◆If:Player is facing Right
◆Control Variables:#0003 magic_x = 1
◆
:End
◆If:Player is facing Up
◆Control Variables:#0004 magic_y = -1
◆
:End
◆Control Variables:#0001 player_x += magic_x
◆Control Variables:#0002 player_y += magic_y
◆Get Location Info:region_info, Region ID, ({player_x},{player_y})
◆If:region_info = 105
◆
:Else
◆Control Variables:#0006 jumpdistance = 1
◆Control Variables:#0001 player_x += magic_x
◆Control Variables:#0002 player_y += magic_y
◆Get Location Info:region_info, Region ID, ({player_x},{player_y})
◆If:region_info = 105
◆
:Else
◆Control Variables:#0006 jumpdistance = 2
◆Control Variables:#0001 player_x += magic_x
◆Control Variables:#0002 player_y += magic_y
◆Get Location Info:region_info, Region ID, ({player_x},{player_y})
◆If:region_info = 105
◆
:Else
◆Control Variables:#0006 jumpdistance = 3
◆
:End
◆
:End
◆
:End
◆If:jumpdistance = 1
◆Set Movement Route:Player (Skip, Wait)
: :◇Frequency:5
: :◇Speed:6
: :◇Through ON
: :◇1 Step Forward
: :◇Through OFF
: :◇Speed:4
: :◇Frequency:3
◆
:End
◆If:jumpdistance = 2
◆Set Movement Route:Player (Skip, Wait)
: :◇Frequency:5
: :◇Speed:6
: :◇Through ON
: :◇1 Step Forward
: :◇1 Step Forward
: :◇Through OFF
: :◇Speed:4
: :◇Frequency:3
◆
:End
◆If:jumpdistance = 3
◆Set Movement Route:Player (Skip, Wait)
: :◇Frequency:5
: :◇Speed:6
: :◇Through ON
: :◇1 Step Forward
: :◇1 Step Forward
: :◇1 Step Forward
: :◇Through OFF
: :◇Speed:4
: :◇Frequency:3
◆
:End
◆
:Else
◆
:End
Nearly, but $gamePlayer.direction, without brackets, is a function reference: it this case you want the result of evaluating the function, which would be $gamePlayer.direction().

I think I'd personally just use event commands, e.g. "store the region ID for the tile 1 step in front of the player":

◆Control Variables:#0001 PlayerX = Map X of Player ◆Control Variables:#0002 PlayerY = Map Y of Player ◆If:Player is facing Down ◆Control Variables:#0002 PlayerY += 1 ◆ :Else ◆If:Player is facing Left ◆Control Variables:#0001 PlayerX -= 1 ◆ :Else ◆If:Player is facing Right ◆Control Variables:#0001 PlayerX += 1 ◆ :Else ◆Control Variables:#0002 PlayerY -= 1 ◆ :End ◆ :End ◆ :End ◆Get Location Info:Variable 3, Region ID, ({PlayerX},{PlayerY})

Like Turan says, if you want to check multiple tiles (e.g. 1, 2, and 3 steps ahead) then you'll need to do this kind of thing once per tile.

Edit: also, I'd recommend sticking to move routes for moving map characters. Otherwise you may see them "teleport" if you tell them to move while they're busy with other movement.
After I have realized that meddling with scripts is a little too much for me, I might take a step backwards again and stick to the simple script calls well documented. I should take your example and do stuff with simple eventing. I know eventing can also get complex, but so far I found myself getting limited oftentimes. Yet for my issue I think this would be the way to go after all! I appreciate your effort and time with this. Thank you sincerely. :LZSsmile:

I Evented a dirty example to better plan the logic, the region thing works, but the logic to check if
a Event is on the spot we want to Jump is not calculated yet.
But maybe it helps. Iam not fluent with the Syntax so i used Eventing for the Prototype.
All the Scriptlines the others posted will help you make this shorter or more flexible and easier for copy paste.
Spoiler
◆If:Button [Pageup] is pressed down
◆Control Variables:#0001 player_x = Map X of Player
◆Control Variables:#0002 player_y = Map Y of Player
◆Control Variables:#0003 magic_x = 0
◆Control Variables:#0004 magic_y = 0
◆Control Variables:#0006 jumpdistance = 0
◆If:Player is facing Down
◆Control Variables:#0004 magic_y = 1
◆
:End
◆If:Player is facing Left
◆Control Variables:#0003 magic_x = -1
◆
:End
◆If:Player is facing Right
◆Control Variables:#0003 magic_x = 1
◆
:End
◆If:Player is facing Up
◆Control Variables:#0004 magic_y = -1
◆
:End
◆Control Variables:#0001 player_x += magic_x
◆Control Variables:#0002 player_y += magic_y
◆Get Location Info:region_info, Region ID, ({player_x},{player_y})
◆If:region_info = 105
◆
:Else
◆Control Variables:#0006 jumpdistance = 1
◆Control Variables:#0001 player_x += magic_x
◆Control Variables:#0002 player_y += magic_y
◆Get Location Info:region_info, Region ID, ({player_x},{player_y})
◆If:region_info = 105
◆
:Else
◆Control Variables:#0006 jumpdistance = 2
◆Control Variables:#0001 player_x += magic_x
◆Control Variables:#0002 player_y += magic_y
◆Get Location Info:region_info, Region ID, ({player_x},{player_y})
◆If:region_info = 105
◆
:Else
◆Control Variables:#0006 jumpdistance = 3
◆
:End
◆
:End
◆
:End
◆If:jumpdistance = 1
◆Set Movement Route:Player (Skip, Wait)
: :◇Frequency:5
: :◇Speed:6
: :◇Through ON
: :◇1 Step Forward
: :◇Through OFF
: :◇Speed:4
: :◇Frequency:3
◆
:End
◆If:jumpdistance = 2
◆Set Movement Route:Player (Skip, Wait)
: :◇Frequency:5
: :◇Speed:6
: :◇Through ON
: :◇1 Step Forward
: :◇1 Step Forward
: :◇Through OFF
: :◇Speed:4
: :◇Frequency:3
◆
:End
◆If:jumpdistance = 3
◆Set Movement Route:Player (Skip, Wait)
: :◇Frequency:5
: :◇Speed:6
: :◇Through ON
: :◇1 Step Forward
: :◇1 Step Forward
: :◇1 Step Forward
: :◇Through OFF
: :◇Speed:4
: :◇Frequency:3
◆
:End
◆
:Else
◆
:End
I appreciate your help. And I apologize for not being specific or clear before when I made my post. :LZSsad:


Thanks to all of the people participating in this thread, I come to the conclusion to event more things from here on instead of scriping and take a step back from more complex scripting until I have gotten some more knowledge with javascript.

So I thank all of you especially @Shaz for always being there for the community as well big thanks to @ATT_Turan for sticking with me haha.

I will completely drop the script and do the eventing parts for my plans. I think everything is said and the thread could be closed from my side. :LZSyum:
Could you please show a screenshot of your event? What are the "other movement commands" you're putting in, and how are you adding them? Putting a heap of movement commands into a script call will not work, as it will not wait for one movement to complete before the next one starts. In fact, all the "script" calls above would not go into a movement route, as that allows only a single line of script. So are you adding them as a movement route, or as a script call?

A screenshot of your actual event commands might clear up some things.
Could you please show a screenshot of your event? What are the "other movement commands" you're putting in, and how are you adding them? Putting a heap of movement commands into a script call will not work, as it will not wait for one movement to complete before the next one starts. In fact, all the "script" calls above would not go into a movement route, as that allows only a single line of script. So are you adding them as a movement route, or as a script call?

A screenshot of your actual event commands might clear up some things.
This (See Event SC1.jpg) is the example skill I tried to build yesterday for the sake of this Script to work with your example slightly altered.
On Screenshot (SC2.jpg) is without any checks / stops and be able to get through everything except from map borders what I do not want.

This all is done with Chrono Engine abs mode, which allows me to call upon this Event with a assigned button press.
So whenever I press the button, what is originally supposed to happen is that the player character will change his speed and frequency to max, turn transparent and through is on. Then the Player character is supposed to move X tiles into the direction of where he is facing. Up front or Diagonal. Originally it had been 3 Steps / Tiles up front of him.

The goal is for the Player(Or Event which makes the Player move) to check if Region number 105 or above is within range of 1 or 2 or so Tiles in the direction he is facing inside the skill (Event page), then calculates how much he can move until he is right in front of Region number 105 and then immediately switches ''Through'' to false / off BEFORE he can even get on top of that Region.

I try to visualize it with the (''Rough_Example.jpg'') The red X implies where the Through option ideally would be set to off / false. So basically right in front of the obstacle which is Region number 105. But before that, it should be set to on / true. And regardless you should be able to use the skill before hitting the ''Wall'' we created.

So: Use SKILL: set speed to max, frequency to max, through is on, transparency is on, Step Forward 3 times but realize after 1 or 2 steps respectively ''There is Region 105, I cannot get through this, therefore through is false'' else if Region 105 is not in front or range just run event as normal.
And after successfully completing the skill, set everything to normal again basically.

I hope this made it clear and understanding now? I am really sorry. :LZSyum:

PS: Do not mind the ''tool_projectile'' comment. I forgot to remove it from that event.
Ok i see that most suggestions are about checking the path first and than to move.
you want to do it step by step, that is different from most solutions but it should be also possible
to create.
The diagonal Movement is also a useful information to plan into the solution.
Because 8directions condition does not exist in standard event editor. Or atleast i dont know about it XD.
So how to determine when the direction is diagonal?

Does the X on the Picture indicate the Spot the Player would jump to?

Edit:
I tryed to use @Shaz code, but a Edited Version does not work in Event Command Conditional Branch:
Code:
$gameMap.regionId($gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction), $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction)) >= 105
There is no crash but it still triggers when standing on top of it instead of infront.
Ok i see that most suggestions are about checking the path first and than to move.
you want to do it step by step, that is different from most solutions but it should be also possible
to create.
The diagonal Movement is also a useful information to plan into the solution.
Because 8directions condition does not exist in standard event editor. Or atleast i dont know about it XD.
So how to determine when the direction is diagonal?

Does the X on the Picture indicate the Spot the Player would jump to?

Edit:
I tryed to use @Shaz code, but a Edited Version does not work in Event Command Conditional Branch:
Code:
$gameMap.regionId($gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction), $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction)) >= 105
There is no crash but it still triggers when standing on top of it instead of infront.
Yes, it works partly but only when we reach the specified Region sadly. Not one tile before. I am a little busy, so I cannot do much at the moment, but luckily weekend is coming and I have a lot of time to try more things.
The eventing is a little easier to accomplish, but I kind of like the challenge and to learn more about coding and the logic behind it. Plus the mere fact to just store information inside of variables got more clear to me after reading what @Shaz taught me in post 3. So I am grateful for lectures and education on that topic. I am slow to understand such things, the more I see it from all kinds of sources, the easier it becomes. So yes. Maybe we can find a solution together in the end!
Maybe we can find a solution together in the end!
I'm confused by the last few posts...doesn't the eventing solution that caethyril gave you in post #24 do exactly what you need? It seems like you already have a solution.
@ATT_Turan thank you very much i overlooked it, editing the () into it solved it and it works like a charm, in the Eventcommand conditional Branch Scriptline. This will surely help the OP, i believe he failed to clearly state that the codeline was not workling on his end. @caethyril also safed the Day.
Code:
$gameMap.regionId($gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction()), $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction())) >= 105
Just end this misery and close the thread finally.
If you want your thread closed, you can report it. That way the mods will actually see it.
Why giving up now? So close infront of the finish line. But it is ok. No worries.
As info, we are mostly no pros and even if we were, it can take some Forums posts to have a perfectly fitting solution at hand.

I learned much thanks to this Thread and all the Scriptlines. Even if its not useful for you at the moment, maybe in half a year you see it again and
suddenly it is helpful.
Thanks for this, the Thread was fun.
Why giving up now? So close infront of the finish line. But it is ok. No worries.
As info, we are mostly no pros and even if we were, it can take some Forums posts to have a perfectly fitting solution at hand.

I learned much thanks to this Thread and all the Scriptlines. Even if its not useful for you at the moment, maybe in half a year you see it again and
suddenly it is helpful.
Thanks for this, the Thread was fun.
Yes sorry. I do not give up on the functionality. I will make it work as I see fit myself with some tweaks thanks to the help you guys gave me with this.
And even tho what Shaz gave as an example did not quite work, I figured out how to make use of this as well. And I am able to expand onto it slowly. So it is very cool. And the difference when we made it a function compared to when it wasn't is also huge. Such little things are fun to learn and to understand.

I just originally asked for a script way to do all of this. I know you can do quite complex things with eventing, but ever since I started to use script calls and tweaked some Plugin script lines, I figured that scripts are more friendly for editing and pretty easy to customize (Also easy to break something of course). I just don't have too much time to learn JavaScript as well as ''Rpg Maker Engine'' and its functions in terms of Script.

When I do everything with eventing tho, my variables pages as well as switches and other things get unbelievably full. I also had used PlayerX and Y variables multiple times now because it interfered with some other functionality I used to determine the players position and then overwrote the other, if used the same variable. Haha.

So making a large project by one person with little time is a headache I tell you guys. You have to keep track of so many things and to remember all little things you did in the past. That's another reason why scripts feel so much easier. Cause you can often avoid some of the errors. Not all tho, because some scripts can also interfere with others. But so far it was easier to work out a solution by myself.

Anyways I am happy with my project so far and most things are working so well. Most mechanics are so cool and I am proud of them to have made them all by myself instead of copying it from others.
(Except from graphics and some plugins of course which I could never have done haha and some things might be inspired of course but always tweaked as much to make it an original).

So take care people stay safe. Thanks again for all the help.

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/script-call-javascript-help.151780/