Roblox는 비동기 작업 처리를 위한 모범 사례를 태스크 라이브러리 도입으로 업데이트했습니다. 이전의 spawn, delay, wait 함수보다 더 깔끔하고 효율적으로 백그라운드 작업을 처리할 수 있습니다. 태스크 라이브러리에는 task.spawn, task.delay, task.wait 등의 메서드가 포함되어 있으며, 이는 더 신뢰할 수 있고 성능이 향상되었습니다.
작업 | 세부 사항 |
---|---|
task.spawn | task.spawn은 호출자를 블록하지 않고 함수를 비동기적으로 실행합니다. |
task.delay | task.delay는 지정된 지연 후에 함수를 실행하며, 다른 작업을 블록하지 않고 타이머 또는 지연 작업을 생성할 수 있습니다. |
task.wait | task.wait는 지정된 시간 동안 현재 작업을 일시 중지합니다. |
task.spawn
local function doBackgroundTask()
print("Background task started")
for i = 1, 10 do
print("Processing step " .. i)
wait(1) -- Simulate some processing delay
end
print("Background task completed")
end
print("Main script execution")
task.spawn(doBackgroundTask)
print("Main script continues immediately after launching background task")
Roblox Studio
task.delay
local function delayedTask()
print("Delayed task executed after 5 seconds")
end
print("Main script execution")
task.delay(5, delayedTask) -- Delay execution of delayedTask by 5 seconds
print("Main script continues immediately after scheduling delayed task")
Roblox Studio
task.wait
local function waitForSomeTime()
print("Wait starting")
task.wait(5) -- Pauses this function, not the entire script, for 5 seconds
print("Wait ended after 5 seconds")
end
print("Main script execution")
task.spawn(waitForSomeTime)
print("Main script continues immediately, while waitForSomeTime pauses internally")
Roblox Studio