Jump to content

[MOOSE] - Cannot use global variables in Scheduler


Recommended Posts

Trying to reference a global variable in a scheduler object and getting the error:
 

Error in timer function: [string "..."]:14: attempt to index global 'bravo' (a nil value)

 

Here is the script I'm trying to run. It is a loop that looks for a group to pass into a zone_polygon. Once it detects that all or part of the group is in the zone, it checks each unit's altitude to see if they fly over 1500 ft. If any of them do, it spits out a message and stops the scheduler.

BASE:TraceAll( false )
BASE:TraceClass("SCHEDULER")
BASE:TraceClass("TIMER")

bravo = GROUP:FindByName('Bravo-1')
groupPolygon = GROUP:FindByName("NoFlyZone")
tzLimit = ZONE_POLYGON:New("NoFlyZone",groupPolygon)

checkOverHeight = SCHEDULER:New(nil,
  function()
    BASE:E('looking for bravo')

    if bravo:IsActive() then
      BASE:E('found bravo')

      if bravo:IsPartlyOrCompletelyInZone(tzLimit) then
        BASE:E('bravo in zone')

        local units = bravo:GetUnits()

        for x=1, #units do
          BASE:E('check bravo height')

          if bravo:GetUnit(x):GetAltitude() > 457.2 then
            BASE:E('bravo height exceeded')

            bravo:MessageToAll("Bravo height: " .. bravo:GetUnit(x):GetAltitude(), 30)
            checkOverHeight:Stop()

          end
        end
      end
    end
  end, {}, 0, 2)

 

I have a similar script working where the bravoGroupPolygon, and tzLimit are declared as locals inside the scheduler function. However, I feel like I should be able to reference the global declarations and save some logic ticks on not instantiating the groups and zone every 2 seconds. Anyone know of a way to do this?


Edited by Proxy404
Link to post
Share on other sites

The log error in line 14 means DCS can not identify the object bravo. Usually means  the object does not exist. Perhaps some issue with ME group name, case, etc. Or . .  Is it late activated? Spawned?

Link to post
Share on other sites

For debug, do the group:findbyname in the scheduler function. Just to test if the issue is global function or the fact the group cannot be instance (my opinion sound like Habu one, issue on this group. However, late activated is not an. issue for a find)

Link to post
Share on other sites
1 hour ago, Habu_69 said:

The log error in line 14 means DCS can not identify the object bravo. Usually means  the object does not exist. Perhaps some issue with ME group name, case, etc. Or . .  Is it late activated? Spawned?

Like I mentioned above, if I move the declaration into the function, it works fine, so unlikely an issue with the ME name. But I think you solved my issue with the point on the spawn time. Bravo-1 is a player group. By that I mean the humans are picking the planes in Bravo-1 and Bravo-1 would be non-existent until the human(s) hit "fly". So there is a timing issue with when the bravo variable is declared. I'd need to update bravo when a plane spawns in and change the "if bravo:IsActive() then" condition to just "if bravo then". Thanks @Habu_69!

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...