Using an expression inside a sync

I want to use ‘sync’ to call an asynchronous function to act on objects in an array, but I don’t know in advance which will need to be acted upon. The following code compiles, but the functions in the for loops execute one after the other. Does someone know how to write this so that the functions all execute at the same time?

sync:
for (i = 1…3):
for (j = 1…3):
if (Test(i, j) = true):
CallAsyncFunction(i, j)
OtherFunction()

sync:
    CallAsyncFunction(i, j)
    OtherFunction()
    block:
        for (i := 1..3)
        
    block:
        for (j := 1..3):
            
    block:
        if (Test(i, j) = true):

You’re not understanding how the sync statement works, it doesn’t let you call an asynchronous function but rather wait for multiple asynchronous/suspending blocks to end before completing

The issue with this method is that it’s not implemented natively for arrays rn

For some reason you didn’t put any indent here, so I’ll assume you’re trying to do the following (which shouldn’t compile, just pseudo code)

sync:
    for (i = 1…3):
        for (j = 1…3):
            if (Test(i, j) = true):
                CallAsyncFunction(i, j) # Wait for all async calls to complete before calling the OtherFunction()

OtherFunction()

It’s not pretty but you could try something like that

OnBegin<override>()<suspends>:void=
    Tasks := for(I := 1..10) { spawn{CallAsyncFunction()} }
    # Will fail if the async method completes instantly though
    AwaitFunctions(Tasks)
    OtherFunction()

CallAsyncFunction()<suspends>:void=
    Sleep(GetRandomFloat(0.1, 5.0))
    Print("CALL ASYNC FUNCTION")

AwaitFunctions(Functions: []awaitable(t) where t:type)<suspends>:void=
    CompleteEvent : event() = event(){}
    var CompletedCount : int = 0
    sync:
        loop:
            CompleteEvent.Await()
            set CompletedCount += 1
            if(CompletedCount >= Functions.Length):
                break

        for(Function : Functions):
            spawn{AwaitFunction(Function, CompleteEvent)}
            Sleep(-1.0) # Tell the compiler that we're suspending eventhough we're not

AwaitFunction(Function: awaitable(t), CompleteEvent: event() where t:type)<suspends>:void=
    Function.Await()
    CompleteEvent.Signal()

OtherFunction():void=
    Print("OTHER FUNCTION")

Which gives this result
image

Sorry for the lack of indent…I’m sure that didn’t help. You are basically correct on what I was trying to do. I’m not sure how I am misunderstanding the purpose of sync; I still want it to run several asynchronous functions at once and not move on until they have all run. The key difference is that I don’t know exactly how many functions (and what exact parameters) I want to run and it is based on data within an array. I basically came to the same conclusion that I will have to track the thread completions myself and then send an event when they are completed. It sure would be a lot easier if I could use “sync” though. Thanks for your help. :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.