Script Call / Javascript Help

Hello everybody!

I have posted in the thread ''JavaScript questions that don't deserve their own thread'' to be told to create my own thread!
And seems like I won't get around writing a proper function, which I am too noob with to do.
(Basic script calls more or less are already enough if not too overwhelming to handle for me)

Alright so what I need help with, because my brain cannot get around this and I really want it to be solved somehow:

Code:
if($gameMap.regionId($gamePlayer.x, $gamePlayer.y) < 105)
{
 console.log(true);
}
else if($gameMap.regionId($gamePlayer.x, $gamePlayer.y) >= 105)
{
 console.log(false);
}

In this example - which is fairly easy to understand - it returns true whenever the player is standing on Region with number 104 or below (Or no number at all).
It returns false whenever the player is standing on Region with number 105 and above.

What I wish to accomplish however, is that the game / event or whatever I call this script from, will know BEFORE the player or event even gets on top of the Region(s) and then turns to false! Like 1 or 2 tiles away it realizes ''Oh there is a wall or blocked Region up ahead so I cannot do this action now because I require at least 3 tiles distance in x and y pos. Or ohh how sad, I have to disappear or be destroyed in 2 or 1 tiles before I collide with specified Region''.

My brain would think it is as simple to do something to add and substract numbers to and from the respective x and y positions.
But that sadly did not work and just shifted the positions around to mess everything up completely.

So any help with this would be appreciated. And if possible, I would want to do this with scripts in Engine and not with Plugins.
However, if the Plugin is definitely 100% surely proved to work and never interfere with anything else ever even with the worst written other Plugins, then I would also take this.

Thanks for your time!
this should do the trick, if you want it 1-2 tiles away.

// for facing down
Code:
$gameMap.regionId($gamePlayer.x, $gamePlayer.y - 1));
if ($gameMap.regionId < 105) {
$gameMap.regionId($gamePlayer.x, $gamePlayer.y - 2));
};

you need them for left (player.x + 1, player.y right x -1,
up + 1 and -1 might be reversed, so you need to check them.

as you want to check the face direct of the player and the ID.
so the code might be longer if its bigger.
if ($gameMap.regionId < 105)
This doesn't seem quite right ... shouldn't there be a variable there?

$gameMap.roundXWithDirection and $gameMap.roundYWithDirection will give you the tile immediately in front of the player, no matter which way they are facing.

So if you want to check the tile immediately in front, do this:
Code:
var x1 = $gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction);
var y1 = $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction);
if ($gameMap.regionId(x1, y1) < 105) { ... }

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) { ... }

Note, you are doing it the long way ...
this
Code:
if($gameMap.regionId($gamePlayer.x, $gamePlayer.y) < 105)
{
 console.log(true);
}
else if($gameMap.regionId($gamePlayer.x, $gamePlayer.y) >= 105)
{
 console.log(false);
}

is the same as this
Code:
console.log($gameMap.regionId($gamePlayer.x, $gamePlayer.y) < 105)

Code:
$gameMap.regionId($gamePlayer.x, $gamePlayer.y) < 105
will return true if the region id is 104 or less, and will return false if the region id is 105 or greater. You don't have to do an else and check for the opposite condition.
I didn't know the roundWithXDirection /YDirection.
it's interesting for that approach. far shorter as well.
Also I wanted to jump in here and clear up a misunderstanding. OP talks about their code 'returning' true or false. However 'return a value' has a very specific meaning in the comtext of programming.
JavaScript:
/* This function prints a value to the console. It returns nothing. */
foo = function () {
   console.log(5);
}

/* Because the function returns nothing, when we try to use it the following will occur */
let x = foo(); //prints 5
console.log(x); //prints the text: undefined

/* This function prints nothing. It returns the value 5 */
bar = function () {
   return 5;
}

/* Because this function actually returns a value we can use it later on.
If we go through the same steps as above the following will occur. */
let x = bar(); //does not print anything
console.log(x); //prints 5
This doesn't seem quite right ... shouldn't there be a variable there?

$gameMap.roundXWithDirection and $gameMap.roundYWithDirection will give you the tile immediately in front of the player, no matter which way they are facing.

