2
\$\begingroup\$

I'm making a basic survival RPG in Unity 3d. I'm separating my world into different scenes to save memory but I'm not sure how I'm going to program NPC daily routines.

For example, if the player arrives in a town scene in the morning, how do I ensure that the NPCs are working in appropriate places with the appropriate equipment? I suppose I could hard code it so NPCs are in certain places at certain times. Therefore, if the player arrives at 9:00am then all the NPCs will move to their 9:00am places. I'm just wondering if there is a better way of doing it than that. I can see myself needing to write a lot of code to cover all the possibilities so it could get really messy. I was hoping there was some way I could have the NPC routines continue seamlessly in the background but this would be taxing on memory -if it's even possible.

Any ideas/pointers on this issue would be appreciated.

\$\endgroup\$
4
\$\begingroup\$

Don't hard-code it, or it'll indeed end up very messy.

You need to script the NPCs daily routines into some data file (XML or other).

Something along the lines of:

<npc name="george">
  <schedule start="0:00" end="8:00">
    <sleep at="home"/>
  </schedule>
  <schedule start="8:00" end="9:00">
    <walk leave="home" destination="smithy"/>
  </schedule>
  <schedule start="9:00" end="17:00">
    <runshop at="smithy"/>
  </schedule>
  <schedule start="17:00" end="18:00">
    <walk leave="smithy" destination="home"/>
  </schedule>
  <schedule start="18:00" end="24:00"> <!-- note: no warp-around -->
    <sleep at="home"/>
  </schedule>
</npc>

This way you can figure out where every NPC is supposed to be and initialize them properly at loading time.

This doubles as your NPC event script file.

\$\endgroup\$
  • \$\begingroup\$ Thank you for replying (I cannot upvote yet). So if the player arrives at 8:20, would I need additional information about how close the blacksmith is to his workshop based on an estimation of how long it takes him to walk there? Otherwise, whether the player arrived at 8:00 or 8:59, I imagine the smith would always begin walking to the shop from that point. \$\endgroup\$ – OwenAlexander Jan 13 '15 at 7:14
  • \$\begingroup\$ Yes. You interpolate the walk to initialize the NPC where it should be between leave-destination according to start-end time. I kept the example simple but it should also have a list of locations by name somewhere, maybe in the same file (where is george's bed) or maybe in a town file with the locations of everything in town. \$\endgroup\$ – Stephane Hockenhull Jan 13 '15 at 7:30
  • \$\begingroup\$ Thank you. I've never used XML before. All the AI I've coded so far has been active in the current scene so I've just used finite state machines. This won't work so well when loading a scene so I will definitely look into data files now. Thanks for the information. \$\endgroup\$ – OwenAlexander Jan 13 '15 at 7:33
  • \$\begingroup\$ Actually, since this is Unity you can use game object names in your scene for setting the locations. Also make sure to write a schedule verifier script that checks for errors in the schedule (locations that don't exist, schedule overlaps, etc.) It'll save your a lot of time and hair pulling. \$\endgroup\$ – Stephane Hockenhull Jan 13 '15 at 7:36
  • \$\begingroup\$ Yes I suppose the path to each location could consist of a series of empty game objects which could then be used as instantiation points when the player enters the scene. A schedule verifier script sounds pretty important especially as buildings etc. can be destroyed in my project so that could lead to some strange bugs. Are there any resources you would recommend so I can get a better understanding of the points you raised? Particularly data bases for AI/daily routines and examples of schedule verifier scripts? \$\endgroup\$ – OwenAlexander Jan 13 '15 at 7:47
0
\$\begingroup\$

Since this is Unity, I do not recommend working with XML files. The work needed to parse them is just useless. Instead, focus on creating custom editors for your scripts. This will help you in the long run.

  1. Create a script for the basic routine. In the editor of this script, you should be able to enter a location and an activity. If you want extra stuff, you may want to use some base class for routines and extend it as needed. A NPC will have multiple routine components, and you will choose between them depending on the current time. Identify routines by name, or something else, for each NPC. Make sure the Location and Activity properties of the routine are editable in Unity's editor. More exactly, you want to be able to select a location from the scene. Sleep routine for the king? Find the bed in the scene's list of objects and drag it to the routine's location slot :) Pretty handy.
  2. Create a script for the schedule. Schedules map timeframes to routine scripts. Dictionary<Timeframe, Routine>.
  3. Attach a schedule to each of your NPCs. Schedules will manage the routine components that you have and will add.
  4. Create a custom editor for your schedule script so that it provides a nice and GUI-driven way to select the hours for each routine.
  5. Make a nice red glowing warning message in the editor if you haven't filled the 24 hours a NPC needs. This is useful, and will catch plenty of bugs. The same applies for empty location slots or empty activities.
  6. Add any sort of other functionality you might want, extending the editor will make it a breeze.

Extend the editor to add custom functionality. It's worth it. Don't ignore Unity's engine features by using workflows that fit other engines better.

When the player enters a level at a specific hour, you have a few choices. Spawn multiple NPCs in similar places and make them walk to their routine's location. This might look weird. Some old games did it (Gothic). Personally, I'd auto-teleport all NPCs to their routine's location when the level starts.

me.teleportAt(mySchedule.myCurrentRoutine.location);

\$\endgroup\$
  • \$\begingroup\$ The time spent writing the simple XML tag processor will be much shorter than the time spent making and debugging an editor extension. K.I.S.S. You need someone working on and debugging an editor extension almost full-time during the project to be able to edit the schedules conveniently in Unity. The artist cannot edit the scene at the same time as the level designer edits the schedule as this causes merge conflicts. You cannot use standard text tools to search & fix the schedule files. Don't underestimate the time it takes to make a proper extension when you can just text-edit and xml file. \$\endgroup\$ – Stephane Hockenhull Jan 13 '15 at 16:52
  • \$\begingroup\$ Actually, no, it isn't. The time debugging bad behavior caused by otherwise valid XML (which is the only thing your parser can say) is likely to be greater than the time spent on the extension. The extension is built incrementally as the project progresses. A first version is as simple as editing the contents of a dictionary. \$\endgroup\$ – user15805 Jan 13 '15 at 17:08
  • \$\begingroup\$ Also, you seem to tell OP to not attempt to create an extension because... you need someone full time to work on it and the artist cannot edit the scene at the same as the level designer? Who are these people? This is just OP trying to work on a project. Furthermore, there are tools in the market to make diff-ing scenes and objects easier. \$\endgroup\$ – user15805 Jan 13 '15 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.