Jump to content

St3v3f

Members
  • Posts

    499
  • Joined

  • Last visited

  • Days Won

    1

Community Answers

  1. St3v3f's post in mist.flagFunc.Units_in_zones -- Spherical was marked as the answer   
    My script does exactly that. It's working like the 'flagFunc' scripts, but turns the flag off again when there are no units in the zone anymore.
     
    'mist.scheduleFunction' schedules a function to run at a later time. The 'unitsInZonesOnOff' function schedules itself to run again in one second after it finished (timer + 1), passing on the 'result' variable as an argument.
    That is the reason you can call my script once and it continues to run until the mission ends.
     
    The 'result' variable is an argument given to my unitsInZonesOnOff function. It is the result from the last time the function ran (Units in zone, yes or no). You could build the function without it, by unconditionally setting the flag true or false every time, but then you couldn't use 'time since flag' conditions in the ME because the flag is set every second.
    The last line 'unitsInZonesOnOff(false)' is what really kicks off the script itself, it calls the function for the first time (after that it re-calls itself with scheduleFunction) and passes on false as an argument, because no units in the zone is the initial state in the mission.
    Before this last line, the function has only been defined, it didn't run yet.
     
    What the script basically does is:
    - Get a list of units in the zone
    - Check if the status changed:
    - If there are no units in the the zone NOW (#ret == 0) but there have been units in the zone BEFORE (result == true), then deactivate the flag and change result to false (so the script knows there have been no units in the zone next time it runs)
    - If there are units in the the zone NOW (#ret > 0) but there have been no units in the zone BEFORE (result == false), then activate the flag and change result to true (so the script knows there have been units in the zone next time it runs)
    - Schedule to run again in one second
     
    To use the script for other situations, just modify the variables in the first lines.
    The script is quickly adaptable to work with 'UnitsInMovingZones' as well:
     

    local units = mist.makeUnitTable({'[blue][plane]'}) local flag = 50207 local zones = mist.makeUnitTable({'[g]Red SGrp01'}) local radius = 300 local zone_type = 'sphere' local function unitsInMovingZonesOnOff(result) local ret = mist.getUnitsInMovingZones ( units, zones, radius, zone_type ) if #ret == 0 and result == true then trigger.action.setUserFlag(flag, 0) result = false elseif #ret > 0 and result == false then trigger.action.setUserFlag(flag, 1) result = true end mist.scheduleFunction (unitsInMovingZonesOnOff, {result}, timer.getTime() + 1) end unitsInMovingZonesOnOff(false)
×
×
  • Create New...