Jump to content

Questions on timer.scheduleFunction in a for loop, timer. removeFuntion, and functionID


Go to solution Solved by cfrag,

Recommended Posts

Posted

Hi all,

 

OK, I have a long winded, multifaceted question.  I am using the base DCS api, and how no doubt there are better ways of doing. However, I am trying to learn the API and Lus.  My questions are more about the DCS API, how it works, and that one can do in lua.

 

I have developed a script to spawn up to 10 la

te activation air CAP groups, with each group having a different probability of spawning and a different start time.  I just a for loop to iterate through these 10 groups and then use the timer.scheduleFunction() to schedule the different groups.

 

At some point, I may want to use the timer.removeFunction (number functionid) function to remove one or move of the timer.scheduleFunction() set by the for loop.

 

From the Hoggit wiki page, it has the timer.scheduleFunction() set up as

“local id = timer.scheduleFunction()”

 

This generates SO many questions for me, I hope these make sense.

1.  For the case where I run through the for loop to generate different timer.scheduleFunctions, do they have function IDs?  If so how do I get the function ID?

 

2.  Is the “functionid” a number or name?  The wiki description seems to indicate a number, but that isn’t a proper lua name.

 

3.  Assuming it is a proper lua name, is there a way to change the functionid/name as you iterate through the for loop.  For example, the first time through the loop the timer.scheduleFunction has an id fo F_01, then second time the fuctionid is F_02

 

I hope those question make sense.  I have attached a snippet of the script for illustration of what I am doing, and what generated the above questions. INDEX_TBL contains the starting index and the ending index of the groups to be spawned.  That way I can easily change if it spawns 5 groups or 10 groups.

 

PS Let me know if this isn’t in the correct forum section.

 

--[[

=================================================================

    function Spawn_LAG. - receives six inputts.  These are:
     i                    Increment value from the main  "for loop" that calls this function.
     Group_Name,          LAG_TBL[1][1]
     Group_Prob,          LAG_TBL[1][2]
     Group_Time           LAG_TBL[1][3]
     Group_Time_Window    LAG_TBL[1][4]
     Group_Num_Prob,      LAG_TBL[1][5]
        
    Call timer.scheduleFunction for each gropu to be activated./  Check the group name for nil values.

=================================================================
--]]

function Spawn_LAG (i, Group_Name, Group_Prob, Group_Time, Group_Time_Window, Group_Num_Prob)    
  local Current_Group = Group.getByName(Group_Name)
  if (Current_Group ~= nil) then
    local GRP_Size_Org = Current_Group:getSize()                                              -- Determines how many are in the current group
    local Group_Prob_Test = math.floor(math.random()*1000)/1000                               -- determines grop test proabaility to three decimal places, mainly for printing
    local TSS_Modifier, GRP_Spawn_TSS = fGRP_Spawn_TSS(Group_Time, Group_Time_Window)         -- Calls function to determiene the Spawn tiume based on a base time and a time window
    LAG_TBL[i][9] = TSS_Modifier
    LAG_TBL[i][12] = GRP_Spawn_TSS
    LAG_TBL[i][6] = GRP_Size_Org
    LAG_TBL[i][7] = Group_Prob_Test
    if (Group_Prob_Test <= Group_Prob) then
      LAG_TBL[i][8] = "Yes"
      timer.scheduleFunction(F_ACTIVATE_LAG, i, timer.getTime() + GRP_Spawn_TSS*60)                         -- Activate Enemy CAP flight group. 
      timer.scheduleFunction(Adjust_Num_Units_In_Group, i, timer.getTime() + GRP_Spawn_TSS + 2)             -- Line to adjust units in A2A_LAG group based on a given probability
     else
      LAG_TBL[i][8] = " No"
    end
  end
end
-- End of function Spawn_LAG

--[[
=================================================================

                  Main LAG Spawn loop
        
=================================================================
--]]

function LAG_Activation (Index_TBL)
  for i = Index_TBL [1], Index_TBL[2] do
    LAG_Spawn = Spawn_LAG (i,LAG_TBL[i][1], LAG_TBL[i][2], LAG_TBL[i][3],LAG_TBL[i][4],LAG_TBL[i][5])
  end
return
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.

  • Solution
Posted (edited)
1 hour ago, AKA_Clutter said:

1.  For the case where I run through the for loop to generate different timer.scheduleFunctions, do they have function IDs?  If so how do I get the function ID?

