AI NPCS

Hey everyone.

Have a question (hopefully someone who tried this can provide an answer.)

I want to try and make a AI system in my game to where basic things are done by NPCS - similar to games like Skyrim, only this is done by a timed system.

For example: If in game the time is 6:30a, housewife walks over to the counter to prep food. System checks again in the same cell if its 7a, self switch A is true and the food shows at the counter where housewife is and preps around there till 11a.

For something like this as an example, is it possible to use only the in game block of code? Or will this be recommended to script out? Note that I'll be trying to give each NPC their own assigned schedule, which includes possible travel time across the map.

Or will this cause problems and lag due to impacting the engine? Thanks!
It's possible to do such a system with eventing, but getting things to sync up between maps would be a logistical nightmare.

OcRam has an NPC scheduler plugin for MZ; I'm not sure if it's compatible with MV but as it doesn't use any windows and events are coded the same way in both engines, I don't see why it wouldn't be besides the plugin commands possibly not being available, if it has any.
You only need the status of the current map to be correct, right? So if you stored the time in a variable you could have different event pages active depending on the value of the variable?
Yes. I don’t plan to have NPCs travel to different maps on foot. Only in its home cell. If I were to have them travel to another map, that’ll be a switch.

Only focused on the current map. Exterior and interior.
So after reviewing carefully how to plan this, I do not think this will be possible. Because RPG Maker has a limited amount of variables that can be used, along with switches.

That and I am finding it hard how to check per actor how to check and see if actors are in the correct area or not. I feel this all can only be done by a plugin.

I can, however, be limited to what per actor can do without overusing switches and variables. I can use switches to change actor locations depending on the time of day, but I'll experiment that when the time comes.
That and I am finding it hard how to check per actor how to check and see if actors are in the correct area or not. I feel this all can only be done by a plugin.
I'd like to offer some advice, but I think you might be mixing some terminology. When you say "actor" here, do you actually mean "event"?

There are event commands that can get the coordinates of an event into variables, so it should be pretty easy to check where the event is on the map and move them as determined by referencing your time system.

In fact, you could have one central autorun event on every map that checks the current location of every NPC event, references the time, and places them into the correct place every time the map loads. No need for a plugin for that.
I'd like to offer some advice, but I think you might be mixing some terminology. When you say "actor" here, do you actually mean "event"?

There are event commands that can get the coordinates of an event into variables, so it should be pretty easy to check where the event is on the map and move them as determined by referencing your time system.

In fact, you could have one central autorun event on every map that checks the current location of every NPC event, references the time, and places them into the correct place every time the map loads. No need for a plugin for that.
Yea events. I call them actors because they're NPCs.

I try testing Mother Schedule for example out and it doesn't seem to work right with the block code.

Ex. Block code only has if statements. It doesn't have if && if. So I would build it like for example if hr >= 6. if hr < 8, move mother here. Then have another conditional check under the same page check a different hour, then another and another. Mother doesn't follow the scheduled time and I set hr at 7 to be sure it revolves within the correct timeframe. So with that problem, I'm unsure if this will work.

Also, I have to consider whether if player is present or not. If player is present, rather have the NPCs move freely vs teleporting to that area and if player is not there, have them teleport into that area when player enters the map. It's a bit deeper than I thought.
What are you trying to have the NPCs do that you can't just event based on a global time variable? I've evented a time system before, although I haven't yet evented NPC behaviors, but I have imagined how I would do it. It only seems more logistically difficult if the NPCs are changing maps, but even then, I imagine you'd just activate a blank event page for every version of the NPC on each map they're not supposed to be on at a certain time?

If you're having trouble with limited game variables, have you looked into Yanfly's Self Switches and Variables?

Although I also can't see why you'd need these if you're just basing their behaviors on a single time variable. For example, if you want one event page to be active between two particular times, all you have to do is make that page trigger at the earlier time, and then make the next page trigger at the later time. As long as it's past the later time, and the order of the later page comes after the earlier page, then the later page will take precedence be active when the time comes. No need to specify "after this time, but before that time" in the same trigger.
What are you trying to have the NPCs do that you can't just event based on a global time variable? I've evented a time system before, although I haven't yet evented NPC behaviors, but I have imagined how I would do it. It only seems more logistically difficult if the NPCs are changing maps, but even then, I imagine you'd just activate a blank event page for every version of the NPC on each map they're not supposed to be on at a certain time?

