#Pub sub event system for ecs is possible or not??

1 messages · Page 1 of 1 (latest)

tame rain
#

I was using pubsub event system from Zbase package (you can find this on github). Unfortunately, he used classes for that (managed type) so i can't use that for my ISystem with burst.
I tried to create a simpler version of his pubsub system but only use managed types, but i stuck at the step that interface is managed type, can't put it into NativeList or what ever NativeCollection so that i can trigger Invoke() method inside that interface.
Any idea that you suggest me?
Thanks for reading.

#

Pub sub event system for ecs is possible or not??

wary belfry
#

while taking an interface valued type like IMyInterface value is not allowed in burst, you are allowed to have a generic method that takes a T that is constrained to an interface, like void MyMethod<T>(T value) where T : IMyInterface, as long as the concrete T you use it with is unmanaged

flint finch
tame rain
#

Someone has told me that I must not try to communicate between systems using pub sub like that, systems talk to each others by using data

flint finch
chilly tide
#

I think thats already relatively close to what pub-sub tries to achieve anyways: any system can create those events independentely, and any other system can "subscribe" by reading them

tame rain
chilly tide
#

then you'd have one system for each type T (also one clean-up system for each type)

#

however, I am not sure if that was my wisest decision, I am kind of regretting doing that part that way in retrospect

#

But if you do not do dynamic buffers, but use event entities instead, you'd have no problem with clearing

#

just have a system query all entities with EventX or something end of the frame and delete them

tame rain
tame rain
chilly tide
# tame rain havent known anything about interface component 🥲 About generic systems, they s...

This is the interface component

    public interface ISkillProperty<T> : IComponentData
        where T: unmanaged, IComponentData
    {
        public T GetSkillPropertyCurrentLevel();
        public string Describe();
    }

With a generic system which adds a skill property at a given level to a skill

    public partial struct SkillAdditionSystem<T, TComp> : ISystem 
        where T:unmanaged, IComponentData, ISkillProperty<TComp>
        where TComp:unmanaged, IComponentData
    {
        private EntityQuery query;

        [BurstCompile]
        public void OnCreate(ref SystemState state)
        {
            state.RequireForUpdate<CommandReloadAttackPrefabs>();
            query = new EntityQueryBuilder(Allocator.Temp)
                .WithAll<CommandReloadAttackPrefabs>()
                .WithAll<CurrentAttackPrefab>()
                .WithAll<T>()
                .Build(ref state);
        }

        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            var em = state.EntityManager;
            var createAttackPrefabCommands = query.ToComponentDataArray<CurrentAttackPrefab>(state.WorldUpdateAllocator);
            var attackFactories = query.ToComponentDataArray<T>(state.WorldUpdateAllocator);
            for(int i = 0; i < createAttackPrefabCommands.Length; i++)
            {
                var targetPrefab = createAttackPrefabCommands[i].prefab;
                var comp = attackFactories[i].GetSkillPropertyCurrentLevel();
                em.AddComponentData(targetPrefab, comp);
            }
        }
    }
#

for example

#

however I do honestly regret doing it like that, I'd rather have kept this part out of the ECS alltogether I think (basically what it does is create an attack which is assembled of many small "skill properties" like damagePerSecond, ExplodeOnImpact etc)

chilly tide
#

you can have a generic

public partial struct CleanUpSystem<T>{
  //queries for `T` and destroys all of them
}
chilly tide
#

however, instead of doing it this way, you could just create entities which are the events themselves. Depending on their components, different systems can consume those entities and do stuff

#

all of them are tagged with EventEntity

#

at the end of the frame, you have a system querying for EventEntity and destroying all of them

#

that would not need generics and I imagine could be a better way

tame rain
flint finch
#

or more like - not at the end of frame, but at the start