Syntax for an exist or does not exist? (Full Version)

All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion



Message


KLAB -> Syntax for an exist or does not exist? (7/15/2021 1:27:06 PM)

apologies, got the whole emulate console thing eventually after much searching and finding soemone elses code who does know what they're doing.....


Tool_EmulateNoConsole(true)
ScenEdit_AddSide({name='test'})
local i= 1, 8
local u = ScenEdit_GetUnit({side='B', name='Marauder #'..i})
if(u ~= nil) then
--print('u was defined')
ScenEdit_MsgBox ('Marauders are present',0)
else
--print('u not defined')
ScenEdit_MsgBox ('Marauders are being added',0)
for planes = 1, 8, 1 do
ScenEdit_AddUnit({type = 'Air', unitname = 'Marauder #'..planes, loadoutid = 5347, dbid = 1404, side = 'B', base='B AIRBASE'})
end
for i=1,8 do ScenEdit_AssignUnitToMission('Marauder #'..i, "Intercept") end
end
ScenEdit_RemoveSide({name='test'})
Tool_EmulateNoConsole(false)

Ok so I repeatedly failing with this, and pretty incompetent at best so your collective patience and help is much appreciated...

I want a group of fighters to effectively re-spawn at timed intervals but to limit the numbers and only re-spawn if all 8 have been shot down or don't exist to avoid a horde accumulating. (I'm calling them Marauder 1 through 8)

So once all 8 are gone after X hrs they'll reappear.

I am ok with the timer, etc but am failing on the whole LUA for check the OPFOR units 1-8 exist and most importantly what is the code for doing something if they don't exist.

Logic would say its a case of
1. Check the unit exists. YES/NO i.e ScenEdit_GetUnit({Side='B', Name="Marauder #"..num}) ~= nil then
2. If exist do nothing.
3. elseif do XYZ

But as soon as it fails to find the unit "Marauder - 1 to 8" it just fails and wont move on to the next action.

so if
if ScenEdit_GetUnit({Side='B', Name="Marauder #"..num}) ~= nil then -- confirms the unit exists

What is the code for a negative result?


Regards

K






KnightHawk75 -> RE: Syntax for an exist or does not exist? (7/15/2021 11:44:49 PM)

What is the code for a negative result?
Here it would be == nil;

But I'd recommend a pcall on SE_GetUnit and true\false check for success and then unit ~=nil check.

--sample list of units coded or generated on the fly, local var and function is just sample could be global just the same.
local somelist = {
[1]={side="B",name="Marauder #1"},
[2]={side="B",name="Marauder #2"},
[3]={side="B",name="Marauder #3"},
[4]={side="B",name="Marauder #4"},
[5]={side="B",name="Marauder #5"},
[6]={side="B",name="Marauder #6"},
[7]={side="B",name="Marauder #7"},
[8]={side="B",name="Marauder #8"}
}
-- takes in a table with name and side entries like the above^.
-- returns True if any of the units exists, false if none of them do, nil if there is an invalid parameter\bad table fed.
local function AnyUnitsExistByNameAndSide(listOfUnits)
  if (listOfUnits ~=nil) and type(listOfUnits) ~= "table" then print("AnyUnitsExistByNameAndSide(): invalid paramter. aborting"); return; end 
  if (listOfUnits ==nil) then print("AnyUnitsExistByNameAndSide(): missing listofUnits paramter. aborting"); return; end 
  local retval,u;
  for k,v in pairs(listOfUnits) do
    retval,u = pcall(SE_GetUnit,{side=v.side,name=v.name}) --retval==true on successful getunit call, u is not nil when unit wrapper is obtained.
    if(retval ==true) and u~=nil then return true; end -- if any of them are valid we can stop as we have our answer, no need to keep checking. 
    retval,u = false,nil;
  end
  return false; -- if we get this far none exist.  
end

local function createUnitsAtBaseAssignMission(theside,thenameprefix,dbid,loadout,basename,missionname,amount)
  local retval,u; 
  for i = 1, amount do
    retval,u = pcall(ScenEdit_AddUnit,{type = 'Air', unitname = thenameprefix..i, loadoutid = loadout, dbid = dbid, side = theside, base=basename});
    if (retval==true) and u ~= nil then 
      pcall(ScenEdit_AssignUnitToMission,u.guid,missionname); --assumes the mission name actually exists already.
    end
  end
end 

--Sample Usage of  the above
local doTheyAllExist=AnyUnitsExistByNameAndSide(somelist);
if (doTheyAllExist ~=nil) and doTheyAllExist==false then --we check we have valid return first, then if we do if it was false.
  --call to code that recreates them since none exist.
  print("none existed, recreating.");
  createUnitsAtBaseAssignMission("B","Marauder",1404,5347,"B AIRBASE","Intercept",8)
else
  print("At least one existed.");
end





KLAB -> RE: Syntax for an exist or does not exist? (7/16/2021 9:27:27 AM)

Wow, as ever this is genius stuff , much appreciated.
K




Page: [1]

Valid CSS!




Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI
0.7348633