Cookbook: Spawn units, modify sensors, add aircraft, group units

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
Craigkn
Posts: 191
Joined: Thu Jan 27, 2022 12:06 am
Location: Central Maryland

Cookbook: Spawn units, modify sensors, add aircraft, group units

Post by Craigkn »

I wanted to share working (as of build 1638) code that does the following:
  • Spawns several ships
  • Adds towed sonar arrays to several of them
  • Adds aircraft to one of the ships
  • Adds weapons to the magazine of some ships
  • Groups them into a group named "CTG 447.2 Sentinel Reach"
This is to create a modernized joint USN/USCG/HMCS ASW group operating off of the Virginia Capes. ChatGPT and Gemini wrote the unit grouping section, I wanted the unit spawning/modifying to be more simple, so that was human written.

Hope this helps someone. I will post more cookbook type scripts if there is interest


Code: Select all

ScenEdit_AddUnit( {type ='Ship', unitname ='USCGC Hamilton (WMSL-753)', dbid =4824, side ='NATO', latitude='37.1324880632969', longitude='-72.4576423523526',proficiency='Veteran'} )
ScenEdit_AddUnit( {type ='Ship', unitname ='USS Oscar Austin (DDG-79)', dbid =2869, side ='NATO', latitude='36.7483430265893', longitude='-71.8507974160873',proficiency='Veteran'} )
ScenEdit_AddUnit( {type ='Ship', unitname ='USCGC Stone (WMSL-758)', dbid =4824, side ='NATO', latitude='36.4853479005933', longitude='-72.5360475370659',proficiency='Veteran'} )
ScenEdit_AddUnit( {type ='Ship', unitname ="HMCS St. John's (FFH-340)", dbid =3569, side ='NATO', latitude='36.7945336585016', longitude='-72.7710340240817',proficiency='Veteran'} )

ScenEdit_AddUnit({type ='Air', unitname ='HSM-60 #1', loadoutid =3, dbid =5338, side ='NATO', Base='USS Oscar Austin (DDG-79)'})
ScenEdit_AddUnit({type ='Air', unitname ='HSM-60 #2', loadoutid =3, dbid =5338, side ='NATO', Base='USS Oscar Austin (DDG-79)'})
ScenEdit_AddUnit({type ='Air', unitname ='HSM-60 #3', loadoutid =3, dbid =5338, side ='NATO', Base='USCGC Stone (WMSL-758)'})
ScenEdit_AddUnit({type ='Air', unitname ='HSM-60 #4', loadoutid =3, dbid =5338, side ='NATO', Base='USCGC Hamilton (WMSL-753)'})
ScenEdit_AddUnit({type ='Air', unitname ='No. 423 #2', loadoutid =3, dbid =2779, side ='NATO', Base="HMCS St. John's (FFH-340)"})

local u = ScenEdit_GetUnit({ side = 'NATO' , name = "USCGC Stone (WMSL-758)" })
local v = ScenEdit_GetUnit({ side = 'NATO' , name = "USCGC Hamilton (WMSL-753)" })

local arc360 = {'360'}

ScenEdit_UpdateUnit({guid=v.guid, mode='add_sensor', dbid=6099, arc_detect=arc360, arc_track=arc360}) --360 version
ScenEdit_UpdateUnit({guid=u.guid, mode='add_sensor', dbid=6099, arc_detect=arc360, arc_track=arc360}) --360 version

ScenEdit_AddWeaponToUnitMagazine( { unitname='USCGC Stone (WMSL-758)', wpn_dbid=202, number=2, maxcap=2 } )
ScenEdit_AddWeaponToUnitMagazine( { unitname='USCGC Stone (WMSL-758)', wpn_dbid=902, number=6, maxcap=6 } )
ScenEdit_AddWeaponToUnitMagazine( { unitname='USCGC Hamilton (WMSL-753)', wpn_dbid=202, number=2, maxcap=2 } )
ScenEdit_AddWeaponToUnitMagazine( { unitname='USCGC Hamilton (WMSL-753)', wpn_dbid=902, number=6, maxcap=6 } )

