Search Unity

Using Actions or similar within the Job System?

Discussion in 'C# Job System' started by Nyanpas, May 6, 2021.

  1. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Greetings,
    So I have this Job that depending on the input will do a lot of different things. The main reason I want to use Actions is that I can add them to a look-up table and in a single for-loop "play them out" based on the indices supplied to the Job that correspond to an Action. This means I don't have to manage a rapidly growing switch-case, which I am about to do... I also wouldn't imagine having to create separate Jobs for each, as that would potentially be a very time-consuming endeavour.

    However, I have looked around for this now, and it seems it is not supported/possible? Is it perhaps also my approach that is wrong?

    Please (forward) assist.
     
    Last edited: May 6, 2021
  2. DreamingImLatios

    DreamingImLatios

    Joined:
    Jun 3, 2017
    Posts:
    4,264
    It sounds like you want to use FunctionPointers?
     
    Nyanpas likes this.
  3. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    Apparently I don't jive enough with the lingo...

    [edit] There is still probably the case for the switch due to the different delegations taking different parameters. I have mapped the switch-case but it still doesn't seem like the best solution for this.
     
  4. davenirline

    davenirline

    Joined:
    Jul 7, 2010
    Posts:
    982
    You can use "action as an entity" pattern. You define an action as an entity with components Action where the common data would be placed, and some other filter action that may or may not contain data. It's sole purpose is so you could filter those entities in systems where the actual execution of the action resides. For example:
    Code (CSharp):
    1. // This is the action entity
    2. Entity entity = entityManager.CreateEntity(typeof(Action), typeof(Attack));
    3.  
    4. class AttackSystem : SystemBase {
    5.     protected overrider void OnUpdate() {
    6.         this.Entities.ForEach(delegate(in Action action, ref Attack attack) {
    7.             // Attack code here. Attack may contain data about target and damage.
    8.         }).Schedule();
    9.     }
    10. }
     
  5. Nyanpas

    Nyanpas

    Joined:
    Dec 29, 2016
    Posts:
    406
    It seems like function pointers are closer to what I need. ;w;