do [WriteOnly] int auto unpack?

_gridNative is a nativearray, to unpack it I do _gridNative.CopyTo(the original variable that’s inside the class)
_changeUMax is an int and _blissChange is a bool
do I need to do something special to unpack them from the job?

        var _blissNative = new NativeArray<int>(_blissmap, Allocator.TempJob);
        var _gridNative = new NativeArray<int>(_grid, Allocator.TempJob);
        var recalculateBoostJob = new RecalculateBoostJob
        {
            r = r,
            boosters4job = new NativeArray<Booster4Job>(b4j.ToArray(), Allocator.TempJob),
            baseBliss = baseBliss,
            blissmap = _blissNative,
            grid = _gridNative,
            changeUMin = _changeUMin,
            changeVMin = _changeUMin,
            changeUMax = _changeUMax,
            changeVMax = _changeVMax,
            _blissChange = false
        };
        var recalculateBoostJobHandle = recalculateBoostJob.Schedule();
        _blissNative.CopyTo(_blissmap);
        _gridNative.CopyTo(_grid);

the job is this, I out the result by doing a =

struct RecalculateBoostJob : IJob
    {
        [ReadOnly] public Rect r;
        [ReadOnly] public NativeArray<Booster4Job> boosters4job;
        [ReadOnly] public int baseBliss;
        public NativeArray<int> blissmap, grid;
        public int changeUMin, changeVMin, changeUMax, changeVMax;
        [WriteOnly] public bool _blissChange;

        public void Execute()
        {
            // convert to int and clamp
            int xMin = Mathf.Max((int) r.xMin, -HALF_GRID_SIZE);
            int xMax = Mathf.Min((int) r.xMax, HALF_GRID_SIZE);
            int zMin = Mathf.Max((int) r.yMin, -HALF_GRID_SIZE);
            int zMax = Mathf.Min((int) r.yMax, HALF_GRID_SIZE);
            int b;
            foreach (var b4j in boosters4job)
            {
                var pos = b4j.pos;
                var boost = b4j;
                int cx = (int) pos.x;
                int dx2 = cx + boost.range + 2;
                int dx1 = cx - boost.range - 1;
                int cz = (int) pos.z;
                int dz2 = cz + boost.range + 2;
                int dz1 = cz - boost.range - 1;
                // within the changed rect?
                if (dx2 < xMin || dz2 < zMin || dx1 > xMax || dz1 > zMax)
                    continue;
                dx1 = Mathf.Max(dx1, xMin);
                dx2 = Mathf.Min(dx2, xMax);
                dz1 = Mathf.Max(dz1, zMin);
                dz2 = Mathf.Min(dz2, zMax);
                // convert current boost [-1,+1] to binary mask
                b = Boost2BlissBitmask(boost.boostAmount);
                int rangeSqr = boost.range * boost.range;
                for (int z = dz1; z < dz2; z++)
                {
                    for (int x = dx1; x < dx2; x++)
                    {
                        int distSqr = (x - cx) * (x - cx) + (z - cz) * (z - cz);
                        if (distSqr < rangeSqr)
                        {
                            var boostCoord = BoostCoord(x, z);
                            // bliss bitwise math
                            blissmap[boostCoord] = blissmap[boostCoord] | b;
                        }
                    }
                }
            }

            // composite pass with base bliss
            b = Boost2BlissBitmask(baseBliss);
            for (int x = xMin; x < xMax; x++)
            {
                for (int z = zMin; z < zMax; z++)
                {
                    var boostCoord = BoostCoord(x, z);
                    grid[boostCoord] = blissmap[boostCoord] | b;
                }
            }

            // expand change rect for rendering
            changeUMin = Mathf.Min(changeUMin, HALF_GRID_SIZE + xMin);
            changeVMin = Mathf.Min(changeVMin, HALF_GRID_SIZE + zMin);
            changeUMax = Mathf.Max(changeUMax, HALF_GRID_SIZE + xMax);
            changeVMax = Mathf.Max(changeVMax, HALF_GRID_SIZE + zMax);
            _blissChange = true;
        }
    }

For “return” values, use NativeReference Struct NativeReference<T> | Collections | 0.11.0-preview.17

1 Like

I don’t understand that page, got an example of outputting int ?

It is like a NativeArray, but with only 1 element.

        // on the system
        NativeReference<bool> _blissChange = new NativeReference<bool>(Allocator.TempJob);
        var recalculateBoostJob = new RecalculateBoostJob
        {
            _blissChange = _blissChange
        };
        var recalculateBoostJobHandle = recalculateBoostJob.Schedule();
        recalculateBoostJobHandle.Complete();
        Debug.Log(_blissChange.Value);

        // the job
        struct RecalculateBoostJob : IJob
        {
            [WriteOnly] public NativeReference<bool> _blissChange;
   
            public void Execute()
            {
                _blissChange.Value = true;
            }
        }
3 Likes

thanks
that must be experimental, even preview package don’t show it


and Unity.Collections don’t have them
I’ll stick to NativeArray of length 1, it seems to be the way

It is only available on recent versions, so if you are on 2019.4 you won’t have access to it. But yeah, it is essentially the same as using a NativeArray of length 1, just prettier.

it is quite a fait bit cleaner, I use 2020.1.17 so maybe this was moved to github only and pulled out of package manager after you added it to your project

NativeReference is not anything undocumented or experimental. It’s listed on the front page of their documentation https://docs.unity3d.com/Packages/com.unity.collections@0.14/manual/index.html

If you’re not seeing it you’re using an older version of the collections package.

https://discussions.unity.com/t/795542

Thanks for the list, I just added the package by URL
… and then saw that
6631450--756286--upload_2020-12-16_23-23-45.png
I’ll stick to the verbose route for this production

Entities package is also preview, mind.

The basic Unity.Collections stuff and Job API are not in preview, laurent example doesn’t seem to have nothing to do with Entities

Ah that is fair enough. I just assumed, since it is DOTS forum.