0 votes

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.

in Engine by (24 points)

1 Answer

+1 vote

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 WE1421PLCFRM 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?

by (1,838 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.