Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Counter inside ScheduleParallel

Discussion in 'Entity Component System' started by RoughSpaghetti3211, Jan 8, 2021.

  1. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Is there a way to keep a counter inside SchefuleParallel lambda

    eg

    int counter = 0
    Entitie.ForEach(()=>
    {
    if( someCondition ) counter++;
    }).ScheduleParrllel();
     
  2. tertle

    tertle

    Joined:
    Jan 25, 2011
    Posts:
    3,626
    You can write your own simple nativecontainer using a lock but it's not ideal and there are a lot of easy hacks you can do.

    Such as if you simply did NativeQueue.ParallelWriter.Enqueue(byte) for your count tracker, then the Length of the queue is your counter. (I wouldn't do this if I was counting to 10,000+s, there are alternatives)
     
  3. RoughSpaghetti3211

    RoughSpaghetti3211

    Joined:
    Aug 11, 2015
    Posts:
    1,695
    Do you ever just spawn counter entities ? and query EntityCalculateCount(()
     
  4. Chris-Herold

    Chris-Herold

    Joined:
    Nov 14, 2011
    Posts:
    115
    At lower entity counts (couple thousand) running this on a single thread (using .Run) will be faster.
    I've ran into this a couple of times and found out the hard way that i was too obsessed with parallel scheduling.
    Burst makes things freakishly fast, this is one of the examples where single thread simply stumps multithreading.
     
    RoughSpaghetti3211 and apkdev like this.
  5. apkdev

    apkdev

    Joined:
    Dec 12, 2015
    Posts:
    263
    RoughSpaghetti3211 likes this.