So if you want to check the tile immediately in front, do this:
Code:
var x1 = $gameMap.roundXWithDirection($gamePlayer.x, $gamePlayer.direction);
var y1 = $gameMap.roundYWithDirection($gamePlayer.y, $gamePlayer.direction);
if ($gameMap.regionId(x1, y1) < 105) { ... }

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) { ... }

Note, you are doing it the long way ...
this
Code:
if($gameMap.regionId($gamePlayer.x, $gamePlayer.y) < 105)
{
 console.log(true);
}
else if($gameMap.regionId($gamePlayer.x, $gamePlayer.y) >= 105)
{
 console.log(false);
}

is the same as this
Code:
console.log($gameMap.regionId($gamePlayer.x, $gamePlayer.y) < 105)

Code:
$gameMap.regionId($gamePlayer.x, $gamePlayer.y) < 105
will return true if the region id is 104 or less, and will return false if the region id is 105 or greater. You don't have to do an else and check for the opposite condition.
This roundY/XWithDirection I have not seen before actually might come in handy thanks for that!

Anyways but sorry maybe I understand it wrong or do something wrong or even explained it badly but I have tried this in a parallel event and it does not seem to work differently from what I did. I do not get a change when I am 1, 2 or more tiles away from said Region.

So when I walk around freely on a map, nothing should happen right? But when I am 1, 2 (or specified otherwise) tiles away from the specific Region in my example 105, it should start to notice ''Ohh player / event is 2 tiles away from this Region!'' and then if 1 tile away ''Ohh player / event is 1 tile away from this Region!'' And I want to have control 1, 2 or so tiles before of this, to set booleans to true / false.

Lets try it like this maybe...: If we have 1 or 2 tiles between Event / Player and the specified Region (105 in my example), we do this $gamePlayer.setThrough(false); otherwise if we are 3 or more tiles away from that Region we simply do that: $gamePlayer.setThrough(true);

If this is already possible with your example, I am very sorry to fail you. I am probably looking into too many script call things without knowledge at all, which might have fried my brain cells already.
Can I come in with a non-coder's suggestion? Let us say that the forbidden region is, as per the OP, regions 105 or above. Paint region 104 on all surrounding tiles 3 deep. Have region 104 call up whatever it is you want to want to warn the player about.
Also I wanted to jump in here and clear up a misunderstanding. OP talks about their code 'returning' true or false. However 'return a value' has a very specific meaning in the comtext of programming.
JavaScript:
/* This function prints a value to the console. It returns nothing. */
foo = function () {
   console.log(5);
}

/* Because the function returns nothing, when we try to use it the following will occur */
let x = foo(); //prints 5
console.log(x); //prints the text: undefined

/* This function prints nothing. It returns the value 5 */
bar = function () {
   return 5;
}

/* Because this function actually returns a value we can use it later on.
If we go through the same steps as above the following will occur. */
let x = bar(); //does not print anything
console.log(x); //prints 5
Thank you for clarifying this. I have made a mistake when I gave the console.log(true/false) example.
What I simply used it for was to return text of true / false. So I forgot to add the "" or '' respectively.
It was only to check whether the player or event was on, close to or not on the specified or a Region itself at all.

But I like people helping with every detail. Even things which were not asked! Thank you I will try to get this into my head.
Can I come in with a non-coder's suggestion? Let us say that the forbidden region is, as per the OP, regions 105 or above. Paint region 104 on all surrounding tiles 3 deep. Have region 104 call up whatever it is you want to want to warn the player about.
I appreciate this thought and I would have done this already if I could. However I am using Region numbers for a lot of things which are calculating other things in the background. Therefore it is important to me, that I can manipulate certain things with scripts. Scripts are even better to work with than eventing things, because you can use them in more ways and turn them off or on with a different function, event or script as well.

Another reason why I need to block off Regions completely is also to make them fully inaccessable by all means. Like even blocking it via the ''through'' option. And not only that, but since I am using Chrono Engine, I can use this script to check whether the (Skill projectile) is about to collide with ''a wall'' (Specified Region) or if it was some Event (Enemy), so that it behaves differently. While on Enemies it could have a piercing effect and on a ''wall'' or ''End of the map'' it simply cannot pierce through and explodes or disappears or whatever.

