Jump to content

Cycle A Flag From 1 To n At s seconds


Wrecking Crew

Recommended Posts

I want to control artillery fire where arty groups have multiple Fire At Points. A flag's value needs to control which point the group will direct their fire to.

 

To do this I'd like a universal script.

A flag needs to increment from 1 to n, at interval of s seconds, and recycle if desired.

 

Here is my first crude attempt to write a script to increment Flag 30. I borrowed heavily from St3v3f's earlier help for flags in spherical zones. I don't know the syntax for a For Loop so I hard-coded it for a Flag = 3 max value. I think I have the variables needed but this script throws an error in the mission (attached).

 

I think I'm missing an Else statement for the max value, but I'd rather do a For Loop to the max value variable.

 

 

-- script to increment a flag from 1 to n at interval intSecs (seconds)

local uFlag = 30 -- flag number, 1-99999
local uFlagMax = 3 -- flag value maximum
local intSecs = 10 -- interval in seconds
local blnCycle = true -- cycle back to 1

local function incrUserFlagFrom1To_n(result)
   local flagVal = trigger.misc.getUserFlag(uFlag)
   if #flagVal == 0 then
       trigger.action.setUserFlag(uFlag, 1)
   elseif #flagVal == 1 then
       trigger.action.setUserFlag(uFlag, 2)
   elseif #flagVal == 2 then
       trigger.action.setUserFlag(uFlag, 3)
   elseif #flagVal == 3 then
       if blnCycle == 0 then
           end function -- is this the correct command to exit?  uFlag should be = 3
       else
           trigger.action.setUserFlag(flag, 1)
       end
   end
   mist.scheduleFunction incrUserFlagFrom1To_n, {result}, timer.getTime() + intSecs)
end

incrUserFlagFrom1To_n(1)

-- end

 

^^^ Really, I have no idea how close or far this is to working. Help! :-)

 

 

WC


Edited by Wrecking Crew
uFlag

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

I'll look at this in detail in the next few days...

 

I need to find references to using LUA For Loops and how to exit a function -- hope to find it in the wiki.

 

WC

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

This does pretty much exactly what you have typed above but is a bit more concise. Uses a for loop, at the end of the loop if binCycle is set to true and the counter is at the end of the user flags it re-sets the counter to 1 to start over again.

 

Not 100% sure that this is the absolute correct syntax, and I don't know what the rest of your code looks like, but this should help you out a bit. You declare things like uFlag and uFlagMax (don't know how the uFlag can be 30 if the uFlagMax is 3, but could just be confusing variable naming).

 

-- script to increment a flag from 1 to n at interval intSecs (seconds)

local uFlag = 30 -- flag number, 1-99999
local uFlagMax = 3 -- flag value maximum
local intSecs = 10 -- interval in seconds
local blnCycle = true -- cycle back to 1

local function incrUserFlagFromOneToN(uFlag, uFlagMax, intSecs, binCycle)
   for i = 1, uFlagMax do
       trigger.action.setUserFlag(uFlag, i)

       if(i == uFlagMax and binCycle == true)
           i = 1
       end
   end
   
   mist.scheduleFunction(incrUserFlagFromOneToN, {uFlag, uFlagMax, intSecs, binCycle}, timer.getTime() + intSecs)
end

incrUserFlagFromOneToN(uFlag, uFlagMax, intSecs, binCycle)

  • Like 1

Robert Sogomonian | Psyrixx

website| e-mail | blog | youtube | twitter

Link to comment
Share on other sites

Thank you, Psyrixx,

 

Yes, this is what I am looking to achieve. It looks like everything is in the code you provided.

 

Why are {} these used instead of () in the following line?

mist.scheduleFunction(incrUserFlagFromOneToN, {uFlag, uFlagMax, intSecs, binCycle}, timer.getTime() + intSecs)

 

 

uFlag could be renamed to uFlagNum -- it is the flag number.

 

uFlagMax could be renamed to uFlagMaxVal. I will make these description changes for my testing.

 

 

 

 

WC

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

Why are {} these used instead of () in the following line?

mist.scheduleFunction(incrUserFlagFromOneToN, {uFlag, uFlagMax, intSecs, binCycle}, timer.getTime() + intSecs)

 

Because the prototype for scheduleFunction accepts a table of variables to pass as arguments into the function.

 

