Unit change sides

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

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

Unit change sides

Post by Gunner98 »

I've used this script quite often and at fist blush it works fine:

for i=1,8 do
if ScenEdit_GetUnit({Side='SE Asia Comd', Name="VF-102 Diamondbacks #"..i,}) ~= nil then
ScenEdit_SetUnitSide({Side='SE Asia Comd', Name="VF-102 Diamondbacks #"..i, newside='7th Fleet'})
end
end

Decided to do a quick test and removed one of the units, I thought that the first line of the statement was supposed to look for missing units and if it finds a gap it would just skip over it. Nots so

Removing a unit caused the script to fail. Any thoughts.


Image
Attachments
Changesides.jpg
Changesides.jpg (100.59 KiB) Viewed 245 times
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/
User avatar
stilesw
Posts: 1572
Joined: Wed Jun 25, 2014 10:08 pm
Location: Hansville, WA, USA

RE: Unit change sides

Post by stilesw »

Hi Bart. This Lua code has worked fine for me in all CMANO/CMO versions recently as this week.

i = 1;
while i < 19 do
if ScenEdit_GetUnit({Side='PRC', Name="J-8F Finback B (Woody) #"..i}) ~= nil then
ScenEdit_SetUnitSide({side="PRC", name="J-8F Finback B (Woody) #"..i, newside="USN"});
end
i = i + 1;
end

-Wayne
“There is no limit to what a man can do so long as he does not care a straw who gets the credit for it.”

Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
User avatar
stilesw
Posts: 1572
Joined: Wed Jun 25, 2014 10:08 pm
Location: Hansville, WA, USA

RE: Unit change sides

Post by stilesw »

A little more readable:

i = 1;
while i < 19 do
____if ScenEdit_GetUnit({Side='PRC', Name="J-8F Finback B (Woody) #"..i}) ~= nil then
________ScenEdit_SetUnitSide({side="PRC", name="J-8F Finback B (Woody) #"..i, newside="USN"});
____end
____i = i + 1;
end
“There is no limit to what a man can do so long as he does not care a straw who gets the credit for it.”

Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
User avatar
Gunner98
Posts: 5966
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Unit change sides

Post by Gunner98 »

Thanks Wayne

I'll give it a try. So this cycles through up to 18, then does the same check as the script I was using.

Tx
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/
User avatar
stilesw
Posts: 1572
Joined: Wed Jun 25, 2014 10:08 pm
Location: Hansville, WA, USA

RE: Unit change sides

Post by stilesw »

The only difference I immediately see is that your code uses the "for...do" construct vs the "while <".
In theory they should both accomplish the same result but do not for some reason. Go figure.

-Wayne
“There is no limit to what a man can do so long as he does not care a straw who gets the credit for it.”

Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
User avatar
michaelm75au
Posts: 12457
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Unit change sides

Post by michaelm75au »

I assume you ran 'Tool_EmulateNoConsole(true)' in the console to allow the script to run non-interactive?
Otherwise it will stop on the error to show up the message.
Michael
User avatar
Gunner98
Posts: 5966
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Unit change sides

Post by Gunner98 »

ahhh, no. I didn't do that. Thanks for the tip

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/
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Unit change sides

Post by KnightHawk75 »

interactive ...issue

just wrap it in pcall to surpess the interactive error, on error retval will be false.

Code: Select all

 for i=1,8 do
   local retval,unit = pcall(ScenEdit_GetUnit,{Side='SE Asia Comd', Name="VF-102 Diamondbacks #"..i,}) ;
   if retval and unit ~=nil then 
     ScenEdit_SetUnitSide({Side='SE Asia Comd', Name="VF-102 Diamondbacks #"..i, newside='7th Fleet'});
   end 
 end
 
Post Reply

Return to “Lua Legion”