Same for throwing ''Objects''(Events) etc. it could all be done by that script if I could make it work, right? But again I fail to do this. So I try to find help on here.
I have the feeling you want to build atleast 3+ different things with this, you wrote it yourself.
A Solution that fits in every case, will need much more details. If its even possible to fit all in one.
But i guess it would be easier to build one by one, because things working for Arrows, might not work for Birds on through Flying around, and doing things with the Player that work easily might absolutly not Work with Event NPC.
I mean maybe multiple smal Solutions solve this while one big one for everything is hard to achieve.
if possible, I would want to do this with scripts in Engine and not with Plugins.
I feel like you might not correctly understand what plugins are - they're just code. There's no difference between you
A - writing a chunk of code and putting it into a Script event command, or
B - putting that code into a function in a plugin and calling that function from your event

So when I walk around freely on a map, nothing should happen right? But when I am 1, 2 (or specified otherwise) tiles away from the specific Region in my example 105, it should start to notice
So the problem with this is it can get extremely computer-intensive. If you start saying "scan every map tile within a potentially large radius every time I take a step," that can have a negative effect on your game.

Keep it limited. The best way to do it would be to only call it when you actually need to do an action and you see if it's viable - but you might be trying to have some on-screen indicator, like your "shoot arrow" button greys out if you're within two squares of a region...I don't know, you still haven't said specifically what you're trying to achieve.

But here's a quick example of checking whether you're up to two spaces away from a specified region ID.

Code:
function NearRegion(id)
{
// Looking two spaces in each direction
    for (var x=$gamePlayer.x-2; x<=$gamePlayer.x+2; x++)
    {
        for (var y=$gamePlayer.y-2; y<=$gamePlayer.y+2; y++)
        {
// If the coordinates are out of bounds, skip checking them
            if (x<0 || y<0 || x>=$dataMap.width || y>=$dataMap.height)
                continue;

            if ($gameMap.regionId(x, y)==id)
                return true;
        }
    }

    return false;
}

Copy that, save it as a *.js file in your plugins folder, then add it to your plugin manager. Then anytime in your game you can use a script field to call, for example, NearRegion(105) to get true or false. You can use it directly in a Conditional Branch, you can set a game variable to it, whatever.
I feel like you might not correctly understand what plugins are - they're just code. There's no difference between you
A - writing a chunk of code and putting it into a Script event command, or
B - putting that code into a function in a plugin and calling that function from your event


So the problem with this is it can get extremely computer-intensive. If you start saying "scan every map tile within a potentially large radius every time I take a step," that can have a negative effect on your game.

Keep it limited. The best way to do it would be to only call it when you actually need to do an action and you see if it's viable - but you might be trying to have some on-screen indicator, like your "shoot arrow" button greys out if you're within two squares of a region...I don't know, you still haven't said specifically what you're trying to achieve.

But here's a quick example of checking whether you're up to two spaces away from a specified region ID.

Code:
function NearRegion(id)
{
// Looking two spaces in each direction
    for (var x=$gamePlayer.x-2; x<=$gamePlayer.x+2; x++)
    {
        for (var y=$gamePlayer.y-2; y<=$gamePlayer.y+2; y++)
        {
// If the coordinates are out of bounds, skip checking them
            if (x<0 || y<0 || x>=$dataMaps[$gameMap.mapId()].width || y>=$dataMaps[$gameMap.mapId()].height)
                continue;

            if ($gameMap.regionId(x, y)==id)
                return true;
        }
    }

    return false;
}

Copy that, save it as a *.js file in your plugins folder, then add it to your plugin manager. Then anytime in your game you can use a script field to call, for example, NearRegion(105) to get true or false. You can use it directly in a Conditional Branch, you can set a game variable to it, whatever.
Where are you getting $dataMaps from? There's only $dataMapInfos or $dataMap.
Where are you getting $dataMaps from? There's only $dataMapInfos or $dataMap.
I'm getting from an extra "S" that my fingers typed, obviously 🤣

It's not my fault that the devs are dumb and every other database array is plural 😛
I'm getting from an extra "S" that my fingers typed, obviously 🤣

It's not my fault that the devs are dumb and every other database array is plural 😛
About that... $dataMap contains the details of the current map; it isn't an array.
About that... $dataMap contains the details of the current map; it isn't an array.
Why you gotta be harshing my mellow, yo? 🤣

Just go ahead and tell me I'm running my D&D campaign wrong and raising my kid to be a gangster. It's okay, I can take it.
Why you gotta be harshing my mellow, yo? 🤣