See http://wiki.hoggit.us/view/ScheduleFunction.

314-я смешанная авиационная дивизия

314th Mixed Aviation Division: The "Fighting Lemmings"- Forums: http://314thsquadron.enjin.com/ - ED Forum Group: http://forums.eagle.ru/group.php?groupid=119

Link to comment
Share on other sites

Thanks for the link ^^^

 

That () vs {} stuff throws me. I'm learning Python at work and think I have a good understanding of the bracket differences in Python, but LUA usage seems tricky.

 

The 'local function' line above is using () with the same variables. :joystick:

 

WC

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

I hear ya, it took me quite some time to come to grips with the syntax basics. I too use a different language (PHP) at work.

 

But the end result of making a DCS mission is rewarding of all the pain.

 

Enjoy!

314-я смешанная авиационная дивизия

314th Mixed Aviation Division: The "Fighting Lemmings"- Forums: http://314thsquadron.enjin.com/ - ED Forum Group: http://forums.eagle.ru/group.php?groupid=119

Link to comment
Share on other sites

After some experimenting, it turned out that my idea for a For loop wasn't the answer, so I have this working --

 

local uFlagNum = 30 -- flag number
local uFlagMaxVal = 3 -- the maximum value, n
local intSecs = 30 -- cycle time in seconds
local blnCycle = true -- cycle the flag back to 1 if true

local function incrUserFlagFromOneToN(uFlagNum, uFlagMaxVal, intSecs, blnCycle) 
if trigger.misc.getUserFlag(uFlagNum) == 0 then
	trigger.action.setUserFlag(uFlagNum, 1)
elseif trigger.misc.getUserFlag(uFlagNum) < uFlagMaxVal then
	trigger.action.setUserFlag(uFlagNum, (trigger.misc.getUserFlag(uFlagNum) + 1))
elseif (trigger.misc.getUserFlag(uFlagNum) == uFlagMaxVal and blnCycle == true) then
       trigger.action.setUserFlag(uFlagNum, 1)
   end 
    
   mist.scheduleFunction(incrUserFlagFromOneToN, {uFlagNum, uFlagMaxVal, intSecs, blnCycle}, timer.getTime() + intSecs) 
end 

incrUserFlagFromOneToN(uFlagNum, uFlagMaxVal, intSecs, blnCycle)

 

 

Let me know if you see any way to reduce this code -- I tried a couple of things but this works so I'm going with it for now.

 

Thanks for all the help!

 

WC

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

You don't want to have mist.scheduledFunction call a function inside of the function it originates from.

 

Stepping through you code, the first call to incrUserFlagFromOneToN will schedule incrUserFlagFromOneToN to be called every second (or whatever the default interval is) because you aren't passing a "number rep" argument.

 

One second goes by and incrUserFlagFromOneToN is called again, but mist.scheduledFunction is called a second time. So now you have two scheduled functions running in your script.

 

Two seconds goes by and incrUserFlagFromOneToN is called again, but you have another mist.scheduledFunction called a 3rd time. So now you have three scheduled functions calling the same function.

 

It will go on and on. In the end your values will increment like crazy and it's not what it seems you want.

 

Also, you have intSecs -- cycle time in seconds, but you have mist.scheduleFunction(incrUserFlagFromOneToN, {...}, timer.getTime() + intSecs), which begin the initial call to the function intSecs from the current time. It will not call the function every intSecs

 

mist.scheduleFunction(incrUserFlagFromOneToN, {...}, <time to start calling the function>,  <how many seconds to wait before calling the function again>, <how many seconds after the start time to stop calling the function>)

 

 

This should work for you

local uFlagNum = 30 -- flag number 
local uFlagMaxVal = 3 -- the maximum value, n 
local intSecs = 30 -- cycle time in seconds 
local blnCycle = true -- cycle the flag back to 1 if true 

local function incrUserFlagFromOneToN(uFlagNum, uFlagMaxVal, blnCycle)
   local flag_value = trigger.misc.getUserFlag(uFlagNum)

   flag_value = flag_value + 1
   
   if (flag_value == uFlagMaxVal and bInCycel == true) then
       flag_value = 1
   end

   trigger.action.setUserFlag(uFlagNum, flag_value)

end 