They all have unique id, yes. The ID is returned as a result, similar to when you call a function like math.random(100) (except random numbers aren't unique). You receive a number, and that number uniquely identifies the entry in DCS's table of future invocations, something like a 'ticket number' that you draw when you are waiting in a queue, or handing off a coat at a valet service. The number on the ticket has no intrinsic information for you, you only can use it to get your coat back. Similarly that ID ('ticket number') says nothing about when the invocation will happen, but it identifies it so you can later tell the system: "hey, delete scheduled ticket number <xyz>, it's no longer needed".  It's that ticket number that you feed to timer.removeFunction()

Since you choose to ignore the result, you lose access to the id. When you write 

1 hour ago, AKA_Clutter said:
timer.scheduleFunction(F_ACTIVATE_LAG, i, timer.getTime() + GRP_Spawn_TSS*60)

the ID that is returned from timer.scheduleFunction is simply discarded. To retain the value, you need a variable to store it, something like 

mySavedID = timer.scheduleFunction(F_ACTIVATE_LAG, i, timer.getTime() + GRP_Spawn_TSS*60)

Now, when the function invocation is scheduled, it hands you back the 'ticket number' for you to do with as you see fit.

 

1 hour ago, AKA_Clutter said:

2.  Is the “functionid” a number or name? 

The current implementation uses numbers, but that may change any day. Treat it as an opaque, abstract 'nubbin'. It contains no valuable information inside, other that it allows you to later cancel the invocation. You cannot (read must not) change that ID yourself

1 hour ago, AKA_Clutter said:

3.  Assuming it is a proper lua name, is there a way to change the functionid/name as you iterate through the for loop.

That would be nonsensical to the system. Like the valet service, it alone knows how to match the ID that it passed to you back to the scheduled item. So if you change it, there is no way it will later be able to match the changed number to the invocation. It would be like drawing a number for your coat, and then changing that ticket number to some other value yourself. When you try to turn in an altered number, the valet doesn't know what to do with it. Best case: you get the wrong coat. 

So, if you need to to know which ticket you received relates to which invocation, built a key system for yourself, and store the scheduled ID's in an array that uses the key as index, e.g. 

myIDTable["groupKappa03"] = timer.scheduleFunction(...)

That, of course, requires that you feel comfortable with arrays. If you aren't already, this would be the ideal time to get acquainted with one of Lua's best features 🙂

 

Edited by cfrag
Posted (edited)
42 minutes ago, cfrag said:

So, if you need to to know which ticket you received relates to which invocation, built a key system for yourself, and store the scheduled ID's in an array that uses the key as index, e.g. 

myIDTable["groupKappa03"] = timer.scheduleFunction(...)

That, of course, requires that you feel comfortable with arrays. If you aren't already, this would be the ideal time to get acquainted with one of Lua's best features 🙂

 

 

Cfrag,

Thanks so MUCH!  This explains a LOT and tells me what I need to do.

I am just getting my toes wet with tables.   I don't understand "for pairs" and for ipairs" in arrays with multi rows and columns, but I haven't HAD to use that yet.

I do use one table that is an "array" of sorts.  It has 30 rows (one for each group I want to spawn) and 13 columns for each row that contain various prices of data.

If I understand the above correctly, I should be able to do what you point out by simply revising the line in the for loop from

     timer.scheduleFunction(F_ACTIVATE_LAG, i, timer.getTime() + GRP_Spawn_TSS*60)

to

    LAG_TBL[i][14] = timer.scheduleFunction(F_ACTIVATE_LAG, i, timer.getTime() + GRP_Spawn_TSS*60)

Thanks again for the quick response.

Clutter

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 (edited)

Tables are easy and fundamental to Lua, there are 2 types and 1 modifier (metatables): Array, Unordered Map (key, value). Key things to note are;

  • 'pairs' will iterate over a key, value table or an array using the index as a key instead of a string, and pairs does not guarantee any order of return.
  • 'ipairs' will do the same as above but cannot be used on a key, value table (i think) or at least it will break on a string key (again i think), pairs does not hold a ref to the current position of the place in the loop whereas 'ipairs' does.
  • arrays are sets of objects with indexing as a reference and each element is contiguous, and will break if the index's are not so or any position in the array is 'nil', likewise you cannot remove an element in an array by '= nil' whilst looping over it like you can with a key, value table and shouldn't as a rule at all, always remove with 'table.remove' outside of a loop.
  • Key, value tables are very fast, and arrays are orders of magnitude faster. (performance)

Both have their strengths and limitations and both are powerful, and then there are metatables, which make Lua as powerful as it is used in the correct way.

Edited by PravusJSB

Creator & Developer of XSAF ::An AI model that wants to kill you, and needs no help from humans.

Discord: PravusJSB#9484   twitch.tv/pravusjsb  https://www.patreon.com/XSAF  https://discord.gg/pC9EBe8vWU https://bmc.link/johnsbeaslu

Work with me on Fiverr: https://www.fiverr.com/pravusjsb

Posted

@PravusJSB

 

Thanks for the short primer.  Not sure that "tables" will ever be easy for me, but I'll keep plugging

 

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

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.

  • Recently Browsing   0 members

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