caesuric / mountain-goap

A GOAP (Goal Oriented Action Planning) AI library, written in C#.
Other
89 stars 10 forks source link

Create a new Thread every time Update() ? #30

Closed hoangcuongzk1 closed 4 months ago

hoangcuongzk1 commented 5 months ago
   /// <summary>Executes an asynchronous step of agent work.</summary>
    private void StepAsync()
    {
      if (!this.IsBusy && !this.IsPlanning)
      {
        this.IsPlanning = true;
        new Thread((ThreadStart) (() => Planner.Plan(this, this.CostMaximum, this.StepMaximum))).Start();
      }
      else
      {
        if (this.IsPlanning)
          return;
        this.Execute();
      }
    }

I'm not good at C# Unity, but I wonder if this code is bad for performance. Creating a new thread every time Update() is called sounds like a very bad idea. Could you explain this to me?

caesuric commented 5 months ago

Sure, if it's busy or planning, it won't start a new thread. Right before it starts the thread, it sets the flag that says it's planning to true. So it should never start more than one planning thread at once.

hoangcuongzk1 commented 5 months ago

Sure, if it's busy or planning, it won't start a new thread. Right before it starts the thread, it sets the flag that says it's planning to true. So it should never start more than one planning thread at once.

Anyway, this repository is perfect for me. It's very helpful for a GOAP beginner. Thank you so much! <3