I’m working on a framework where the player has at his disposal a fairly large library of task verbs: clean, patrol, maintain equipment, train, and so forth, and I would like to be able to assign each NPC a series of tasks and have them execute in series. Right now my solution is to give each task’s icon a string corresponding to that icon’s function, making a List containing the string from every icon in that NPC’s task list, and then iterating through that list and running SendMessage to call the function that corresponds to each task.
This feels clunky, prone to breaking, hard to debug, and exceptionally brute-forcey, so is there an obvious better method I should be researching?
could you have a base class of “task” that more specific tasks extend, you can then have a list of the base class type to hold various specific type tasks?
I suspect that’s a great idea, but please bear with me since I just learned about inheritance today: you’re saying I
- Create one class BaseTask:MonoBehavior with the variables and functions universal to every task
- Create multiple classes inheriting from that CleanTask:BaseTask, PatrolTask:BaseTask, and so forth containing stuff specific to each individual task
But where I get lost is the last bit- how do I specifically target which of the classes that inherit from BaseTask fire in which order on each NPC?
if you have a virtual function in the base class (PerformTask() for example) each specific task class can override that function to do “their thing”. So triggering the first action in the list is myListOfBaseClasses[0].PerformTask() etc. rather than having “PatrolTask.Patrol()” “GuardTask.Guard()” etc.
https://unity3d.com/learn/tutorials/modules/intermediate/scripting/overriding
Which task the NPC chooses to do next is more of an AI decision making question…
So learning about overriding is pretty close to the neatest thing I’ve done today, since that allows me to do a bunch of related tasks I’ve been struggling with. To make sure I’m not misunderstanding though, you’re saying that myListOfBaseClasses would be a list containing every task the NPC is capable of performing, the index would target which of the specific classes (and thus, overrides) would be called, and actually setting up an NPC’s task list would just be an exercise of telling it which indexes of myListOfBaseClasses to call?
GetComponents() would return an array of all components of type “Task”, you could put that into “myAvailableTasks” variable. Then in the AI you can check to see if the npc has the required task available before being told to do it.
so the AI would do something like
pseudocode
myPatrolTask = find PatrolTask in myAvailableTasks
if(myPatrolTask && I want to patrol)
{
myPatrolTask.PerformTask()
}
Oooh perfect, you absolutely rock and have made my day and week massively easier, thank you so much for your patient assistance!
you might also want to look at:
put the two together and you’ll find all components of a specific type in an entire hierarchy, handy if a task is attached to a carried object (shield with “BlockTask”, sword with “AttackTask” as children of the “hands” for example)
or alternatively you could have the tasks register (i.e. add to the myAvailableTasks) with the AIController (or whatever the AI class is called) when they are initialised, and deregister (i.e. removed from myAvailableTasks) when the component is removed… this would be a better approach if the available tasks change during the object lifetime i think.
Ooh, that’s really smart- if I wanted to simplify it even further I could probably make a go of simply having two HandTasks whose references change depending on what items get equipped.