Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Time cycle with delegates

Discussion in 'Scripting' started by X1r15, Jul 16, 2020.

  1. X1r15

    X1r15

    Joined:
    Dec 19, 2019
    Posts:
    12
    Hey,
    I am working on a calendar system.

    Counting the "in game time" works already pretty well. What I am struggling with is scheduling actions.
    Let's say I am having 20 NPC. They have their own day and week schedules. They walk to different places and perform different actions.

    I thought about building a solution around observer pattern using delegates in dictionary.

    So the plan would be to have a dictionary<string, action> and the string would be unique time identifier (e.g. 10:53 WED). All the NPC would subscribe to the potential events at certain time. When the time changes, I would "grab" from the dictionary actions for particular time (if they exist) and execute the methods.

    Do you think solution like that has sense?
    Is there any better/cleaner way to design and implement something like that?
    Is there a way to achieve something like that and store it in files?

    Thanks for your help! :)
     
  2. chengwang2077

    chengwang2077

    Joined:
    Nov 23, 2019
    Posts:
    131
    There are two suggestions here.
    I think the use of dictionaries is usually done during the optimization phase. use class is better
    Code (CSharp):
    1. public class Mission{
    2. public float time;
    3. public System.Action action;
    4. }
    This is more scalable , because the requirements may change at any time during development, He might become like this
    Code (CSharp):
    1. public class Mission{
    2. public float plannedTime;
    3. public System.Action action;
    4. public MissionType missionType;
    5. public MissionName missionName;
    6. public float progress // 0-1
    7. }
    If the behavior of the NPC is close to that of humans, then the NPC actually has two plans, one for what to do every day, the other for what to do every Monday, and what to do every Tuesday.
     
  3. X1r15

    X1r15

    Joined:
    Dec 19, 2019
    Posts:
    12
    @nwet3t4 this is actually very good point. Thank you so much.

    The question here is - how would you actually schedule that to run at specific time without the use of dictionary?

    Would you rather keep the Missions on the NPCs themselves and use the static method on the "World time" to get the time?
     
  4. chengwang2077

    chengwang2077

    Joined:
    Nov 23, 2019
    Posts:
    131
    The mission is placed in the LIst<Mission> missionList; this field belongs to NPC.
    The mission is placed in the LIst<Mission> missionList; this field belongs to NPC.
    Traverse the time of each mission, if the mission time is equal to the current time, do action
    Of course, you can arrange the mission in chronological order, so that each frame you only judge whether the time is equal to the time of the next mission
    Yes, Mission are an attribute of NPC, and time is managed by Time class.
     
  5. X1r15

    X1r15

    Joined:
    Dec 19, 2019
    Posts:
    12
    This sounds sensible! Thank you so much :).