Polarith AI
1.8
Label

Every behaviour of our system has got a property called Label. The purpose of the Label is to distinguish between behaviour references. So, if you obtain a list of all attached AIMBehaviour components by calling GameObject.GetComponents() and you need to access a specific behaviour instance, you can use the Label to find the correct one. Note that this is only an issue when there are more than two behaviours of the same type.

For example, we have an agent with two Seek behaviours and a Flee behaviour. There is no problem to check whether an AIMBehaviour is an AIMSeek or an AIMFlee component (due to GameObject.GetComponents() you can obtain components of a single type anyway). However, if you need to distinguish between the two Seek components, you need to use a Label.

The following code snippet is an example for getting a specific AIMSeek component using a Label.

AIMSeek[] seekComponents = gameObject.GetComponents<AIMSeek>();
foreach (AIMSeek s in seekComponents)
if (s.Label == "MySpecificSeek")
Debug.Log("We have found our specific Seek instance!");

Remarks on State Handling

A very important use case for the Label mechanism is the integration of a so called state handling logic, e.g. a finite-state machine or some kind of fuzzy logic. Such techniques define which behaviours are active for specific situations so that the agent may react properly to the current situation.

Our package already contains a very simple but powerful example script which demonstrates how to use the Label feature together with state handling mechanisms. The example script SwarmStates shows how to switch between behaviour sets to achieve some kind of swarming behaviours, whereby it utilizes the animation state machine of Unity called Mecanim. Besides animation, this works very well for simple AI state logic, too. When entering a state, the script deactivates all attached behaviours at first. Then, all states which are contained in the Label list of SwarmStates are activated. The list consists of strings which represent the labels of the corresponding attached behaviours. If the list contains an invalid label, it is ignored. Otherwise, the corresponding behaviour gets enabled.

Imprint