musurca
Posts: 128
Joined: 7/16/2020 Status: offline
|
I've dealt with this issue quite a bit--there are a few different approaches that you can take. The "easiest" and most CMO-specific way is to dynamically create a new Event which triggers a script at a specific date and time in the scenario (see the Command Lua documentation for the trigger type called "Time"). Unfortunately setting up a dynamic event is a bit unwieldy, and there are some weird and undocumented issues related to how the time value is encoded. Here's a function which should handle them: ---- function WaitFor(seconds, script) local function uuid() local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' return string.gsub(template, '[xy]', function (c) local v = (c == 'x') and math.random(0, 0xf) or math.random(8, 0xb) return string.format('%x', v) end) end -- add these special magic numbers. don't ask why. local run_epoch = (62135596800 + ScenEdit_CurrentTime() + seconds) * 10000000 local evt_name = uuid() ScenEdit_SetEvent( evt_name, { mode='add', IsRepeatable=false, IsShown=false } ) local trigger_name = uuid() ScenEdit_SetTrigger( { name=trigger_name, mode='add', type='Time', Time=run_epoch } ) local action_name = uuid() ScenEdit_SetAction( { name=action_name, mode='add', type='LuaScript', ScriptText=script } ) ScenEdit_SetEventTrigger( evt_name, { mode='add', name=trigger_name } ) ScenEdit_SetEventAction( evt_name, { mode='add', name=action_name } ) end ----- Example usage: SetUnitComms(my_unit_guid, True) WaitFor(15, "SetUnitComms(my_unit_guid, False)") ----- (A better version of this function would clean up after itself to avoid leaving dead events in the list, but I leave that as an exercise for the reader.) A slightly more flexible approach would be to use Lua coroutines instead. In that version, you would have a coroutine manager that runs every second and iterates through a stack of active coroutines which would yield to wait a second of sim time. However I wouldn't bother if you're not familar with coroutines and won't be needing this pattern often.
< Message edited by musurca -- 7/22/2021 7:48:56 AM >
|