Hello there! So I’m working on my first big project and I’m trying to figure out how to have NPCs that will have daily schedules.
I’ve got a system that is working ok right now but I’m posting on here because I think it’s a little brute force and I wonder that there isn’t a more flexible way of doing this.
Here is what I’m doing:
I have a world clock that goes from 00:00 to 23:59 and then repeats as the day night cycle.
Each NPC has an AI script and a Schedule script attached. The Schedule just holds 24 ScriptableObjects that work like enums to determine what kind of scheduled activity the NPC can do at that hour. For example:
The first SO slot in the schedule refers to the hour of 00:00 - 01:00 and it might have the SO SleepActivity. This is nice, since I can go through each NPC and set up their schedule on the inspector.
The AI script checks what time it is in the world clock and checks what activity is set up in the Schedule for that hour. So I can change activities every hour.
This works fine, though I would like to extend it for days of the week eventually. I just wonder if there’s a better way of doing this. I’d love to have more intervals than hourly for example, but I don’t love the idea of having to set up 48 if statements to check the time of day if, for example, I use 30 minute increments.
Any ideas on how this could be made a bit more flexible? Maybe I’m doing it fine already, but I figured I’d ask since this is my first time doing something like this.
EDIT: Adding some of my code to make it a bit more clear.
This is the Schedule script attached to each NPC.
public class Schedule : MonoBehaviour
{
public ScheduleActivity blockOne;
public ScheduleActivity blockTwo;
...
public ScheduleActivity blockTwentyFour;
}
So the Schedule component takes in ScheduleActivities which are empty scriptable objects being used just like enums. Currently I have 3 types of ScheduleActivities; sleep, work, freeTime. Each block represents an hour of the day, blockOne is 00:00 - 01:00, blockTwo is 01:00 to 02:00, etc… So I can determine what activity each NPC should be doing a specific hour.
Then, the AI script which is also attached to each NPC checks the TimeManager and determines what block is currently active, and then prints out the name of the activity the NPC should be doing (but I can move the NPC to the activity location without too much hassle, this is just for prototyping) with the following code:
if (timeManager.Hours >= 0 && timeManager.Hours < 1)
{
Debug.Log(schedule.blockOne.name);[/INDENT]
}
else if (timeManager.Hours >= 1 && timeManager.Hours < 2)
{
Debug.Log(schedule.blockTwo.name);[/INDENT]
...
else if (timeManager.Hours >= 23)
{
Debug.Log(schedule.blockTwentyFour.name);[/INDENT]
}
So I skipped the ones in between, but yeah it’s 24 if statements, and that’s only checking per hour. If I wanted a system where I could change an NPC’s schedule every 30 or 15 minutes it would be even more if statements. This is the part of the system that’s really bugging me right now. I’m sure there’s a better way to implement this while still being able to have NPCs have unique schedules that can be easily modified.