Warning: Lot’s of confusing stuff ahead. Sorry. My main questions are the bold ones.
Heho!
I kinda need a push in the right direction, I am doing this project to learn unity better and I am a little bit stuck.
So I have a bunch of npcs which supposed to do certain tasks on their own (like a sim, but automated).
Basically I made a hierarchy of importance and then I just wanted to put them all after each other into the Update Function so they get called after each other. Here’s some pseudocode for that:
if (thirsty){
GoDrink();
return;
if (hungry){
GoEat();
return;
etc…
But after playing around with it a little bit, I have the feeling this is already the wrong direction, is it?
Does anyone know a good tutorial for that? Or can you help me out a little bit and tell me how this is usually done? I mostly only find stuff for enemies.
It basically gets complicated at the point where they have to actually go somewhere (lets say a well) to drink and I need to wait until they are there, or until a certain action is fulfilled (“cooking”).
I keep coming back to Coroutines, but how do I make the whole script pause when I use them and not just have them run next to the normal functions?
Or can I use WaitForSeconds without a return?
This is for example how my Drink Function looks right now,
the game checks where you are and if that building has water. If yes, you can drink as much as it has, but maximum 100. The Problem is, during the waiting time it already continues with the Update function and calls other Coroutines/Functions.
How do I make a “cooldown” in which the npc is not being able to pick up a new action?
IEnumerator Drink()
{
if (currentPlace.gameObject.GetComponent<Object_ressources>() != null)
{
int temp = currentPlace.gameObject.GetComponent<Object_ressources>().water;
int temp2 = 100-thirst;
if ((temp + thirst) <= 100)
{
thirst += temp;
}
else
{
thirst = 100;
}
currentPlace.gameObject.GetComponent<Object_ressources>().water -= temp2;
}
yield return new WaitForSeconds(5);
}
edit: Also the term Finite State Machine was thrown at me, is that the solution?