#[Q] Burst and managed types not possible?

1 messages · Page 1 of 1 (latest)

lapis nebula
#

Hi, i tried to burst the instantation of gameObjects from a pool, like this:

[BurstCompile]
struct SpawnEnemyJob : IJob {
    public Vector3[] enemySpawnPos;
    public int spawnQueue;
    public IPool pool;
    public int i;
    public void Execute() {
        var enemy = pool.Get();
        enemy.transform.position = enemySpawnPos[spawnQueue-i];
    }
}

But i get the error that IPool is a managed type and not supported by bursty. According to docs, only native types (like int, bool, float, etc) are allowed (which makes sense as thats closer to machine assembly): https://docs.unity3d.com/Packages/com.unity.burst@1.0/manual/index.html

The only exception here are entities, right? These (along native types) can be used for bursting. But i am not sure if my understanding is correct.

wraith dirge
#

Burst only works on unmanaged types

#

Entities are unmanaged (they're just a struct of 2 ints)

#

Vector3[]
arrays are managed and not allowed
IPool
interfaces are managed and not allowed
var enemy = pool.Get();
gameobjects are managed and not allowed
enemy.transform
transform is managed and not allowed

#

95% of stuff in UnityEngine is managed and not allowed

#

you've linked a very old version of the manual

#

is what is supported

lapis nebula
wraith dirge
#

If your game is primarily using gameobjects it is very hard to use burst on them

lapis nebula
wraith dirge
#

You generally need to write your data to a nativearray, operate on it, write it back to gameobjects

lapis nebula
#

ah thats a good idea, it could serve as bridge then between bursted code and managed, incase the following applies: can the array then hold other (managed) data, like for example transforms?

wraith dirge
#

no

#

if it's a class, it's managed and can't be used in burst

#

(there is a special in TransformAccessArray that can be used in burst that provides position/rotation)

lapis nebula
#

i understood you wrong maybe, but if the IJob struct can can work with a native array, couldnt i pass into this array any value from the caller?

#

calling script looks like this:

private IEnumerator DistributedSpawning(){
...
// create and schedule the job
var job = new SpawnEnemyJob() {
    enemySpawnPos = enemySpawnPos,
    spawnQueue = spawnQueue,
    pool = pool,
    i = i
    nArray[] = new Array["here","there"];  // <-
};
job.Schedule().Complete();
...
wraith dirge
#

i dont know what your types are

#

you can't pass in an Array

sonic condor
#

You also can't use strings.

wraith dirge
#

NativeArray is a custom type that can only hold unmanaged values

#

^ strings are also managed

#

(there is a custom burst support for converting these to fixedstrings under certain circumstances)

lapis nebula
lapis nebula
wraith dirge
#

you cant pass in managed types into a burst job under any circumstances (*this is slightly a lie if you want to get really really hack but just assume it is true)

sonic condor
#

There are ways to get managed types into a job that isn't bursted but it's really roundabout and prone to breaking if someone breathes on it wrong.

lapis nebula
#

Ok thank you, so it seems i can't burst the SpawnEnemyJob method watzke But thank you for the infos, i think now i know where i can use bursty in my code (only few places, but i try to apply it where i can to get warm with burst, as i just started looking into this recently)

median brook
#

generally what you'd want - store data in as unmanaged way as possible and process it batched with bursted jobs

#

but that only relates to algorithms like moving or pathfinding

#

or smth like that

#

also, there's a Transform job

#

which lets you move game objects bursted

#

and multithreaded

lapis nebula
sonic condor
#

IJobParallelForTransform

lapis nebula
#

Thank you very much, i will read into this and convert the code to IJobParallelForTransform 🙂

low geyser
lapis nebula
#

Thanks to all you guys, this really helped, i used google before but many things on burst and dots are still a bit unclear, so the help of the community is of great value and i appreciate your help very much, thank you! Loaded with this info, i should be good to go i guess. Will leave the forum post open another day incase someone wants to add something, for readers that maybe find this post later. Thank you ✌️

sonic condor
median brook
low geyser
lapis nebula
#

guys wait, i think i have a bursted ISystem up and running

#

one moment please

#

Here it is, this uses ISystem, Multithreads and Burst, i use this to move enemies:

using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;
using Unity.Mathematics;

[BurstCompile]
public partial struct Mover : ISystem {

    [BurstCompile]
    public void OnCreate (ref SystemState state){
        state.RequireForUpdate<EnemyCom>(); // currently this is not working right yet, have to check later
    }
    
    public void OnDestroy(ref SystemState state){}

    [BurstCompile]
    public void OnUpdate(ref SystemState state) {
        var playerEntity = SystemAPI.GetSingletonEntity<PlayerCom>();
        var playerWorldTransform = SystemAPI.GetComponent<WorldTransform>(playerEntity).Position;
        var delta = SystemAPI.Time.DeltaTime;
        var moveJobHandle = new MoveJob() {
            playerPos  = playerWorldTransform,
            moveSpeed  = 1f,
            deltaTime = delta
        }.ScheduleParallel(state.Dependency);
        state.Dependency = moveJobHandle;
    }
}

[WithAll(typeof(EnemyCom))]
public partial struct MoveJob : IJobEntity {

    public float3 playerPos;
    public float  moveSpeed;
    public float  deltaTime;

    [BurstCompile]
    private void Execute(ref TransformAspect _transform){
        float3 moveDirection = math.normalize(playerPos - _transform.LocalPosition);
        _transform.LocalPosition += moveDirection * deltaTime * moveSpeed;
        _transform.LookAt(playerPos);
    }
}
median brook
sonic condor
median brook
lapis nebula
#

i am new to dots, so maybe i understand something wrong, but the code example i believe does exactly what you said (refering to Lims prior message, about bursted ISystem)

sonic condor
#

Well, now my hybrid sprite renderer is partially bursted. Neat.

lapis nebula
#

This is great news (as i want to switch to IJPFT, like you guys suggested me) 👍

low geyser
sonic condor
lapis nebula
sonic condor
#

Also if you want, here's the other half of the managed section. This and the earlier screenshot are the typical code required to link a managed game-object to an entity.

lapis nebula
#

Thank you, it is of great help to see code examples, even if i do not understand all of it yet. To my last understanding, baking was the way to link gameObjects to entities, but this seems different

sonic condor
#

Baking and companion GOs are getting wiped from Entities. The impression I've gotten from various Unity dev Q&As are basically dont use companion objects, roll your own hybrid solution. Which is what is being done here.

#

Hybrid isn't very "performant". Ideally you stay either entirely in mono-land or entities-land. Mixing results in a lot of friction. I really dont want to make my own sprite renderer so I live with this.

lapis nebula
#

Oh thats new, i didnt know baking gets removed in future. So they ease up this step it seems, which is appreciated 👍

median brook
#

😅

#

(hopefully)

sonic condor
median brook
low geyser