Hello
I am trying to shoot projectiles, for that I would use a pool of inactive projectiles instead of instantiate a new projectile every time. With EntityQuery and EntityCommandBuffer I try to get a single projectile that is inactive and activate it. My current approach is to ether use a bool in the component to disable and enable it or adding or removing component tags. But I am failing to access a single entity with projectile component and DeactivateTag. This feels like a simple problem.
I think i am not using the EntityQuery incorrecly and i get a error when i try to run.
Error
ArgumentException: EntityQuery.CompareComponents may not include typeof(Entity), it is implicit
Unity.Entities.EntityQueryManager.CompareComponents (Unity.Entities.ComponentType* componentTypes, System.Int32 componentTypesCount, Unity.Entities.EntityQueryData* queryData) (at Library/PackageCache/com.unity.entities@0.17.0-preview.42/Unity.Entities/Iterators/EntityQueryManager.cs:332)
When I checked the unity GitHub samples, they just instantiate a new entity in most examples. So mabye my approach is wrong and i should just spawn new projectiles and then destroy them.
https://github.com/Unity-Technologies/EntityComponentSystemSamples/tree/master/ECSSamples
System
protected override void OnUpdate()
{
var beginCommandbuffer = beginECB.CreateCommandBuffer();
var endCommandbuffer = endECB.CreateCommandBuffer().AsParallelWriter();
EntityQuery projectileQuery = GetEntityQuery(ComponentType.ReadOnly<Entity>(), ComponentType.ReadWrite<ProjectileComponent>(), ComponentType.ReadWrite<DeactivateTag>());
var projectileGroup = projectileQuery.ToComponentDataArray<ProjectileComponent>(Allocator.Temp);
var deactivatedProjectiles = projectileQuery.ToEntityArray(Allocator.Temp);
//Fire weapon
Entities.ForEach((ref WeaponFire weaponFire) =>
{
if (weaponFire.fire)
{
beginCommandbuffer.RemoveComponent<DeactivateTag>(deactivatedProjectiles[0]);
}
//var test = projectileQuery.ToComponentDataArray<ProjectileComponent>(Allocator.TempJob);
}).Schedule();
deactivatedProjectiles.Dispose();
}
Components
public struct ProjectileComponent : IComponentData
{
public float maxLifeTime;
public float lifeTime;
public float projectileSpeed;
public float damage;
public bool active;
//Enum for dmg Type
public float3 direction;
public float3 position;
}
public struct WeaponFire : IComponentData
{
public bool fire;
public float3 barrelPosition;
}
Any help or examples would be appreciated