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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Loop Npc Behaviour

Discussion in 'Scripting' started by Andrew203, Sep 4, 2018.

  1. Andrew203

    Andrew203

    Joined:
    Apr 23, 2018
    Posts:
    43
    Hi
    I'm making rts-like game where you have workers responsible for gathering resources, I want to create a behaviour similiar to one in the stronghold games where peasants are automatically starting work as soon as you build some work place and then go for example to farm, gather resources and go back to storage and repeat it for eternity. Im looking for a way to do it with huge performance efficiency as there will be a lot of npcs. And I cant find a way to do this. I could make like 10 booleans and call 5 funtions in update but I think it will kill my performance.
     
  2. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    333
    Not sure what you have already, but if you want to have a huge amount of NPCs then you do not want to have a call for Update on each of them.
    Have a manager calculate all of it on each frame(Can be on Update) and then apply changes on each NPC after calculation.
     
  3. Andrew203

    Andrew203

    Joined:
    Apr 23, 2018
    Posts:
    43
    Hey, i made a script for worker with universal functions fitting for diffrent kinds of work, and I have a script for a farm in which one function searches for a worker if job is not occupied, now I want to somehow manage it by calling workers functions from farm script.
     
  4. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    687
    You can do this all in one method with coroutines. When the farm or whatever if first constructed, you could start a coroutine on the peasants to go to the farm- yield until they get there. Then yield return new WaitForSeconds for however long they farm for. Send them to storage, yield until they get there. Then start the coroutine all over again.
     
  5. Andrew203

    Andrew203

    Joined:
    Apr 23, 2018
    Posts:
    43
    Somehow I managed to get it to work with Coroutine, but calling yield return 4 times in one IEnumerator wont be somehow performance heavy? especially that I have used WaitWhile twice which calls functions responsible for reaching positions every frame?
     
  6. jRocket

    jRocket

    Joined:
    Jul 12, 2012
    Posts:
    687
    Probably not an issue, but you can check the profiler to find out what the performance impact is.

    An alternative to coroutines would be using callbacks with c# delegates. So instead of having a method like MoveTo(Vector3 location), you would have something like MoveTo(Vector3 location, Action onComplete), where onComplete is another method what would be call whenever the move is complete.