코루틴

코루틴은 함수가 특정 지점에서 일시 중지하고 다시 시작할 수 있게 해주는 프로그래밍 도구입니다. 일반 함수와 달리, 코루틴은 지정된 일시 중지 지점에서 멈추고 나중에 다시 시작됩니다. 이 기능은 전체 프로그램을 멈추지 않고 시간이 걸리는 작업을 관리하는 데 유용합니다.
설명
일시 중지코루틴 내에서 사용되어 실행을 일시 중지하고 다른 코드가 실행될 수 있도록 합니다. 코루틴을 일시 중지하고 상태를 저장합니다.
다시 시작코루틴이 마지막으로 일시 중지된 지점에서 다시 시작되는 작업으로, 실행을 계속합니다.
상태코루틴의 현재 상태를 확인하는 함수로, 예를 들어 일시 중지됨, 실행 중, 완료됨 (죽음) 등의 상태를 확인할 수 있습니다.
래핑특정 코루틴을 래핑하여 호출 시 다시 시작되며, 반환되는 값은 일시 중지된 값만 반환됩니다.

예제: 코루틴

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

이 튜토리얼이 도움이 되셨다면, 저의 작업을 지원하기 위해 커피 한 잔을 사주시면 감사하겠습니다.

지원해 주셔서 정말 감사합니다!

커피 한 잔 사주기