Entitas icon indicating copy to clipboard operation
Entitas copied to clipboard

[Question] Entitas Job Systems

Open RealCosmik opened this issue 6 years ago • 8 comments

Hi I seen recently that there was an Entitas Job System being implemented Just wondering how that is coming along. While the Entitas Job System is still in development, would it be considered bad practice to have async Execute methods inside my IExecuteSystems

RealCosmik avatar Mar 31 '18 16:03 RealCosmik

Hi, see release notes Entitas 1.5.0

sschmid avatar Apr 03 '18 20:04 sschmid

Hi, is it possible to call CreateEntity in the Job System? My scenario is that, I have a collision detection system, if they collided, I will create entity with a custom collision event component for later use.

raymondleungmh avatar Apr 06 '18 08:04 raymondleungmh

@raymondleungmh In general, it is possible to creates entities in a JobSystem. But you have to be careful if you have multiple JobSystem running in parallel creating entities, that can lead to race-conditions. If you are sure that only one JobSystem at time modifies the contexts / groups, then you're fine

sschmid avatar Apr 23 '18 19:04 sschmid

OK. Thanks!

raymondleungmh avatar Apr 25 '18 03:04 raymondleungmh

@sschmid Hi! Are there any tutorials about your JobSystem?

subzero911 avatar Apr 09 '19 18:04 subzero911

Hi, no there’s only a test jobsystem used by the unit tests https://github.com/sschmid/Entitas-CSharp/blob/master/Tests/Tests/Fixtures/Systems/TestJobSystem.cs

You might find some code and comments about limitations in the gitter chat, I think there was a discussion about it.

sschmid avatar Apr 09 '19 21:04 sschmid

Am I right to do like this?

public class PositionComponent : IComponent
{
    public Vector3 value;
}

public class SimpleMoveJobSystem : JobSystem<GameEntity>
{
    public SimpleMoveJobSystem(GameContext context, int threads) :
    base(context.GetGroup(GameMatcher.AllOf(GameMatcher.Position)), threads)
    {
    }

    protected override void Execute(GameEntity entity)
    {
        entity.ReplacePosition(entity.position.value + Vector3.up);
    }
}

public class GameController : MonoBehaviour
{
    SimpleMoveJobSystem system;    

    void Start()
    {
        var contexts = Contexts.sharedInstance;

        var e = contexts.game.CreateEntity();        
        e.AddPosition(Vector3.zero);

        int workerThreads, portThreads;
        ThreadPool.GetAvailableThreads(out workerThreads, out portThreads);
        system = new SimpleMoveJobSystem(contexts.game, workerThreads);
    }

    private void Update()
    {
        system.Execute();
    }
}

This code works, but I don't know how to check if it actually performs in multiple threads and gives me some performance gain.

subzero911 avatar Apr 09 '19 23:04 subzero911

Am I right to do like this?

public class PositionComponent : IComponent
{
    public Vector3 value;
}

public class SimpleMoveJobSystem : JobSystem<GameEntity>
{
    public SimpleMoveJobSystem(GameContext context, int threads) :
    base(context.GetGroup(GameMatcher.AllOf(GameMatcher.Position)), threads)
    {
    }

    protected override void Execute(GameEntity entity)
    {
        entity.ReplacePosition(entity.position.value + Vector3.up);
    }
}

public class GameController : MonoBehaviour
{
    SimpleMoveJobSystem system;    

    void Start()
    {
        var contexts = Contexts.sharedInstance;

        var e = contexts.game.CreateEntity();        
        e.AddPosition(Vector3.zero);

        int workerThreads, portThreads;
        ThreadPool.GetAvailableThreads(out workerThreads, out portThreads);
        system = new SimpleMoveJobSystem(contexts.game, workerThreads);
    }

    private void Update()
    {
        system.Execute();
    }
}

This code works, but I don't know how to check if it actually performs in multiple threads and gives me some performance gain.

No, you shouldn't modify the group in excute, just set the position value directly, do not replace it.

wangsihao87 avatar Sep 11 '20 02:09 wangsihao87