Mulitiple coroutines tasks

Dear sirs,

I would like to make a function that on certain events makes a bulb blink using for-next cycle with some waits() in it. The whole function would last for 2-3 seconds, but I do not want to slow down the main thread (while-cycle) of the game. So, I would like to run a function that makes the bulb blink in additional threads. The problem is that events that make the bulb blink may occur during time when the bulb is already blinking, i.e. the thread is still alive. All sorts of tries plus debouncing did not really help me.
I cannot figure out the right method of declaring the function and calling it. Any ideas, please?

1 Like

Could we see the code you already have regarding this?

Question


Are you looking for something like this?

1 Like

If bulbs blink here not randomly, but because of external events and the bulbs blink without interrupting the main cycle, then I would like to see how blink-functions are declared and called.

What do you want to happen when …

The problem is that events that make the bulb blink may occur during time when the bulb is already blinking, i.e. the thread is still alive.

… ??? Do you want the blink to remain ON longer (add cumulative time?) or do you want to ignore the effect of a new blink event if the build is already blinking? Or something else?

Did you try this pattern with coroutines?

Maybe something like:

local blink = false
local changeBlink = coroutine.wrap(function()
    for i = 60, 0, -1 do
       wait(2.5)
       blink = true
    end
end)
changeBlink()

They all use coroutine.wrap(). For each light, I run a function which setup the coroutine for one. Then I have the blinking logic which is a table of functions. I’ll be brief:

-- Raw extract from the table. The logics are random, but they always have a fixed pattern.
function(light)
    for i = 1, 3 do
        light.BrickColor = BrickColor.new("White")
        wait(1.25)
        light.BrickColor = BrickColor.new("Really blue")
        wait(1.25)
    end
end;

The setup do not need a declared function.

coroutine.wrap(function()
    print("This is a coroutine without any name; cannot be called again.")
end)()

Wrap does not work:
Message: cannot resume dead coroutine

After long days of headaches, I found the solution here:

Thank you for those who participated in the conversation.

So…

Here is the code:

  1. There is a function that makes a bulb blink, there is no need to give the code, because above there are a few examples. It is an easy task. Lets call it data_bulb_blinker(…)
  2. We need a function that re-creates threads and do not let them conflict:
local function Data_bulb_sub(seat_index_str)
      local co = coroutine.create(function(seat_index_str)
            data_bulb_blinker(seat_index_str)
      end)
      if coroutine.status(co) == "dead" then
            co = coroutine.create(function(seat_index_str)
                  data_bulb_blinker(seat_index_str)
            end)
      end
      coroutine.resume(co, seat_index_str)
end
  1. In the main thread-cycle I just call the function simple like this:
    Data_bulb_sub(rec_player_LST.seat)

Here is the result!!

4 Likes