Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

Keeping an NPC In a Region

MugenDraco

Villager
Xy$
0.21
I want to keep a randomly moving NPC inside a certain area. I've marked this area with a Region ID of 1. I have a Parallel event running to check if the NPC's random movements take her outside of Region 1. I'm using a conditional statement, and the If clause is the following script: $gameMap.events(this.eventId).regionId !== 1

I've set it up so that if the statement evaluates to true, to move her to the right, back inside the area, since the only exit is on the left side of the area. But it always evaluates to true for some reason, as if the Region ID isn't 1, which it obviously is. So she's just moving to the right indefinitely.

I Googled the syntax of events and region IDs for over an hour to stop all the "can't find property/function" errors I was getting, so I'm fairly certain the code in the script is correct. I have no idea why it's saying the region ID of the NPC isn't 1 when it is. Does anyone have any ideas what could be causing this?
 

CT_Bolt

Global Moderator
Staff member
Resource Team
Xy$
0.02
I want to keep a randomly moving NPC inside a certain area. I've marked this area with a Region ID of 1. I have a Parallel event running to check if the NPC's random movements take her outside of Region 1. I'm using a conditional statement, and the If clause is the following script: $gameMap.events(this.eventId).regionId !== 1

I've set it up so that if the statement evaluates to true, to move her to the right, back inside the area, since the only exit is on the left side of the area. But it always evaluates to true for some reason, as if the Region ID isn't 1, which it obviously is. So she's just moving to the right indefinitely.

I Googled the syntax of events and region IDs for over an hour to stop all the "can't find property/function" errors I was getting, so I'm fairly certain the code in the script is correct. I have no idea why it's saying the region ID of the NPC isn't 1 when it is. Does anyone have any ideas what could be causing this?
You could use the Region Restrictions Plugin by Yanfly: Click Here to View (cool)

Let me know if that will work for what you need. (cute)
 

MugenDraco

Villager
Xy$
0.21
How did I overlook that plugin? That was exactly what I needed. Thanks. Though as a programmer I'm still perplexed as to why the script didn't evaluate correctly.
 

CT_Bolt

Global Moderator
Staff member
Resource Team
Xy$
0.02
No problem. Happy to help. (cool)

Total guessing here haven't tested but maybe try this?

Change this $gameMap.events(this.eventId).regionId !== 1
to this:
$gameMap.events()[this._eventId-1].regionId() !== 1

Maybe?
 
Last edited:

MugenDraco

Villager
Xy$
0.21
Nope, no underscore. Threw an error. From what I've Googled, that used to be the case though. They've changed naming conventions of variables and functions over the different versions quite a few times. It made it tough just getting the game to run when I first started messing around with scripts.
 

CT_Bolt

Global Moderator
Staff member
Resource Team
Xy$
0.02
Nope, no underscore. Threw an error. From what I've Googled, that used to be the case though. They've changed naming conventions of variables and functions over the different versions quite a few times. It made it tough just getting the game to run when I first started messing around with scripts.
Oh sorry about that I edited the above post after I tested it is this...

This is how you get & test an events regionId...
JavaScript:
$gameMap.events()[this._eventId-1].regionId() !== 1
Screenshot of My Event Setup:
In this example the event the walks to the right constantly but will walk left when it hits regionId 1.



Here's what's going on...
  • Get the events for the map using a function from the Game_Map Object which returns an array of events.
  • Since this is an array we start at 0 unlike the event ID which starts at 1. Therefore we subtract 1.
  • Then we call the regionId function from the GameEvent Object which is derived from the Game_CharacterBase Object.
  • This will result in the regionId for the event.

Yay!

Happy Game Making. (cool)(icecream)

Please test and let me know if that works for you also. (cute)
 
Last edited:

MugenDraco

Villager
Xy$
0.21
Very helpful. Thanks again. I tested it too. It works. The Region Restriction plugin is an easier and more efficient way of doing it, but I'll still keep this script in my notes. Who knows? It may come in handy down the road.
 
Top