Coroutine (コルーチン)

コルーチンは、特定のポイントで関数を一時停止し、再開することができるプログラミングツールです。通常の関数が直線的に実行されるのに対し、コルーチンは指定されたポイントで一時停止し、その後に続行されます。この機能は、プログラム全体を停止させることなく、時間をかけて行う必要があるタスクを管理する際に便利です。
説明
Yield (一時停止)コルーチンの実行を一時停止し、他のコードが実行できるようにする操作です。コルーチンを一時的に停止し、その状態を保存します。
Resume (再開)コルーチンが最後に一時停止した場所から再開する操作で、その実行を続けます。
Status (ステータス)コルーチンの現在の状態を確認するための関数です。例えば、一時停止中、実行中、または終了(完了)しているかを確認します。
Wrap (ラップ)特定のコルーチンを呼び出した際に再開し、一時停止された値のみを返すラップ関数です。

例: Coroutine (コルーチン)

local function countingCoroutine()
	for i = 1, 3 do  -- Count from 1 to 3
		coroutine.yield("Count: " .. i)  -- Yield the count value
	end
	return "Counting completed."  -- Return a final message when done
end

-- Create the coroutine from the function
local co = coroutine.create(countingCoroutine)

-- Start the coroutine and check the status and results at each step
print("Starting coroutine...")
print("Status before start:", coroutine.status(co))  -- Initially, status should be 'suspended'

local success, result = coroutine.resume(co)  -- Resume for the first count
print(success, result)  -- Should print true, "Count: 1"
print("Status after first count:", coroutine.status(co))  -- Should still be 'suspended'

success, result = coroutine.resume(co)  -- Resume for the second count
print(success, result)  -- Should print true, "Count: 2"
print("Status after second count:", coroutine.status(co))  -- Should still be 'suspended'

success, result = coroutine.resume(co)  -- Resume for the third count
print(success, result)  -- Should print true, "Count: 3"
print("Status after third count:", coroutine.status(co))  -- Should still be 'suspended'

success, result = coroutine.resume(co)  -- Resume for the final return
print(success, result)  -- Should print true, "Counting completed."
print("Status after completion:", coroutine.status(co))  -- Should now be 'dead'

-- Check status after completion to confirm no further actions can be taken
success, result = coroutine.resume(co)
print(success, result)  -- Should print false and possibly an error message
print("Status after attempting to resume a completed coroutine:", coroutine.status(co))  -- Should be 'dead'
Image 1
Roblox Studio

例: coroutine.wrap (ラップ) の使用

local function countToThree()
	for i = 1, 3 do
		coroutine.yield("Count: " .. i)  -- Yield with a return value
	end
	return "Counting completed."  -- Return a final message when done
end

local wrappedCoroutine = coroutine.wrap(countToThree)

print("Starting coroutine...")
local result
result = wrappedCoroutine()  -- Executes and yields "Count: 1"
print(result)  -- Prints "Count: 1"
result = wrappedCoroutine()  -- Resumes and yields "Count: 2"
print(result)  -- Prints "Count: 2"
result = wrappedCoroutine()  -- Resumes and yields "Count: 3"
print(result)  -- Prints "Count: 3"
result = wrappedCoroutine()  -- Ends the counting, returns "Counting completed."
print(result)  -- Prints "Counting completed."
result = wrappedCoroutine()  -- Error
Image 1
Roblox Studio

このチュートリアルがお役に立ちましたら、私の仕事をサポートするためにコーヒーをおごっていただけると嬉しいです。

ご支援、誠にありがとうございます!

コーヒーをおごる