Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I just looked at that implementation and I find myself wondering why so much code is needed for something relatively simple.</p> <p>From what you say, you want a simple way of composing behaviours. A behaviour here, I presume, is a mapping from a state to zero or more actions by an agent. You can model this very easily using C# lambdas. For example:</p> <pre><code>Action Selector(Func&lt;bool&gt; cond, Action ifTrue, Action ifFalse) { return () =&gt; { if cond() then ifTrue() else ifFalse() }; } Action Sequencer(Action a, Action b) { return () =&gt; { a(); b(); } } </code></pre> <p>The leaves of your tree are simple Actions that do something appropriate to the state. You "run" a tree simply by executing it.</p> <p>If you want to get fancy, you can parameterise this scheme to make the state explicit.</p> <p>Hope this helps.</p> <p>---- Addendum ----</p> <p>Jason asked for an example of how you could use this approach, so here's a simple "AI" patrolling guard example (I assume WorldState corresponds to a description of the environment at the time the behaviour tree is evaluated):</p> <pre><code>Func&lt;bool&gt; ifPlayerIsInSight = () =&gt; ...true iff WorldState shows guard can see player...; Action shootAtPlayer = () =&gt; { ...aim guard's weapon at player and fire... }; Func&lt;bool&gt; ifUnderFire = () =&gt; ...true iff WorldState shows guard hears player gunfire...; Action takeCover = () =&gt; { ...guard runs for nearest shelter... }; Action walkBackAndForthGuardingDoorway = () =&gt; { ...default guard patrol behaviour... }; Action patrollingGuardBehaviour = Selector(ifPlayerIsInSight, shootAtPlayer, Selector(ifUnderFire, takeCover, walkBackAndForthGuardingDoorway)); </code></pre> <p>To make the guard do something, just call <code>patrollingGuardBehaviour()</code>. Note that the various subactions and tests can be implemented as methods with the right signatures rather than inline as lambdas. You can add other combinators to <code>Selector</code> and <code>Sequencer</code>, e.g., for parallel activity.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload