Assign to Mission Escort

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
Gunner98
Posts: 5998
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

Assign to Mission Escort

Post by Gunner98 »

Having an issue setting a Boolean switch to assign some units to escort. I've used the base function dozens of times and it works fine without the escort bit:

for i=29,32 do
if ScenEdit_GetUnit({Side='WP', Name="574th MBAP #"..i,}) ~= nil then
if not ScenEdit_AssignUnitToMission("574th MBAP #"..i, "Bomber Strike", escort=true) then
end
end
end

I played around and tried brackets and quotes, upper case - just haven't hit the suite spot yet.

Help greatly appreciated as always.

Is there a tutorial on calling Boolean returns?

Tx

B
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Assign to Mission Escort

Post by jkgarner »

First, in Lua, strings can be designated either with double quotes marks OR single quote marks...
Most coders in Lua seem to prefer single quotes, but if the string has a tick within it, then you can switch that string to the double quote.
Otherwise, you must escape the mark within the string.
A good tutorial for this is at tutorialspoint dot com (look for lua_strings)

Second, according to the documentation, if the function fails, a message is generated that you can not catch...
more to the point, both the unit AND the mission must exist for the side in order for the function to work.
You test for the existence of the unit on the side before the call, but not the mission.

The following code does both and has print statements in place to help you figure out what is going on.

When you are in your scenario where you want to run the function, pause the game and copy the code into the Lua console to see what is going on.

Code: Select all

 for i=29,32 do
     if ScenEdit_GetUnit({Side='WP', Name='574th MBAP #'..i,}) ~= nil then
         if ScenEdit_GetMission({'WP','Bomber Strike'}) ~= nil then
             if ScenEdit_AssignUnitToMission('574th MBAP #'..i, 'Bomber Strike', escort=true) then
                 print ('succeeded in assigning escort')
             else
                 print ('failed to assign escort')
             end
         else
             print ('Mission: Bomber Stike not defined for side: WP')
         end
     else
         print ('Unit: 574thMBBAP'..i..' not defined for side: WP')
     end
 end
 

Good luck.
User avatar
Gunner98
Posts: 5998
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Assign to Mission Escort

Post by Gunner98 »

Thanks

Both the mission and the unit exist, I'm setting them up for AI missions. I like to check for the unit each time in case the player kills one of them.

Tried the script, getting an error: ERROR: [string "Console"]:4: ')' expected near '='

Pretty sure its a simple syntax thing (it usually if for me) with the escort command. I'll check out the tutorial you mentioned.

Cheers
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Assign to Mission Escort

Post by KnightHawk75 »

@jkgarner btw sometimes you can catch the error if you wrap the call in pcall or xpcall and with xp can throw it to a custom error handling routine, the first parameter fed to that routine will be the error message. xpcall didn't work as well in 5.1, but now that we have 5.3 it works better since one can pass parameters now.
status = xpcall(ScenEdit_RunScript,gKH_apiBase_HandleError,(tostring(v.filepath)));
That said for this, it's overkill.

@Gunner98
Tried the script, getting an error: ERROR: [string "Console"]:4: ')' expected near '='
I think you may need to just change the following in what jkgarner posted.

Code: Select all

ScenEdit_AssignUnitToMission('574th MBAP #'..i, 'Bomber Strike', escort=true)
 to
 ScenEdit_AssignUnitToMission('574th MBAP #'..i, 'Bomber Strike',true)
 
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Assign to Mission Escort

Post by jkgarner »

So, KnightHawk75, your version is running Lua 5.3?
My version is still running Lua 5.2.
(you can tell by looking at the dlls in the directory where the executable is found)

So pcall or xpcall would catch that error? Cool! I'll look into that.
Thanks. That should help on my on-going quest for error-free code.


jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Assign to Mission Escort

Post by jkgarner »

Just a question:
When xpcall calls the error handler, does it return control back to the calling function (just after the point of the call)?
User avatar
Gunner98
Posts: 5998
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Assign to Mission Escort

Post by Gunner98 »

ScenEdit_AssignUnitToMission('574th MBAP #'..i, 'Bomber Strike',true)

Thank you - works the trick.
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Assign to Mission Escort

Post by KnightHawk75 »

ORIGINAL: jkgarner

So, KnightHawk75, your version is running Lua 5.3?
My version is still running Lua 5.2.
(you can tell by looking at the dlls in the directory where the executable is found)

So pcall or xpcall would catch that error? Cool! I'll look into that.
Thanks. That should help on my on-going quest for error-free code.

If running CMO and relatively up to date you should have 5.3. It should tell you that in the lua console when you first open it, and you should have the lua53.dll dated 1/18/2020 in any of the more recent builds put out in 2020.
That said 5.2 I believe includes the improvements to xpcall as well. I don't actually use it or simpler pcall often in CMO, but every once in awhile it comes in handy.

Your question about xpcall returning, yes execution continues on in the example with status getting it's assignment after the custom error handler function runs should there be an error. In fact even if you have errors inside your own error handler it'll eventually break itself out and continue as a safeguard. It self-limits additional errors generated with-in the error handler to like 255 or something like that. Also there are actually 3 values that can be returned from the xpcall, in the sample I just wasn't using the others.

This may help with usage:
https://stackoverflow.com/questions/301 ... s#30125834



jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Assign to Mission Escort

Post by jkgarner »

At my office (yes, I do this stuff for work! [:)] ) We are running last year's version Command PE, which is based on an older version of CMANO, and uses version 5.2 (lua52.dll) They just released a new version, but we are waiting for funding to come through to upgrade. I expect when we get the upgrade that it may be running 5.3.
User avatar
Gunner98
Posts: 5998
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Assign to Mission Escort

Post by Gunner98 »

I do this stuff for work!

Nice! I design scenarios for work, hobby overlap is the way to go.
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Assign to Mission Escort

Post by KnightHawk75 »

ORIGINAL: jkgarner

At my office (yes, I do this stuff for work! [:)] ) We are running last year's version Command PE, which is based on an older version of CMANO, and uses version 5.2 (lua52.dll) They just released a new version, but we are waiting for funding to come through to upgrade. I expect when we get the upgrade that it may be running 5.3.

I wondered that given the mention of 5.2, nice! [:)]
Post Reply

Return to “Lua Legion”