Question about branches

Hey,
I am confused about how to use branches. What is the difference between using multiple braches with different sizes and only one branch with a big size?
When I use multiple branches for one agent, how the agent will reach those branches in the code?

When the AI runs, it will pick one value in each branch and pass that to your code. It’s useful when you need to have your agent make two decisions at the same time. For example, if you only need your agent to decide which way to walk, you would have 1 branch with 5 values. (don’t move, move left, move right, move up, move down).

If you also want your agent, in the same turn as walking, to do something else (for example, pick a direction to look in), you would add a second branch, with 4 values (up, down, right, left).

Then your code would look something like this:

public override void OnActionReceived(float[] branches) {
  int movement = Mathf.FloorToInt(branches[0])
  int lookDirection = Mathf.FloorToInt(branches[1])

 // now use these values to make your agent do something
 if (movement == 0) { row = -1; col = -1; };
 if (movement == 1) { row = -1; col = 1; }
 if (movement == 2) { row = 1; col = -1; }
 if (movement == 3) { row = 1; col = 1; }

 if (lookDirection == 0) { angle = 0; };
 if (lookDirection == 1) { angle = 90; }
 if (lookDirection == 2) { angle = 180; }
 if (lookDirection == 3) { angle = 270; }

// Now execute the actions
DoMovement(row, col);
SetLookDirection(lookDirection);
}

On the other hand, if you want your agent to either move in a certain direction, or look in a certain direction, you would use one branch with 9 values: (don’t move, move left, move right, move up, move down, look up, look down, look right, look left). And your code would execute only one of those in a single turn.

Note that you can use CollectDiscreteActionMasks() to mask out the actions that are not possible. For example, if you are next to a wall on your left, you would add the index of moving left to the mask so it’s not picked as a possible action.

Hope that helps.

1 Like