Entitas icon indicating copy to clipboard operation
Entitas copied to clipboard

Activate and Deactivate

Open AtheosCode opened this issue 6 years ago • 1 comments

How do I suspend or remove a system at runtime?

For example, when I need my ResourceSystem to pause or remove, delete all RenderComponent, and when it is added, reuse the parameters of the Resource component to create and RenderComponent. So I can choose whether to render the module at any time.Just like editor mode.

using System.Collections.Generic;
using Entitas;
using UnityEngine;

public class ResourceSystem : ReactiveSystem<GameEntity>,IReactiveSystem
{

    private readonly IGroup<GameEntity> _resourceGroup;

    public ResourceSystem(Contexts contexts) : base(contexts.game)
    {
        _resourceGroup = contexts.game.GetGroup(GameMatcher.AllOf(GameMatcher.Resource));
    }


    protected override ICollector<GameEntity> GetTrigger(IContext<GameEntity> context)
    {
        return context.CreateCollector(GameMatcher.Resource);
    }

    protected override bool Filter(GameEntity entity)
    {
        return entity.hasResource;
    }

    protected override void Execute(List<GameEntity> entities)
    {
        foreach (var entity in entities)
        {
            entity.AddSafeRender(entity.resource.resource);
        }
    }

    public new void Activate()
    {
        base.Activate();
        foreach (var entity in _resourceGroup)
        {
            if (entity.hasResource)
            {
                entity.AddSafeRender(entity.resource.resource);
            }
        }
    }
    public new void Deactivate()
    {
        foreach (var entity in _resourceGroup)
        {
            if (entity.hasRender)
            {
                entity.RemoveSafeRender();
            }
        }
        base.Deactivate();
    }
}

AtheosCode avatar Aug 19 '18 16:08 AtheosCode

There's no native implementation. Regarding your purpose, is that possible to create/destroy the whole entity on demand?

zhuchun avatar Aug 20 '18 12:08 zhuchun