Thread and Scopes

Basic Lua knowledge

by LadyCelastia

Author Avatar

Introduction to Threads

Have you ever tried to run 2 while loops in a script and only 1 of them ran? It's because a while loop stops everything below the loop chunk from working. Why? It's because the while loop and the rest of the script are on the same thread. A thread runs from the first line to the last line. But if you put a while loop in the middle of the script, the while loop will go on forever (Assuming that you don't have any mechanic to break the loop) and the rest of the script will never be executed. How do we solve it?

Solution

We can solve the problem by creating a new thread within the script (Or you can say, thread). But how? Is it possible to create a thread within a thread?

Yes, it's possible. There are multiple methods to create new threads, we'll cover them one by one.

Spawn

spawn(function()
    while wait(1) do
        print("This loop will run!")
    end
end)

while wait(1) do
    print("This loop will also run!")
end

Spawn(f) is the most basic way of creating a new thread, it spawns a new thread that runs at the next frame while not stopping the script itself. You can use it for many things!

Delay

delay(1, function()
    print("This will print 1 second later!")
end)

print("This will print first!")

delay(Int, f) works like spawn(f). But it will wait an amount of time (Determined by Int variable) before executing, while not stopping the script.

Coroutine

coroutine.resume(coroutine.create(function()
    print("This will print first!")
end))

coroutine.wrap(function()
    print("This works just like the above!")
end)

print("This will print later!")

Coroutine has an unique property. Instead of executing the wrapped code in the next frame, it executes them immediately. Can be helpful in certain circumstances.

Coroutine Advanced

Coroutine has way more methods and functions than spawn(f) and delay(Int, f).

coroutine.yield()

local MyFunction = coroutine.wrap(function(Variable)
    print("I am called! " ..Variable)
    local NewCallVar = coroutine.yield()
    print("I am called again so I'm resuming! " ..NewCallVar)
end)

Put a coroutine on yield until it's called again.

coroutine.status()

local function NormalFunction()
    print("I am called!")
    wait(1)
    print("I am now dead!")
end

local NewThread = coroutine.create(NormalFunction)

print(coroutine.status(NewThread)) -- suspended

coroutine.resume(NewThread)

wait(.1)

print(coroutine.status(NewThread)) -- running

wait(2)

print(coroutine.status(NewThread)) -- dead

Returns a coroutine's status as string. Return suspended if the coroutine is on yield or not resumed. Return running when the coroutine is running. Return dead when the coroutine isn't on yield nor running.

coroutine.running()

local IAmAFunction = coroutine.create(function()
    print("I am called!")
    wait(1)
    print("I am now dead!")
end)

print(coroutine.running()) -- Thread: XXXXXX

coroutine.resume(IAmAFunction)

wait(.1)

print(coroutine.running()) -- Thread: YYYYYY

coroutine.yield(coroutine.running()) -- This works!

Returns the current running thread.

Conclusion to Threading

There's endless possibilities with coroutine, spawn(f) and delay(Int, f) ! Make sure to explore them yourself!

Introduction to Scope

Scope is useful for creating a chunk of code that doesn't affect the rest of the code. For example, variables in scopes don't carry to the main code.

How to create a scope

It's actually very simple to create a scope. Just do:

do
    print("This will print first!")
    print("This is a new scope!")
    local MyVar = "I will not be carried to the main code!"
    wait(1)
end
print("This will print 1 second later!")
print("I am outside the scope!")
print(MyVar or "Doesn't exist!") -- Doesn't exist!
print(MyVar) -- nil

Conclusion to Scope

Scope is useful for code that doesn't have anything to do with the main code. To be honest, it doesn't have much uses.

And that wraps it up! Thanks for viewing my community tutorial! Please leave a comment and a rating! :D

View in-game to comment, award, and more!