mist.scheduleFunction(incrUserFlagFromOneToN, {uFlagNum, uFlagMaxVal, blnCycle}, timer.getTime(), intSecs)  

314-я смешанная авиационная дивизия

314th Mixed Aviation Division: The "Fighting Lemmings"- Forums: http://314thsquadron.enjin.com/ - ED Forum Group: http://forums.eagle.ru/group.php?groupid=119

Link to comment
Share on other sites

Thanks for the link ^^^

 

That () vs {} stuff throws me. I'm learning Python at work and think I have a good understanding of the bracket differences in Python, but LUA usage seems tricky.

 

The 'local function' line above is using () with the same variables. :joystick:

 

WC

 

{} defines a table.

 

() is used for two purposes

 

1. Part of a function call to define input variables used. Depending on the function called it can be blank.

2. Order of operations. eg. 7 * (4 + 3)

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

Link to comment
Share on other sites

Is this valid --

flag_value =+ 1 -- (or flag_value += 1)?

 

When I tested my code yesterday, if this last line was missing then the Flag 30 value never got off of zero --

incrUserFlagFromOneToN(uFlagNum, uFlagMaxVal, intSecs, blnCycle)

 

A few more hours left before Holiday! I'll try this latest suggestion later today. Thanks, number3.

 

number3 -- re your comments on the placement of the mist.schedule function inside the main function -- originally, I saw this in code St3v3f provided in this thread -

http://forums.eagle.ru/showthread.php?t=109684

 

I'll have to study your comments to get my head around this. When I tested my code it all did work, but the timing from 1 to 2 to 3 etc. seemed to be off from the seconds interval I'd used.

 

WC

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

Is this valid --

flag_value =+ 1 -- (or flag_value += 1)?

 

I'm not certain as I've never tried it. When I started with lua I steered clear of it because of my lack of knowledge of the syntax.

 

number3 -- re your comments on the placement of the mist.schedule function inside the main function -- originally, I saw this in code St3v3f provided in this thread -

http://forums.eagle.ru/showthread.php?t=109684

 

Interesting... I could be wrong. The way I understand mist.scheduleFunction in 3.2 is that it adds the function call to a Tasks table and increments the task id. So repetitive calls, to the same function, to scheduleFunction will add multiple tasks to call the same function. I don't see any code in that function that checks if the Task table already has a duplicate function name scheduled.

314-я смешанная авиационная дивизия

314th Mixed Aviation Division: The "Fighting Lemmings"- Forums: http://314thsquadron.enjin.com/ - ED Forum Group: http://forums.eagle.ru/group.php?groupid=119

Link to comment
Share on other sites

OK, got it working.

 

This --> flag =+ 1 doesn't seem to be supported in documentation so I didn't try it.

http://www.lua.org/manual/5.0/manual.html

 

That last line --

incrUserFlagFromOneToN(uFlagNum, uFlagMaxVal, blnCycle)

isn't necessary. In fact, when I used it the first value I was seeing was 2 instead of 1 (or 0).

The line may be necessary in the code that St3v3f provided in that other thread, to initialize the 'return' value from the function.

 

The mist.schedule.. is now located outside of the local function scope. I see why... I have that coded inside the local function in other missions -- need to find those.

 

---

 

Here is the latest code. This version accounts for when blnCycle is False.

 

local uFlagNum = 30 -- flag number 
local uFlagMaxVal = 3 -- the maximum value, n 
local intSecs = 10 -- cycle time in seconds 
local blnCycle = true -- cycle the flag back to 1 if true 

local function incrUserFlagFromOneToN(uFlagNum, uFlagMaxVal, blnCycle)
   local flag_value = trigger.misc.getUserFlag(uFlagNum)
   
   if (flag_value == uFlagMaxVal) then
       if (blnCycle) then
		flag_value = 1
	end
else
	flag_value = flag_value + 1
end

   trigger.action.setUserFlag(uFlagNum, flag_value)
end 

mist.scheduleFunction(incrUserFlagFromOneToN, {uFlagNum, uFlagMaxVal, blnCycle}, timer.getTime(), intSecs)

 

WC

Visit the Hollo Pointe DCS World server -- an open server with a variety of COOP & H2H missions including Combined Arms. All released missions are available for free download, modification and public hosting, from my Wrecking Crew Projects site.

Link to comment
Share on other sites

  • Recently Browsing   0 members

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