Creating coroutines dynamically for each 'scenario'

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to create an individual coroutine for each ‘scenario’ of battles or pathfinding. That is, a new coroutine is created when a remote event is fired and ‘asks’ the server for a new thread to handle that battle or pathfinding until it is done.

  2. What is the issue? Include screenshots / videos if possible!
    Currently the pathfinding is on a separate thread but if I were to try to have a different unit do its own pathfinding to a hex it would stop the previous unit in order to account for the new one that I selected. Same with battles, because there is a separate thread doing it but only one battle scenario can take up that thread at a time, as it is not a thread per battle, but just one thread handling every time a battle may happen. If that makes sense.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Looked on all the things talking about coroutines but I don’t see anything about dynamic creation of them. Just initializing them by the developer.

2 Likes

Instead of dynamically creating new coroutines, try putting a function with arguments that you can pass and calling that coroutine each time.

local pathfind = coroutine.create(function(Humanoid, WalkTo)
--Perform pathfinding
end)

coroutine.resume(pathfind, workspace.Bot.Humanoid, workspace.Part)
2 Likes

Yea, I switched up from spawn() to coroutine because of the features given but now I’m getting “cannot resume dead coroutine”.

Edit: If all else fails, I’ll have to switch back to spawn(), which is okay, and maybe try to figure out from there.

1 Like

Could you provide some code?
Also are you returning a value, that could also be the cause of cannot resume dead coroutine.

1 Like

Yes I just did a if then return at the top for a scenario of where it violates what I want.

Most of it is within a while loop that checks for obstacles while on the path, and moving the unit onto the path, just some conditions.

Here’s the top, however.

if goal == states.lastGoal then
		return
	end
	if states.running then
		states.reset = true
		repeat wait() until not states.reset
	end
	
	if path then
		states.running  = true
		states.startPos = unit.PrimaryPart.CFrame.p
		states.lastGoal = goal
			
		local lastPoint = states.startPos
		local i = 1
-- while loop going down

Edit: First time working with coroutines and I still think dynamic creation would be better for this situation (and later removing them, if absolutely needed to avoid errors) as it would simplify things and I already thought out a ‘tracking system’ for it.

Edit2: I forgot this as well, my bad.

states.running = false
		if states.reset then
			states.reset = false
		end

At the end of the thread.

2 Likes

Came back home and did some testing, there is some light more with spawn() than coroutines, but overall the answer is still fairly unanswered.

Spawn, because it runs after being initialized gets the crown here as I might be able to make it work with a kind of scheduling/tracking system, but again, still in the works and unanswered. If anyone has anything, would greatly appreciate it.

Edit: If I find a way to create them dynamically, I’ll just add it to this thread so anyone who needs it and stumbles on the thread can have the answer.

1 Like

You might want to look into Evaera’s Promise API which acts as a wrapper around your regular Coroutines and provides some very useful methods for many common operations you might think to do with coroutines, such as having them run one after the other, waiting for multiple coroutines to all be completed etc.

Promises and Why You Should Use Them

1 Like

I’m still looking into it but it seems I had the wrong idea of coroutines from the beginning. The wiki doesn’t explicitly state it, and I didn’t even see it imply it, but only one can run at a time. My case will need multiple running at a time.

So now, is spawn() able to do what coroutines cannot, or am I at a hard limit?

Edit: And yea, the thread also wrote about coroutines being one at a time…