If you're having trouble with limited game variables, have you looked into Yanfly's Self Switches and Variables?

Although I also can't see why you'd need these if you're just basing their behaviors on a single time variable. For example, if you want one event page to be active between two particular times, all you have to do is make that page trigger at the earlier time, and then make the next page trigger at the later time. As long as it's past the later time, and the order of the later page comes after the earlier page, then the later page will take precedence be active when the time comes. No need to specify "after this time, but before that time" in the same trigger.
Wait, so you're telling me under condition for variable that only contains the >=, that it will switch correctly without needing to check the >= and <? Because I was under the impression long as X >=, it will go on infinitely until X is less than the current defined X.

I'm just trying to give the game some NPC life. I don't want NPC's on a 256 x 256 map for example, or when I go into a small house see static NPCs. Want them to do something just to add a little life. Give them a schedule. Not to have them stay static.

I'll have to take a look at that plugin. I use yanfly alot. Just mostly the battle plugins.
Yea events. I call them actors because they're NPCs.
That's what I figured, but it's best to be precise since "actors" means something in RPG Maker.

I try testing Mother Schedule for example out and it doesn't seem to work right with the block code.

Ex. Block code only has if statements...
I'm sorry, I don't know what any of that means. What is Mother Schedule? What is "the block code"? Screenshots, yo.

Also, I have to consider whether if player is present or not. If player is present, rather have the NPCs move freely vs...
Well, you don't really. Events don't run if a player isn't present. Map events can only do anything when that map is loaded. And there's certainly no need for this kind of thing to be running continuously via a parallel common event.

Wait, so you're telling me under condition for variable that only contains the >=, that it will switch correctly without needing to check the >= and <? Because I was under the impression long as X >=, it will go on infinitely until X is less than the current defined X.
No - events will always use the highest numbered page whose conditions are fulfilled. So if you have event page 2 with a condition of a variable >=2, and event page 3 is that same variable >=3, as soon as you set that variable to 3 the event will change up to page 3.
Wait, so you're telling me under condition for variable that only contains the >=, that it will switch correctly without needing to check the >= and <? Because I was under the impression long as X >=, it will go on infinitely until X is less than the current defined X.
No - events will always use the highest numbered page whose conditions are fulfilled. So if you have event page 2 with a condition of a variable >=2, and event page 3 is that same variable >=3, as soon as you set that variable to 3 the event will change up to page 3.

Yes, exactly what ATT_Turan said. The highest event page number (or last in order) always takes priority, no matter what the conditions of any pages are. If the highest page's conditions are met (e.g. variable >= 3), then it will be active. If not, it will look at the next highest page to see if its conditions are met (e.g. if variable >= 2). So if your last event page has no conditions at all, it will always be the one active, even if every condition of every page before it is currently met.

I very rarely (if ever) find it necessary to explicitly check if a value is between two numbers, neither for a page's conditions, nor even if-else chains within the event itself (because you can always order the branches to check if the highest number is true first, and then work numerically backwards, and the same logic applies).
That's what I figured, but it's best to be precise since "actors" means something in RPG Maker.


I'm sorry, I don't know what any of that means. What is Mother Schedule? What is "the block code"? Screenshots, yo.


Well, you don't really. Events don't run if a player isn't present. Map events can only do anything when that map is loaded. And there's certainly no need for this kind of thing to be running continuously via a parallel common event.


No - events will always use the highest numbered page whose conditions are fulfilled. So if you have event page 2 with a condition of a variable >=2, and event page 3 is that same variable >=3, as soon as you set that variable to 3 the event will change up to page 3.
Ah, then this answers my concern. It won't be possible for me to setup a NPC schedule then because the schedule will only run if the player is present, which will be awkward if the player is present and the NPC is in a area they're not supposed to because the event wasn't running in the backend.

Granted, I was adventuring the idea of using common event to operate the schedule, but it can only do checks on switches and timing. Having the schedule itself will need to be in that cell for it to work right.

