r/howdidtheycodeit • u/ah7madaj3 • Nov 16 '23
Question How did they code Npc routine
In games like harvest moon each character have multiple places and routine like drinking in the bar between 6_7 cut the woods 4 days in the week they might go to they pathfind to thier target and most importantly they react to whats going on (rain,events,seasons,time of day, place activities and gifts) what kind of system can be made to manage all of these things.
6
Upvotes
18
u/gravelPoop Nov 16 '23
One approach would be need based priority system (I think game makers toolkit did video about how the Sims use this few months ago). But I would think that at least earlier Harvest Moon games have only pre-planned routines and some sub routines that make appear to do those things convincingly enough.
4
u/Hexatona Nov 16 '23
Each screen has a schedule.
Days have phases like Morning, Day, Evening, Night, and each NPC has a location they are when a phase is active. You can even have seasons as well, which adds another layer of complexity to where npcs can be at any given time. Keep track of the season, day, time of day - and then for any given screen, load up the schedule and see which NPCs are supposed to be active.
3
u/SuperSathanas Nov 16 '23
It could probably be done with something like an entity component system, or part of the existing component system. You let each entity/NPC own a schedule object, and you run update logic on them at whatever interval you determine. A naïve implementation of that schedule object might look like
When an NPC is created/loaded in/whatever, store an instance of NPCSchedule in an array or list somewhere. Give the NPC a pointer or index to that schedule. Push their tasks onto the vector or list or whatever you use. At whatever interval you decide, loop over your list of NPCSchedule, and if isActive is true, run update logic on it. Check if it's time to start a new activity. You'd of course also have to handle "popping" the NPC's into place if you load in a new part of the world where NPCs haven't been getting updated.