--[[
  Lua Script: Example of Grouping Units in Command: Modern Operations

  This script demonstrates how to assign multiple units to a new or existing group
  and optionally set a group leader.

  ASSUMPTIONS:
  1. The units you want to group (e.g., "USS Arleigh Burke", "USS Ticonderoga", "USS Supply")
     already exist in the scenario on the specified side.
  2. ScenEdit_SetUnit() and ScenEdit_GetUnit() are available C:MO Lua functions.
]]

--------------------------------------------------------------------------------
-- MAIN SCRIPT LOGIC: Grouping Units
--------------------------------------------------------------------------------

-- Define the side the units belong to
local sideName = "NATO"

-- Define the names of the units to be grouped
local unitNamesToGroup = {
  "USS Oscar Austin (DDG-79)",   -- This will be the group leader
  "USCGC Hamilton (WMSL-753)",
  "USCGC Stone (WMSL-758)",
  "HMCS St. John's (FFH-340)"
}

-- Define the desired group name
local newGroupName = "CTG 447.2 Sentinel Reach"
local groupLeaderName = "USS Oscar Austin (DDG-79)"


for _, unitName in ipairs(unitNamesToGroup) do
  local unitToGroup = ScenEdit_GetUnit({side = sideName, unitname = unitName})
  if unitToGroup then
    local success, errorMsg = ScenEdit_SetUnit({
      guid = unitToGroup.guid,
      group = newGroupName
    })
    if not success then
      -- Error handling: ScenEdit_SetUnit might return false and an error message
      -- For this example, we'll just note it. In a real scenario, you might log more.
      -- Note: No print statements as per user preference.
      -- Consider adding a ScenEdit_SpecialMessage for errors if critical.
    end
  else
    -- Unit not found, handle error or log
    -- ScenEdit_SpecialMessage(sideName, "ERROR: Unit '" .. unitName .. "' not found for grouping.")
  end
end

--------------------------------------------------------------------------------
-- Step 2: Set the group leader
-- This step is crucial. After units are in a group, one must be designated leader.
-- You can set the group leader by targeting the unit that should be the leader.
--------------------------------------------------------------------------------
local leaderUnit = ScenEdit_GetUnit({side = sideName, unitname = groupLeaderName})
if leaderUnit then
  -- Method 1: Using the 'groupLead' parameter (often preferred for clarity)
  -- This assumes the leaderUnit is already part of the 'newGroupName' from Step 1.
  local successLead, errorMsgLead = ScenEdit_SetUnit({
    guid = leaderUnit.guid,
    groupLead = true -- Designate this unit as the leader of its current group
    -- Alternatively, you can set groupLead to the GUID of the leader unit if operating on another unit,
    -- but setting it to 'true' for the leader itself is common.
    -- Or, more explicitly: groupLead = leaderUnit.guid
  })

  --[[
  -- Method 2: Setting the 'group' property with leader info (more complex for just setting lead)
  -- This method is more powerful if you are creating the group and setting the lead simultaneously
  -- for the leader unit. If units are already in a group, Method 1 is usually simpler.
  ScenEdit_SetUnit({
    guid = leaderUnit.guid,
    group = {name = newGroupName, lead = true}
  })
  ]]

  if not successLead then
    -- ScenEdit_SpecialMessage(sideName, "ERROR: Failed to set group leader for '" .. groupLeaderName .. "'. " .. (errorMsgLead or ""))
  end
else
  -- ScenEdit_SpecialMessage(sideName, "ERROR: Designated group leader '" .. groupLeaderName .. "' not found.")
end

-- At this point, the specified units should be in the group "CTG 447.2 Escorts"
-- with "USS Oscar Austin (DDG-79)" as the leader.

-- You could send a confirmation message if desired:
-- if ScenEdit_SpecialMessage then
--   ScenEdit_SpecialMessage(sideName, "Units have been assigned to group: " .. newGroupName .. " with leader: " .. groupLeaderName)
-- end

-- Script execution completes.

Post Reply

Return to “Lua Legion”