Oh wells. It was worth the idea.
Ah, then this answers my concern. It won't be possible for me to setup a NPC schedule then because the schedule will only run if the player is present, which will be awkward if the player is present and the NPC is in a area they're not supposed to because the event wasn't running in the backend.

Granted, I was adventuring the idea of using common event to operate the schedule, but it can only do checks on switches and timing. Having the schedule itself will need to be in that cell for it to work right.

Oh wells. It was worth the idea.

I don't see why not, though. If time is controlled via a master variable, and every NPC is basing their schedule on that time variable, then they should all always be up to date. Assuming you've set up each NPC event's page conditions appropriately, then as soon as the player enters the map, the correct page should automatically activate, no matter where that NPC was in their schedule the last time you left the map.

So like if you left the map with an NPC cooking dinner at 6pm, and then re-enter the map at 11pm when they're supposed to be in bed on a different map, they won't still be on this map making dinner just because you weren't here from 6pm to 11pm. The moment you enter the kitchen map, the NPC event there will check the time variable, realize it's 11pm, and then the highest priority event page with the condition encompassing the time being >= 11pm will immediately activate (presumably a blank page, since the NPC is supposed to be elsewhere - i.e. sleeping in their bedroom).

The main thing is that the NPC events' behaviors are dictated by a master, external, independent variable. What you're saying would absolutely be a concern if you were to set up their schedules by calling an action, then calling a wait command for a certain amount of time, then another action, then waiting another set amount of time, and so on - because those "wait" commands would be the only thing determining when the NPC event does the next thing, and they would not be counting when you're off the map.

However (aside from the fact that using wait commands would inherently be super inefficient and prone to bugging out), by using a master time variable to dictate when the NPCs do certain things instead, there are no processes that need to be continuously running within the NPC events themselves. Their behaviors are automatically controlled via a completely external variable. You only need to continuously update the time variable, and then the events will sort themselves out via their page conditions every time you enter a map, and on every frame thereafter. So, this way, the NPC events are not actually constantly running through every part of their cycle at all times. Rather, whenever it is relevant (i.e. when the player is on the map), it will be checked each frame what they're supposed to be doing right then, and if they aren't doing it, it'll immediately make them do it - thus effectively creating an illusion of a cycle, only when and where the player can see it.

In fact, you really don't want to have a million NPC events all active and updating on every map, 24/7, even if you could. That would cause massive performance issues.

Trust me, just try it, I think it will work.
Well I think I figured it out. However, I keep encountering a frustrating bug I cannot figure out and need help on why it is not working right. Screens are attached.

1673302934468.png

1673302960794.png

1673302982762.png



Been doing several tests here. First page is completely ignored and goes to the second page instead. When I change the 2nd page to action, first page works. If I'm understanding correctly, the 2nd page shouldn't even run until the page conditions are met first. First screen shows after all the runs, self switch A then turns on. 2nd and 3rd screen has been the troublesome page that decides to go first.

I've tried from loops to multiple condition checks, and nothing seems to work right. First page by itself is flawless AND does what I want it to do. It's the 2nd page (and possible future pages) that have an issue. Note that none of this operates through common event - all of it in the map.
Well I think I figured it out. However, I keep encountering a frustrating bug I cannot figure out and need help on why it is not working right. Screens are attached.

Been doing several tests here. First page is completely ignored and goes to the second page instead. When I change the 2nd page to action, first page works. If I'm understanding correctly, the 2nd page shouldn't even run until the page conditions are met first. First screen shows after all the runs, self switch A then turns on. 2nd and 3rd screen has been the troublesome page that decides to go first.

I've tried from loops to multiple condition checks, and nothing seems to work right. First page by itself is flawless AND does what I want it to do. It's the 2nd page (and possible future pages) that have an issue. Note that none of this operates through common event - all of it in the map.

Hmm... Lots of things with this, but I'll address the first big two that come to mind:

1) I don't think you need a loop encompassing an entire event page that already has its trigger set to "Parallel", as the entire page will loop by default. I doubt this is causing the issue, but it could be making things weird, so better safe than sorry.

