Proper handling of asynchronous operations in loops?

So, I tend to have a lot of operations where I don’t care about the timing. Moving a set of objects, managing scoring for a set of things that all can happen pretty much whenever, and so on.

I tend to handle these operations asynchronously. (Or need to, because moving twenty things together needs to happen as a group) The only way I can do this at the moment is - I think - through spawn{}

Is this the proper way to manage these threads? I can’t use Thread within loops, but the documentation says to only use spawn{} as a last resort.

I want to make this example device work properly:

mass_activator_device := class(creative_device):
    @editable Props : []creative_prop = array{}
    @editable DropIn : logic = true
    @editable DropInHeight : float = 1000.0
    var DropInTime : float = 1.0

    ActivateAllDrop(): void=
        var prop_transform : transform = transform{}
        for(i := 0..Props.Length-1):
			if (CurrentProp := Props[i]):
				CurrentProp.Show()
				set prop_transform = CurrentProp.GetTransform()
				spawn{CurrentProp.MoveTo(vector3 {X:=prop_transform.Translation.X, Y:=prop_transform.Translation.Y, 
					Z:=(prop_transform.Translation.Z-DropInHeight)}, 
					prop_transform.Rotation, DropInTime)}


Note that I’ve messed up the spacing here to make this all fit together. Also, this should probably be a couple functions, but was an alpha version of what I ended up actually using.

But what I’m doing is using spawn to move a bunch of props. I’d like to know what the right way to do this is. I’d really love to be able to use Race, but either I can’t figure out the syntax or it’s such a bad idea it’s intentionally disabled.

1 Like

Hello,

I’m still at the ‘I understood some of those words’ stage, but I did just watch a concurrency presentation and there was a slide about spawn.

1 Like

You problem is slightly not clear for me. Are you trying to say you want to execute some code while simultaneously executing async functions that are independent from the rest of the code?

Well, I watched that presentation myself yesterday and today. I’m going to edit the original question, and add an example.