Technical Tip : Checking for Task Completion in Lua Scripts
 
If you have used entity plans and global plans in VR-Forces, you know that in an entity plan, entities execute tasks in the order that they are listed in the plan. A new task is not started until the previous task is complete. In a global plan, tasks get sent without regard to the status of the prior task and the most recently sent task will override earlier ones unless you structure the plan to prevent that. Unfortunately, there is nothing in the plan language to help you know when a task is complete, so writing global plans can be tricky. If you create a scripted task, the basic behavior is similar to a global plan.
 
Suppose you send a series of tasks like this:

vrf:startSubtask(“some-task”)
vrf:startSubtask(“another-task”)
vrf:startSubtask(“final-task”)
 
It is likely that some-task and another-task will not run to completion, because final-task will override them. However, unlike global plans, the VR-Forces Lua API provides functions that let you test for task completion. So when you write a scripted task, you can do the following:

firstTaskID = -1
secondTaskID = -1
thirdTaskID = -1
function tick()
-- always send the first task the first time through tick
if firstTaskID == -1 then
firstTaskID = vrf:startSubtask(“send-text”, {destination_name = “DtBroadcast”, text=”first task”})
end

-- when the first task is complete, send the second task
if vrf:isSubtaskComplete(firstTaskID) then
-- don’t want this to be true the next tick and don’t want firstTaskID == -1 to be true either.
firstTaskID = 0
secondTaskID = vrf:startSubtask(“send-text”, {destination_name = “DtBroadcast”, text=”second task”})
end

-- when the second task is complete, send the third
if vrf:isSubtaskComplete(secondTaskID) then
secondTaskID = -1
thirdTaskID = vrf:startSubtask(“send-text”, {destination_name = “DtBroadcast”, text=”third task”})
end

-- when the third task is complete, exit
if vrf:isSubtaskComplete(thirdTaskID) then
vrf:endTask(true)
end