Page 1 of 1

Unit naming

Posted: Sat Aug 14, 2021 10:15 am
by Gunner98
Setting up some random cargo ships to pop up on a non player side. I know that someone made a script for this a while back but for the life of me I cannot find it.

Anyway, this isn't that elaborate. The ships pop up on an event (%chance every XX hrs) the ships are one of three types and each type is assigned to a consistent mission. No problem and the script here works fine to a point:

-----Random cargo ships

a = math.random(1,3)

if a == 1 then
ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Container', dbid='2028', latitude='9.70632518352066', longitude='-79.9607376175008'})
ScenEdit_AssignUnitToMission('Panamax Container', 'Anegada Passage Assy')
elseif a == 2 then
ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Cargo', dbid='2774', latitude='9.70632518352066', longitude='-79.9607376175008'})
ScenEdit_AssignUnitToMission('Panamax Cargo', 'Mona Passage Assy')
elseif a == 3 then
ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Tanker', dbid='339', latitude='9.70632518352066', longitude='-79.9607376175008'})
ScenEdit_AssignUnitToMission('Panamax Tanker', 'Guadeloupe Passage Assy')
end

The problem of course is that the names are not unique, so the moment a second ship of the same type shows up it doesn't get assigned to the mission and just sits there. So I know there is a simple way of either adding something to the name string or using UnitX() -- but I fiddled with it for a while and decided to raise the white flag.

Any thoughts guys?

RE: Unit naming

Posted: Sat Aug 14, 2021 2:32 pm
by KnightHawk75
I mean if it doesn't need to be pretty and you don't want to pull from a long list of names that don't get re-used etc..

You can just use the guid.

Code: Select all

 local a = math.random(1,3);
 local u;
 
 if a == 1 then
   u = ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Container', dbid='2028', latitude='9.70632518352066', longitude='-79.9607376175008'});
   ScenEdit_AssignUnitToMission(u.guid, 'Anegada Passage Assy');
 elseif a == 2 then
   u = ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Cargo', dbid='2774', latitude='9.70632518352066', longitude='-79.9607376175008'});
   ScenEdit_AssignUnitToMission(u.guid, 'Mona Passage Assy');
 elseif a == 3 then
   u = ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Tanker', dbid='339', latitude='9.70632518352066', longitude='-79.9607376175008'});
   ScenEdit_AssignUnitToMission(u.guid, 'Guadeloupe Passage Assy');
 end 

If however you actually do want to generate pretty unique names then throw on a random suffix.
I'd actually recommend this anyway just to avoid confusion in troubleshooting things unless it impacts the scene\story etc.
Throw a gKH={} at the top of your global startup script... or just change the name.

Code: Select all

 --5.1 generate basic 8-9char suffix for names.
 --returns string such as (AB12345CD). 
 function gKH:GetRandomSuffix()
   local a = math.random(65,90); --generate random number from 65-90  {caps}
   local b = math.random(97,122); --generate random number from 97-122 {lower}
   local c = math.random(1000,99999)--generate random number from 1000-99999
   local d = math.random(65,90);--generate random number from 65-90  {caps}
   local e = math.random(97,122); --generate random number from 97-122 {lower}
   return '(' .. string.char(a,b) .. tostring(c) .. string.char(d,e) .. ')' 
 end
 --Then adapting your existing code be it in a function or just in a raw action for the event involved:
 local a = math.random(1,3);
 local u;
 if a == 1 then
   u = ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Container' ..gKH:GetRandomSuffix(),
       dbid='2028', latitude='9.70632518352066', longitude='-79.9607376175008'});
   ScenEdit_AssignUnitToMission(u.guid, 'Anegada Passage Assy');
 elseif a == 2 then
   u = ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Cargo'.. gKH:GetRandomSuffix(),
       dbid='2774', latitude='9.70632518352066', longitude='-79.9607376175008'});
   ScenEdit_AssignUnitToMission(u.guid, 'Mona Passage Assy');
 elseif a == 3 then
   u = ScenEdit_AddUnit({type='ship', side='NATO Convoy Cmd', name='Panamax Tanker'..gKH:GetRandomSuffix(),
       dbid='339', latitude='9.70632518352066', longitude='-79.9607376175008'});
   ScenEdit_AssignUnitToMission(u.guid, 'Guadeloupe Passage Assy');
 end 
 

RE: Unit naming

Posted: Sat Aug 14, 2021 7:19 pm
by Gunner98
Thanks KnightHawk

Both solutions work like a charm. I don't have a global startup script so just removed the 'gKH' from the function and the AddUnit script

That's what I'll use.

Cheers

RE: Unit naming

Posted: Sun Aug 15, 2021 1:04 pm
by KnightHawk75
ORIGINAL: Gunner98

Thanks KnightHawk

Both solutions work like a charm. I don't have a global startup script so just removed the 'gKH' from the function and the AddUnit script

That's what I'll use.

Cheers

Actually if it's all just going to sit inside the event "action" then sure. But then "function" technically should be 'local function' though.
ie:
local function GetRandomSuffix()
...
end
..add code that references the above here...