Hello, we’ve come accross an issue while working with the FollowerEntity.
Before anything, we are using version 5.4.4 of the package. We haven’t tried updating the package to version 5.4.5 or 5.4.6 as this issue wasn’t mentionned in the changelog.
The issue is that properties on the follower entity that can be used to toggle ecs components can throw an exception if the entity doesn’t exist anymore although it doesn’t look like it should and are also not behaving consistently.
Let’s tackle the first issue, we noticed when setting the updateRotation property in the FollowerEntity, an exception would be raised if the entity doesn’t exist anymore. It seemed weird to us that it would throw an exception in such case instead of ignoring it and resuming. So we looked into the code and here’s what we found:
In FollowerEntity the property calls the proxy:
public bool updateRotation {
get => syncRotation;
set {
syncRotation = value;
proxy.updateRotation = value;
}
}
The proxy calls a function called ToggleComponent:
public bool updateRotation {
get => entityExists && world.EntityManager.HasComponent<SyncRotationWithTransform>(entity);
set {
ToggleComponent<SyncRotationWithTransform>(world, entity, value, false);
}
}
The ToggleComponent function is where the exception is thrown, it looks like this:
internal static void ToggleComponent<T>(World world, Entity entity, bool enabled, bool mustExist) where T : struct, IComponentData {
if (world == null || !world.IsCreated || !world.EntityManager.Exists(entity)) {
if (!mustExist) throw new System.InvalidOperationException("Entity does not exist. You can only access this if the component is active and enabled.");
return;
}
if (enabled) {
world.EntityManager.AddComponent<T>(entity);
} else {
world.EntityManager.RemoveComponent<T>(entity);
}
}
Here is our first issue, it looks like the mustExist parameter is not correctly used. It looks like it should throw the exception if mustExist is true and return if it is false. If we look at the updateRoration property again, we can see that it’s called with the parameter to false which in the current state of the code will throw the exception.
The second issue is that the simulateMovement property is the only property that behaves differently than other properties that can be toggled, which seems inconsistent:
public bool simulateMovement {
get => entityExists && world.EntityManager.HasComponent<SimulateMovement>(entity);
set => ToggleComponent<SimulateMovement>(world, entity, value, true);
}
Here we can see this is called with mustExist to true which means it will never throw an exception.
As a temporary fix, we decided to check wether the entity still exists before setting any of its properties.
Hope this helps!