DCS func scheduleFunction

From DCS World Wiki - Hoggitworld.com
Revision as of 10:25, 27 April 2025 by Grimes (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Scripting Root

Envrioment: Mission Scripting
Function: scheduleFunction Added with: 1.2.0
Member Of: timer
Syntax: functionId timer.scheduleFunction(function functionToCall , functionArgs anyFunctionArguement , time modelTime )
Description: Schedules a function to run at a time in the future. This is a very powerful function.

The function that is called is expected to return nil or a number which will indicate the next time the function will be rescheduled. Use the second argument in that function to retrieve the current time and add the desired amount of delay (expressed in seconds).


Return Value: functionId
Return Example: 3
Examples: The following will run a function named "main" 120 seconds from one the code would run.
 timer.scheduleFunction(main, {}, timer.getTime() + 120)


The following example sets up a repetitive call loop where function CheckStatus is called every 5 seconds or.

function CheckStatus(ourArgument, time)
 -- Do things to check, use ourArgument (which is the scheduleFunction's second argument)
 if ourArgument == 53 and someExternalCondition then
   -- Keep going
   return time + 5
 else
   -- That's it we're done looping
   return nil
 end
end
timer.scheduleFunction(CheckStatus, 53, timer.getTime() + 5)


This function will check if any red coalition units are in a trigger zone named "anyReds" and will set the flag "zoneOccupied" to true. This function will schedule itself to run every 60 seconds.

  local function checkZone(zoneName)
     timer.scheduleFunction(checkZone, zoneName, timer.getTime() + 60)
     local zone = trigger.misc.getZone(zoneName)
     local groups = coalition.getGroups(1)
     local count = 0 
     for i = 1, #groups do
        local units = groups[i]:getUnits()
        for j = 1, #units do
            local unitPos = units[j]:getpoint()
             if math.sqrt((zone.point.x - unitPos.x)^2 + (zone.point.z - unitPos.z)^2) < zone.radius then
                 count = count + 1
             end
        end
     end
     if count > 0 then
         trigger.action.setUserFlag("zoneOccupied", true)
     else 
        trigger.action.setUserFlag("zoneOccupied", false)
     end
   end
   checkZone("anyReds")
Related Functions: timer Functions: getTime, getAbsTime,getTime0, scheduleFunction, removeFunction, setFunctionTime
Notes: