shacklettbp / madrona

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conditional systems - Only trigger system if another system returns True

UsaidPro opened this issue · comments

Hello! This is an amazing library and I am excited to use it in my project. One question I have - does Madrona support conditionally running a system depending on a trigger (like only run SystemB when SystemA returns True)?

My project essentially involves agents performing N actions moving items on a board before finally a simulation is run to get the board result for final reward. In other ECS libraries such as Unity DOTS or Bevy, I setup the ECS systems so the agent-action systems would run when a trigger system determined if BoardPhase.isPlacing = true and the final-reward simulation systems would run otherwise. I was thinking to replicate something similar in Madrona, but cannot find any conditional system trigger logic in the source code.

Does this exist? If not, what code files would be a good starting point for me to add this functionality?

commented

There isn't direct support for this currently. However, the simplest thing you can do is in your system which you want to run conditionally, wrap your logic in an if statement which triggers if BroadPhase.isPlacing is true. So technically the system always gets scheduled but only does something if that flag is set.

Does dependence on a global singleton impact the task graph? I was worried that having my systems all depend on a BroadPhase with an if clause checking BroadPhase.isPlacing == true/false would prevent systems from running in parallel.

Like if SystemA ingested ComponentA and there were N entities using ComponentA, I would assume N SystemA threads run in parallel. But if SystemA ingested ComponentA and BroadPhase singleton (read-only) would SystemA still parallelize properly?

No, SystemA would need to use Context::singleton to get the broadphase singleton rather than passing it into the system.
So the simplest solution would be something like:

if (!ctx.singleton().isPlacing) return;

I would like to improve the taskgraph APIs to better support this in the future, but it's not on the immediate roadmap because the above conditional check winds up having very little performance overhead.

Feel free to reopen if this doesn't answer your question.