Best way to implement an NPC schedule

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By pbaggers

I’m trying to make a game that runs off of real time with NPCs that do different things at different times of day, and different days of the week.

I realize this is an incredibly ambitious thing to try and do, and I’d like to know where I’d even begin to implement such a system.

I’ve thought about giving each NPC their own timetable telling them where to be and what they should be doing at what time, and I’ve tried doing this via a dictionary, but it’s resulted in an incredibly long and messy script and I wonder if there’s a better solution to my problem.

:bust_in_silhouette: Reply From: johnygames

You might want to keep things separate. Maybe remove the timetable from your code and create a json file with all the necessary information. Then import the json file at runtime and have NPCs read off of that. It should consist of the NPC’s name or ID and all the traits you want them to have.

Another solution that I can think of is to create export variables (namely variables that are visible in the editor) and fill them in with all the necessary information. For example, you could create slots for time of day, day of the week, activity, place etc. Then the main script of your game will be checking for those variables iteratively through a list of all NPCs and sort out where and when they need to be as well as what they should be doing.

Finally, you could create a single string variable (let’s name it a state variable) for all NPCs that can be broken down into separate indicators, kind of like a hash function. For example, the string value WE_14_21_PLC_FRM could be interpreted as Wednesday, 14:00-21:00 at the Palace farming. Your code then reads all the NPCs’ state variables and sets their state accordingly.

In any case, the key thing to remember is that your code becomes verbose if you have to include every single detail. What is more, rewriting the same code for all of your NPCs sure is wasteful. Keep NPCs in a list or group → iterate through said list/group → check attributes once for each item in list–> have main script regulate the game.

As a side note, you could even experiment with putting NPCs in lists/groups based on their common attributes. As far as I know, a single object can be part of several groups at the same time, so why not assign your NPCs in different groups based on where you want them?

This is a lot to take in, but helpful nonetheless. Presently, I have a timetable that is one big dictionary for all NPCs in the game, stored in a singleton node (My game isn’t huge btw, I plan on having no more than a couple of dozen characters). I think I might go with your single string idea, because it could shave off hundreds or possibly thousands of lines of code.

pbaggers | 2020-08-15 15:06