Page 1 of 1

Unit change sides

Posted: Sat Sep 04, 2021 10:00 am
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

RE: Unit change sides

Posted: Sat Sep 04, 2021 10:50 am
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

RE: Unit change sides

Posted: Sat Sep 04, 2021 10:52 am
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

RE: Unit change sides

Posted: Sat Sep 04, 2021 5:13 pm
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

RE: Unit change sides

Posted: Sat Sep 04, 2021 5:23 pm
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

RE: Unit change sides

Posted: Sat Sep 04, 2021 6:44 pm
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.

RE: Unit change sides

Posted: Sun Sep 05, 2021 1:27 pm
by Gunner98
ahhh, no. I didn't do that. Thanks for the tip

B

RE: Unit change sides

Posted: Sun Sep 05, 2021 5:44 pm
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