2) What's controlling "player-present"? Is this a switch determining whether the player is on this map? If so, where is the code that checks and sets this value? If this value is set to false when the player is off the map and true when the player is on the map, then it will never trigger this part of the event's code, as map events only run when players are on the map. Therefore, it would always return as ON whenever this event is running, because the player must be on the map for it to run at all. Therefore, with the way this is set up, the event can never turn Self Switch A off once it's been turned on.

Of course, that's based on assumptions on my part, as to what that switch is and how it's controlled. I could be wrong about how you have it set up. But that's the first thing I'd want to look into.

The other things I noticed were mostly just ways the code could be cleaned up a bit/made more concise, but this shouldn't affect whether it works or not, y'know?

P.S. Side note, why don't you just make the schedule within the NPC event itself, directly making it behave differently based on what time it is? Also, how come it matters where the player is standing? These aren't judgmental questions BTW - just suggestions, and/or seeing if there's any reason why they can't be done.
Hmm... Lots of things with this, but I'll address the first big two that come to mind:

1) I don't think you need a loop encompassing an entire event page that already has its trigger set to "Parallel", as the entire page will loop by default. I doubt this is causing the issue, but it could be making things weird, so better safe than sorry.

2) What's controlling "player-present"? Is this a switch determining whether the player is on this map? If so, where is the code that checks and sets this value? If this value is set to false when the player is off the map and true when the player is on the map, then it will never trigger this part of the event's code, as map events only run when players are on the map. Therefore, it would always return as ON whenever this event is running, because the player must be on the map for it to run at all. Therefore, with the way this is set up, the event can never turn Self Switch A off once it's been turned on.

Of course, that's based on assumptions on my part, as to what that switch is and how it's controlled. I could be wrong about how you have it set up. But that's the first thing I'd want to look into.

The other things I noticed were mostly just ways the code could be cleaned up a bit/made more concise, but this shouldn't affect whether it works or not, y'know?

P.S. Side note, why don't you just make the schedule within the NPC event itself, directly making it behave differently based on what time it is? Also, how come it matters where the player is standing? These aren't judgmental questions BTW - just suggestions, and/or seeing if there's any reason why they can't be done.

The problem is, I want the player to interact with the NPC. To do so needs to be action button, not parallel. Hence why the event is on a separate page and the NPC carries the dialogue. Unless the NPC can function without the need of parallel, then I didn't know this AND could resolve the issue I am having.

Update: I just tested your idea out and didn't know I had this option... Alright, gonna revamp everything and have all actions done under NPC instead of a separate event.

Update: The idea works, but conflicts with checking whether player is present or not.

Final Update: My idea will not be possible without scripting it in. Something is not working right with the condition page setting. One can explain to me how it works, but when I logically view how conditions work, something must be met in order for it to run or it is ignored. No matter if I put it on the condition page itself or made a conditional branch, the action on scheduling is done wrong.

Ex. I set hr to 6. First page operates on hr 6 schedule. Next page has a hr 7 schedule, thus a condition is made on the page to check if hr is >= 7. If false, it should ignore it. The program doesn't. It ignores the rule of the logic and goes over to that page instead. Making no sense.

I've tried such example without plugins involved and strangely, this still happens. Don't understand how or why when the variable is not true. Even tried with switches and changes nothing. So the only solution would be scripting it out.
Wow. I was able to figure it out. Talk about overthinking it way too much. Provide the screens on what I did.

The problem I was having is that I was putting too much in one page. I was making per page do a variety of checks to check whether actions should be made or not. I never thought to make a page just for schedules and a page for position checking. Because I decide to separate the two in their own events, Mother is able to do her schedule correctly without teleporting to an area randomly, drift off the map, or not be in an area where she is not supposed to be in.

Self Switches some point may need to be replaced. For right now, I'm using them to try and save on Switch use. But tested this out twice and haven't had any issues since.

However, this does change things on how I want done. I think I should do the scheduling simple instead of complex for the time being.

1673466339924.png

1673466359118.png

1673466375148.png

1673466392453.png

1673466404883.png
The problem is, I want the player to interact with the NPC. To do so needs to be action button, not parallel.
Ahh, yes, you're right. I hadn't thought about that! This really depends on how complex your conditions are, though. It would be easy to just have different pages of the NPC event active depending on the time - but you are also checking for the player's position and whatnot, which could complicate it.