Just go ahead and tell me I'm running my D&D campaign wrong and raising my kid to be a gangster. It's okay, I can take it.
You're running your D&D campaign wrong and raising your kid to be a gangster.
You're running your D&D campaign wrong and raising your kid to be a gangster.
;_;
I feel like you might not correctly understand what plugins are - they're just code. There's no difference between you
A - writing a chunk of code and putting it into a Script event command, or
B - putting that code into a function in a plugin and calling that function from your event


So the problem with this is it can get extremely computer-intensive. If you start saying "scan every map tile within a potentially large radius every time I take a step," that can have a negative effect on your game.

Keep it limited. The best way to do it would be to only call it when you actually need to do an action and you see if it's viable - but you might be trying to have some on-screen indicator, like your "shoot arrow" button greys out if you're within two squares of a region...I don't know, you still haven't said specifically what you're trying to achieve.

But here's a quick example of checking whether you're up to two spaces away from a specified region ID.

Code:
function NearRegion(id)
{
// Looking two spaces in each direction
    for (var x=$gamePlayer.x-2; x<=$gamePlayer.x+2; x++)
    {
        for (var y=$gamePlayer.y-2; y<=$gamePlayer.y+2; y++)
        {
// If the coordinates are out of bounds, skip checking them
            if (x<0 || y<0 || x>=$dataMaps[$gameMap.mapId()].width || y>=$dataMaps[$gameMap.mapId()].height)
                continue;

            if ($gameMap.regionId(x, y)==id)
                return true;
        }
    }

    return false;
}

Copy that, save it as a *.js file in your plugins folder, then add it to your plugin manager. Then anytime in your game you can use a script field to call, for example, NearRegion(105) to get true or false. You can use it directly in a Conditional Branch, you can set a game variable to it, whatever.
Sorry if I am not clear enough.

I will try to explain it once more:

I want a check to happen if the Player is 1 or 2 or so tiles in front of a certain Region, as stated above, I picked 105 because I am using a ton of Regions below this value, so I won't get into issues later on.

I just got an idea maybe this helps me to explain it further. How about in a script call combined with a movement route. Let's say I make a ''Blink'' or ''Dash'' kind of Skill which moves the player forward by 3, 4 or 5 tiles. I would want the ''Through'' option to be enabled, to blink / dash through certain obstacles such as Events or ''basic'' blocked off areas by Region restriction, where you cannot walk through without through enabled. However, I would want a specified Region (in my example Region number 105) to be not passable EVER, even with through on. So my idea is to have a check between every step inside this movement route with a script to see whether the player is close to the inpassable Region or not. And if so, then the ''through'' option will remain false. So ideally a script which checks between every forward step, would be perfect. Because I would want to use the Skill even when the player is right in front of the impassable Region but never be able to pass through.

Edit: I quickly made an example and put it into a .gif on the project. Hope this helps?
I want a check to happen if the Player is 1 or 2 or so tiles in front of a certain Region.
You were told how to do that by Shaz in post #3. You responded and said it didn't work, but you didn't post screenshots of what your event looks like so we can't give any additional advice.

Let's say I make a ''Blink'' or ''Dash'' kind of Skill which moves the player forward by 3, 4 or 5 tiles...So my idea is to have a check between every step inside this movement route with a script to see whether the player is close to the inpassable Region or not.
That's a pretty inefficient way to do it. It makes more sense to check one time when you activate the skill how far forward you can go and set the destination point, then move.
You were told how to do that by Shaz in post #3. You responded and said it didn't work, but you didn't post screenshots of what your event looks like so we can't give any additional advice.


That's a pretty inefficient way to do it. It makes more sense to check one time when you activate the skill how far forward you can go and set the destination point, then move.
I have used a mix of movement routes without script before. This should be sufficient as an example tho.
I did not know how to use $gamePlayer.moveForward(); multiple times effectively in a script call other than copying it twice or three times. So I only left it at 1 step forward which is also sufficient for this example. Because when I am in front of the Wall I can still hop onto it regardless of the steps I do.

Also to show the example region number (Yes I have used Region numbers on the ''free tiles'' before too. No change).

And yes again: 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.

Original thread: https://forums.rpgmakerweb.com/threads/script-call-javascript-help.151780/