ISimpleBehaviorJobWrapper
Simple behaviors are implementations of ISimpleBehaviorJobWrapper. The BaseSteeringSystem schedules them after querying the entities with the main entity query. There are three interfaces relevant in creating a simple behavior. The most important one is a custom job ISimpleBehaviorJob<C, R> which significantly simplifies creating the behaviors.
ISimpleBehaviorJobWrapperScheduleshould schedule a job which writesVelocityResults toresults.
ISimpleBehaviorJob<C, R>Executemethod should return a singleVelocityResult.
ISimpleBaseBehavior- entities used by
ISimpleBehaviorJob<C, R>must have a component implementing this interface.
- entities used by
Implementing ISimpleBehaviorJobWrapper
Below is the implementation of the GoForwardJobWrapper. It can be easily used as a template to create your own behavior. The Schedule method receives information about the entities inside BaseBehaviorParams. It should allocate an object of the type specified in [OutData] and assign it to out results. The behavior should write one VelocityResult per entity into VelocityResults. Here the GoForwardJob is resposible for that.
[JobWrapper(typeof(GoForwardComponent))]
[OutData(typeof(VelocityResults))]
public class GoForwardJobWrapper : ISimpleBehaviorJobWrapper
{
public JobHandle Schedule(
SystemBase systemBase,
in BaseBehaviorParams mainBaseParams,
out IDelayedDisposable results,
in JobHandle dependency)
{
// Create new object to hold the results, should be same as declared in OutData
results = new VelocityResults(mainBaseParams.EntityCount);
// Scheduling GoForwardJob
var outDependency = new GoForwardJob
{
}.Schedule<GoForwardJob, GoForwardComponent, VelocityResult>(
systemBase,
mainBaseParams,
(VelocityResults)results,
1,
dependency);
return outDependency;
}
}
Attributes
This job wrapper uses two attributes. First declares what type of object the behavior returns. Second declares which components are expected on the entity. The types from the attributes are used in the editor of the SteeringSystemAsset.
[OutData]- Declares what type of results the behavior creates, aboveVelocityResults.[JobWrapper]- Declares which components should be present on the entity, aboveGoForwardComponent.
The correct way to match the type arguments with generic parameters to ISimpleBehaviorJob<C, R> is the following:
[JobWrapper(typeof(C))][OutData(typeof(R))]
ISimpleBehaviorJob<C, R>
ISimpleBehaviorJob<C, R> simplifies creation of new simple behaviors. For each entity, the Execute is called once, passing in information about the entity in the behaviorData parameter. The method returns a result of type R. Internally the result is then written into the results.
Generic Parameters
C- Type of component on the main entities implementingISimpleBaseBehavior- should match the argument of[JobWrapper].R- Type of result returned from the behavior - should match the argument of[OutData].
[BurstCompile]
struct GoForwardJob : ISimpleBehaviorJob<GoForwardComponent, VelocityResult>
{
public VelocityResult Execute(EntityInformation<GoForwardComponent> entity)
{
// Create a new result, implement your own behavior here instead
var result = new VelocityResult(
entity.Forward, // Going forward in the current facing direction
entity.Component.BaseData.DirectionStrength, // How much I want to go in that direction
entity.Component.Speed, // At what speed I want to go in that direction
entity.Component.BaseData.SpeedStrength, // How much I want to go at that speed
entity.Component.BaseData.Priority); // The priority of this behavior
return result;
}
}
You can just copy and paste this, then change all occurences of GoForward to MyBehaviorName to create a new behavior quickly. The only thing to implement is the Execute method. See other default behaviors to understand more complex examples.