Unless the NPC can function without the need of parallel, then I didn't know this AND could resolve the issue I am having.
This could potentially be done within the NPC event's movement route (set to Custom), but this would require some scripting in order to check conditions. Movement routes are amazing because, if you can figure out how to write the script(s) that you need, you can make an event do probably anything as if it was a parallel event page - while still being interactable, because it doesn't use the actual event page. But yeah, you would need to know how to script it appropriately.

If you ever want to try that, feel free to send me a message and I could maybe help you work some of it out 🙂 If you told me what you wanted to do in the movement route, then if I know (or can figure out) how to script it, I could help you with what to type in the script field to make it happen.

Otherwise, what you've got going on now will probably work just fine.

Wow. I was able to figure it out. Talk about overthinking it way too much. Provide the screens on what I did.
Oh my goodness, yes, this is so much easier to understand at a glance than what you had before. All these things can theoretically all be checked on one page. But whatever was going wrong, it most likely had to do with the specific setup you had. Separating them out like this undoubtedly removes whatever the issue in your structure was.

The only thing I see now that I would caution could potentially cause a bug in the future is having two separate events trying to move "Mother" at the same time. Both events trigger when hr >= 6, so if the conditions in the "NPC Check" event's conditional branches ever happen to be true at the moment that hr becomes >= 6, then both events will try to move "Mother" at the same time. This is because there is nothing to stop the "NPC Schedule" event from still making her walk at the same time that "NPC Check" is changing her location. You could fix this by turning on a switch in "NPC Check", just before it moves her, and then add a condition to the "NPC Schedule" that this switch must be off in order for it to move her. Then turn the switch off again, right after "NPC Check" moves her, to allow "NPC Schedule" to return to normal functionality - without conflict.

Self Switches some point may need to be replaced. For right now, I'm using them to try and save on Switch use. But tested this out twice and haven't had any issues since.
This is what Yanfly's Self Switches & Variables plugin would be useful for 🙂 Although I think it costs money, but it's one of the absolute most useful plugins I've ever purchased - it's indispensable to most of my more complex systems, of just this sort of thing. It would let you create as many self switches as you want for an event, without cluttering up your master list of Game Switches. All you'd need is one master Game Switch for each type of self switch (e.g. A, B, C, D, E, F, G, etc.), and then that one master switch can be re-used for different events that each keep track of their own separate values.

Or even better, you could just use a self variable, and instead of having separate self switches A/B/C/D/etc. to switch to different pages, you could just use a single variable that ticks its value up or down to switch pages. So instead of turning on self switch A (and having to remember to turn it and all the others off when the day restarts its cycle at midnight), you would just change the self variable to = 1. Then instead of turning on self switch B, you would just change the same self variable to = 2, and so on.

You can still do the above (use a variable instead of many switches) without the plugin, but you'd just need to dedicate a Game Variable to each NPC to track their individual progress in their schedule. So whether you buy the plugin or not, I still highly recommend doing it this way, as it will allow you to switch between more than 4 event pages, while taking up way fewer Game Variables than it would if you were using Game Switches.
Ahh, yes, you're right. I hadn't thought about that! This really depends on how complex your conditions are, though. It would be easy to just have different pages of the NPC event active depending on the time - but you are also checking for the player's position and whatnot, which could complicate it.


This could potentially be done within the NPC event's movement route (set to Custom), but this would require some scripting in order to check conditions. Movement routes are amazing because, if you can figure out how to write the script(s) that you need, you can make an event do probably anything as if it was a parallel event page - while still being interactable, because it doesn't use the actual event page. But yeah, you would need to know how to script it appropriately.

If you ever want to try that, feel free to send me a message and I could maybe help you work some of it out 🙂 If you told me what you wanted to do in the movement route, then if I know (or can figure out) how to script it, I could help you with what to type in the script field to make it happen.

Otherwise, what you've got going on now will probably work just fine.


Oh my goodness, yes, this is so much easier to understand at a glance than what you had before. All these things can theoretically all be checked on one page. But whatever was going wrong, it most likely had to do with the specific setup you had. Separating them out like this undoubtedly removes whatever the issue in your structure was.

