Jump to content

Recommended Posts

Posted

HI All,

I am trying to use the timer.scheduleFunction to call a function, but to call it with multiple arguments, (Group_Name, GRP_Size_Prob).  I have tried enclosing the multiple arguments in both () and {}, and it always gives an error when it gets to local Current_Group = Group.getByName(Group_Name) in the called function.  The code runs fine if I call it with just one argument, Group_Name.

 

Below is a simplified version, and following that more of the code (not the full listing)

Please keep in mind that I am a noob at lua/programing/DCS API.

Thanks,

Clutter

<><><><><><><>

timer.scheduleFunction(Adjust_Num_Units_In_Group, {Group_Name, 0.25}, timer.getTime() + Group_Time + 1) 

function Adjust_Num_Units_In_Group (Group_Name, GRP_Size_Prob)               -- Function to determine if the group has 2 of 4 planes, and then delete planes 3 and 4

  local Current_Group = Group.getByName(Group_Name)            -- CHeck to see if the group exist.

  if not Current_Group then return end                             -- no such group

 

  local size = Current_Group:getSize()                        -- how many in group

 

end

 

timer.scheduleFunction(Adjust_Num_Units_In_Group, {Group_Name, 0.25}, timer.getTime() + Group_Time + 1)

 

-- change (F_A2A_LAG_1 ~= nil) to (LAG1 ~= nil) and retry -2/12/23 still to test

function Adjust_Num_Units_In_Group (Group_Name, GRP_Size_Prob)               -- Function to determine if the group has 2 of 4 planes, and then delete planes 3 and 4
  local Current_Group = Group.getByName(Group_Name)            -- CHeck to see if the group exist.
  if not Current_Group then return end                             -- no such group 
  
  local size = Current_Group:getSize()                        -- how many in group
  
  if size >=3 then
   local Size_Prob_Calc = math.random()
       if (Size_Prob_Calc > GRP_Size_Prob) then
      for i= 3, size do 
       local allUnits = Current_Group:getUnits()
       if not allUnits then return end -- sanity check
       trigger.action.outText("Group " .. Current_Group:getName() .. " has " .. size .. " units and I will remove unit number " .. i, 30)
       local unitToRemove = allUnits[i]
       unitToRemove:destroy()                                                                               -- remove it from game
      end
    else
    trigger.action.outText("Group " .. Current_Group:getName() .. " has " .. size .. " units and none will be removed", 30)
   end      
     else
     trigger.action.outText("Group " .. Current_Group:getName() .. " has " .. size .. " units and none will be removed", 30)      
  end  
end

function Spawn_A2A_LAG (Group_Name, Group_Prob, Group_Time)
--[[
     Group_Name,  A2A_LAG_TBL[1][1]
     Group_Prob,  A2A_LAG_TBL[1][2]
     Group_Time   A2A_LAG_TBL[1][3]

--]]
  local Current_Group = Group.getByName(Group_Name)
  if (Current_Group ~= nil) then
    local x= math.random()
    if (x <= Group_Prob) then
  --    trigger.action.outTextForCoalition(2,MSG_Header .. "A2A_LAG_1 will  be spawned.  Probability is " .. x .. MSG_Footer, 15)
      timer.scheduleFunction(F_A2A_LAG_1, Current_Group, timer.getTime() + Group_Time)             -- Activate Enemy CAP flight group 1
  --    trigger.action.outTextForCoalition(2,"\n====================\n\n    TSS_A2A_LAG_1 is " .. TSS_A2A_LAG_1 .. "!\n\n====================", 10)
      A2A_Count = A2A_Count +1                                                             -- Increment A2A counter
      TBL_A2A_LAG_Times[A2A_Count] = math.floor((Group_Time/60)*100)/100                                         -- Add LAG start time to table - Convert to minutes
      timer.scheduleFunction(Adjust_Num_Units_In_Group, {Group_Name, 0.25}, timer.getTime() + Group_Time + 1)             -- Line to adjust units in A2A_LAG group based on a given probability
     else
      trigger.action.outTextForCoalition(2,MSG_Header .. "A2A_LAG_1 will not be spawned, Probability is " .. x  .. MSG_Footer, 15)
    end
  end
end
-- End of function Spawn_A2A_LAG

for i = 1, 2 do
  A2A_LAG_Spawn = Spawn_A2A_LAG (A2A_LAG_TBL[i][1], A2A_LAG_TBL[i][2], A2A_LAG_TBL[i][3])
  -- A2A_LAG_2 = Spawn_A2A_LAG (A2A_LAG_TBL[2][1], A2A_LAG_TBL[2][2], A2A_LAG_TBL[2][3])
end

 

----------------

AKA_Clutter

 

Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080  FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.

Posted

Suppose the easiest way is to build the function you are calling to accept a table.

function test(var)
  env.info(var[1])
  env.info(var[2])
end

timer.scheduleFunction(test, {"Hello", "World"}, timer.getTime() + 2)

You can also name the variables. 

function test(var)
  env.info(var.groupName)
  env.info(var.groupSize)
end

timer.scheduleFunction(test, {groupName = "Whatever", groupSize = 47}, timer.getTime() + 2)

 

  • Like 1

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Posted (edited)

Thanks, I'll give that a go.  Knew there had to be a way.

 

DO I need to use "env.info" or is that just part of the example.

 

Edited by AKA_Clutter

----------------

AKA_Clutter

 

Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080  FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.

Posted

Part of the example, it just prints whatever the values are to the log. It is showing how to access each value though. Table index[1] if the table is indexed numerically or by var.keyName if the table entries have keys. 

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Posted (edited)
53 minutes ago, TJTAS said:

I thought scheduled functions had to have a return in them?

Only if you wish them to be called again. In that case, return the number of seconds + timer.getTime() for the next invocation.

In all other cases, return either nil, or nothing, which is the same.

 

Edited by cfrag
  • Like 1
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...