Entitas
Entitas copied to clipboard
[Question] Entitas Job Systems
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
Hi, see release notes Entitas 1.5.0
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 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
OK. Thanks!
@sschmid Hi! Are there any tutorials about your JobSystem?
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.
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.
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.