#Is this a proper way to do an Async function?

1 messages · Page 1 of 1 (latest)

whole abyss
#

I have an expensive (I believe) function, that however is related purely to visuals, so I'd like to call it parallelly with the main code, to not bog it down. Info online on it is kinda super confusing at times, but I gathered that async is the way to go. Would that work?


    //TODO: Make Recalculate() async
    
    private bool meshIsRecalculating;
    private bool recalculationQueued;
    /// <summary> Queue recalculation of the decal mesh </summary>
    public async Task<bool> RecalculateAsync()
    {
        if (meshIsRecalculating)
        {
            if(recalculationQueued) return false;
            recalculationQueued = true;
            return false;
        }
        meshIsRecalculating = true;
        await Task.Run(Recalculate);
        meshIsRecalculating = false;
        return true;
    }
    
    /// <summary> Recalculate the decal mesh </summary>
    public void Recalculate()
    {

        //The expensive calcs here, to cut out intersecting meshes into a final decal mesh

        if (recalculationQueued)
        {
            recalculationQueued = false;
            Recalculate();
        }        
    }

(p.s. I'd love to just use Unity's standard decals instead, but they're incompatible with my stylized lighting shader)

radiant grail
cinder jungle
whole abyss
fossil socket
whole abyss
#

The limitation is as simple as: don't use unity API from threads or you'll get runtime errors.
So I can't use any Unity functions or call Unity classes?

fossil socket
whole abyss
#

Ah, damn, ok, that's exactly what I was intending to use it with... Time to think of another approach, I guess...

fossil socket
tiny willow
shy jasper
shy jasper
#

its truly random

#

a poorman's hack for this is to make your own thread dispatcher in an update loop

cedar summit
#

As UniTasks no 1 fan id recommend that 😆

shy jasper
#

its bad, simply bcos it will allocate each time you instantiate new delegate AND fat chance you'll end up with tons of closures.

#

and its well known that callvirt on delegate is slower than regular call (not that it matters, but still important to mention)

shy jasper
fossil socket
#

Data structures, yes. These don't touch unity Objects.

tiny willow
#

Awaitables does give you an easier way to switch back to the main thread, but if you're stuck with just tasks you can still do that, it's just extra lines of code

shy jasper
#

unity never has a proper support for async/await before awaitable

#

as the dude said, he's using the version where there's no awaitable in it

shy jasper
shy jasper
whole abyss
# shy jasper Task.Run will mess tons of things if you're not careful

On one hand I want to try to make it async after all and try to pass everything in as structs and get back the results (also as struct) once they're done, and apply them to the decal mesh. On the other hand, part of this function is getting volume intersecting mesh renderers and obtaining meshes from them, so it's unclear if there will even be any point in making that function async in the first place if I'll move those heavy calls out of it...

tiny willow