Matrix Games Forums

Forums  Register  Login  Photo Gallery  Member List  Search  Calendars  FAQ 

My Profile  Inbox  Address Book  My Subscription  My Forums  Log Out

Syntax for an exist or does not exist?

View related threads: (in this forum | in all forums)

Logged in as: Guest
Users viewing this topic: none
  Printable Version
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Syntax for an exist or does not exist? Page: [1]
Message << Older Topic   Newer Topic >>
Syntax for an exist or does not exist? - 7/15/2021 1:27:06 PM   

Posts: 300
Joined: 2/27/2007
Status: offline
apologies, got the whole emulate console thing eventually after much searching and finding soemone elses code who does know what they're doing.....

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)
--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'})
for i=1,8 do ScenEdit_AssignUnitToMission('Marauder #'..i, "Intercept") end

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?



< Message edited by KLAB -- 7/15/2021 7:49:24 PM >
Post #: 1
RE: Syntax for an exist or does not exist? - 7/15/2021 11:44:49 PM   


Posts: 1212
Joined: 11/15/2018
Status: offline
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,}) --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;
  return false; -- if we get this far none exist.  

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.

--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)
  print("At least one existed.");

< Message edited by KnightHawk75 -- 7/16/2021 11:50:30 PM >

(in reply to KLAB)
Post #: 2
RE: Syntax for an exist or does not exist? - 7/16/2021 9:27:27 AM   

Posts: 300
Joined: 2/27/2007
Status: offline
Wow, as ever this is genius stuff , much appreciated.

(in reply to KnightHawk75)
Post #: 3
Page:   [1]
All Forums >> [New Releases from Matrix Games] >> Command: Modern Operations series >> Mods and Scenarios >> Lua Legion >> Syntax for an exist or does not exist? Page: [1]
Jump to:

New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts

Forum Software © ASPPlayground.NET Advanced Edition 2.4.5 ANSI