The only thing I see now that I would caution could potentially cause a bug in the future is having two separate events trying to move "Mother" at the same time. Both events trigger when hr >= 6, so if the conditions in the "NPC Check" event's conditional branches ever happen to be true at the moment that hr becomes >= 6, then both events will try to move "Mother" at the same time. This is because there is nothing to stop the "NPC Schedule" event from still making her walk at the same time that "NPC Check" is changing her location. You could fix this by turning on a switch in "NPC Check", just before it moves her, and then add a condition to the "NPC Schedule" that this switch must be off in order for it to move her. Then turn the switch off again, right after "NPC Check" moves her, to allow "NPC Schedule" to return to normal functionality - without conflict.


This is what Yanfly's Self Switches & Variables plugin would be useful for 🙂 Although I think it costs money, but it's one of the absolute most useful plugins I've ever purchased - it's indispensable to most of my more complex systems, of just this sort of thing. It would let you create as many self switches as you want for an event, without cluttering up your master list of Game Switches. All you'd need is one master Game Switch for each type of self switch (e.g. A, B, C, D, E, F, G, etc.), and then that one master switch can be re-used for different events that each keep track of their own separate values.

Or even better, you could just use a self variable, and instead of having separate self switches A/B/C/D/etc. to switch to different pages, you could just use a single variable that ticks its value up or down to switch pages. So instead of turning on self switch A (and having to remember to turn it and all the others off when the day restarts its cycle at midnight), you would just change the self variable to = 1. Then instead of turning on self switch B, you would just change the same self variable to = 2, and so on.

You can still do the above (use a variable instead of many switches) without the plugin, but you'd just need to dedicate a Game Variable to each NPC to track their individual progress in their schedule. So whether you buy the plugin or not, I still highly recommend doing it this way, as it will allow you to switch between more than 4 event pages, while taking up way fewer Game Variables than it would if you were using Game Switches.

It's funny how you warn me about this because I encountered a different bug in the making that was an easy fix. When the player arrived, the event does not stop running and with the NPC check under way force the actor to teleport to the character area while the same time run. So I merged the two events into one and done the following pics.

This stabilized everything.

The idea was to try and put the switches inside the common event, so at the beginning of the game when system is active, it stays active along with everything else. Time checks happen per cycle. >= or < checks are crucial to avoid any glitches in the time system itself. Once 24 hits, hr itself turns 0 and any switches that were active turn off. This creates the infinite loop on NPC Scheduling.

Then there's the check whether player is present or not. So in the conditional branch I had to make a few modifications. x and y for player, x and y for NPC. Had to check if player is facing up, for entry in RPG Maker alot of the times is that. Course, if it isn't in some cases it is simple as changing the direction the player faces. This is a simple check to confirm before checking the cords of player and npc. Other way around and it bugs out.

Check confirms, ignores making the npc move and auto tellies to the area it should be at before map completes its load. Whereas before, it was doing both the same time.

Never did I think this was possible, but I guess it is. It's more intense than the average scheduling system I thought of, so the idea of making the same NPC active both interior and exterior will be a pass. I'm only going to practice interior or exterior per NPC, and that is if I allow such NPC to have a schedule. Some will be static.

Oh and reference older pics, this idea drastically decreased the amount of page use.

Edit: Forgot to edit my comment in the NPC Schedule from 24 to 0. Got that taken care of.

1673545257454.png

1673545278045.png


1673545808835.png

1673545826255.png
games like Skyrim
if you have Skyrim (I had Oblivion), go talk to a scheduled NPC and stall them way past their planned exit.
you'll probably figure out the system just by looking at what they do when that happens.

*choreography* in games should be way easier to understand when people understands that time is linear, and that an NPC can't be half-present or half-gone, and can't be in two places at the same *time* (which, again, is linear)
you have to cut the schedule into manageable bits so that it allows for things like "going out, inconditional" to avoid being stopped by the player.
"being at X" should only be valid after "entering X" and before "exiting X"... if the NPC is "AT X", it's available, and if it's going in or out, it's not, despite "being" there.

Original thread: https://forums.rpgmakerweb.com/threads/ai-npcs.153809/