Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[Coroutines] Thread Manager? Help?

Asked by 7 years ago

So I am on a quest to make a functional thread manager. It can re-use threads that are cached in a table that can be pulled at any time. So I'm testing it with some lighting and boolean's and the module that controls the lighting requires a boolean to turn on/off the lights. But I keep running into an error. The thread does run but doesn't seem to operate and errors.

Any ideas?

local _ModuleEffects = script.Parent:WaitForChild("Effects")
local _COMMANDS = {}
local _THREADS = {}

-- Threading --

local AddNewThread = function(EffectName, ...)
    local NewThread = coroutine.create(...)
    _THREADS[EffectName] = NewThread
    return NewThread
end

local FindOldThread = function(EffectName, ...)
    if _THREADS[EffectName] ~= nil then
        return _THREADS[EffectName]
    else
        return false
    end
end

local RemoveThread = function(EffectName, ...)
    local Thread = FindOldThread(EffectName)
    if Thread ~= false then
        _THREADS[EffectName] = nil
    end
    return _THREADS[EffectName]
end

local ThreadStatus = function(EffectName, ...)
    local Thread = FindOldThread(EffectName)
    if Thread ~= false then
        return(EffectName.." status: "..tostring(coroutine.status(Thread)))
    end
end

local RunThread = function(EffectName, ...)
    local Thread = FindOldThread(EffectName)
    if Thread ~= false then
        coroutine.resume(Thread)()(...)
        print("Running thread "..EffectName)
    else
        local Module = _ModuleEffects:FindFirstChild(EffectName,true)
        if Module then
            AddNewThread(EffectName, require(Module))
            print("Created new thread for "..EffectName)
        end
    end
end

-- Returning functions to operate --

return RunThread
0
Saying exactly what the error is and showing us what you're using to test this code would be helpful - based on your current description (without analyzing the code), I can only conclude that "an error" occurred. chess123mate 5873 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago

With a few changes to the script I was able to make it work for me, but I don't understand what you're trying to do with line 39. It is guaranteed to fail, by the way, since coroutine.resume always returns a boolean and you cannot call a boolean!

I see a number of issues:

  • The first time someone calls RunThread (for each different EffectName), it won't actually run the thread, only create it
  • AddNewThread accepts a variable number of arguments and sends them all to coroutine.create -- but coroutine.create only accepts one argument (the function with which to create the coroutine).
  • RemoveThread, on line 26, will always return nil. You probably meant to return Thread
  • Your Modules will need to yield (via coroutine.yield) instead of returning for this whole idea to work. If they aren't yielding, you might be over-complicating this.
  • (Tip) You can simplify FindOldThread to return _THREADS[EffectName] or false. If your if statements simply do if Thread then or if not Thread then, then you don't even need the or false.
  • (Note) You make RemoveThread and ThreadStatus, but do not use them nor return them

As for line 39, the correct syntax is coroutine.resume (co [, val1, ...]), and you probably want coroutine.resume(Thread, ...). You may also want to return whatever it returns, or at least check to see what it returns (since the first argument it returns is whether it succeeded or not).

Ad

Answer this question