updating a unit's formation

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
arnhrwd
Posts: 5
Joined: Fri Mar 18, 2022 8:42 am

updating a unit's formation

Post by arnhrwd »

Is it possible to update a unit's formation via Lua? I've tried something like below (plus a lot of other things, including SetUnit rather than UpdateUnit) and the formation details never change, the formation type e.g. remains as 'Rotating':

Code: Select all

ScenEdit_UpdateUnit({guid=unit.guid,formation={type='Fixed'}})
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: updating a unit's formation

Post by KnightHawk75 »

unitwrapper.formation tables. make a copy, modify, assign back.

a little local test that creates 3 ships and sets their rotating formation to specific placement 5nm apart, the non leads will e at relative 135 and 225 degrees from lead.

Code: Select all

local createdUnits={}; --tmp store created units.
local createdGroupName = "TestGroup";
local createdGroup; --tmp store group object.

local function setupUnits(sidename)
  for i=1,3 do -- create some units
    createdUnits[i] = ScenEdit_AddUnit({type="Ship",side=sidename,name="TestUnit #"..i, dbid="2718",latitude=28.0,longitude=-122.0})
    createdUnits[i].heading = 0;  createdUnits[i].group = createdGroupName;
  end
  createdGroup = SE_GetUnit({side=sidename,name=createdGroupName});
end

local function cleanupUnits(sidename)
  local r,g = pcall(SE_GetUnit,{side=sidename,name=createdGroupName});
  if r and g~=nil then ScenEdit_DeleteUnit({side=sidename, unitname=createdGroupName},true);end
end

local function setupFormation(cunits,cgrp)
  local l
  if #cunits < 2 then print("nothing to do 1 or less creted units."); return; end 
  if ((cgrp ~=nil) and cgrp.group ~=nil) then
    l = cunits[1];  cgrp.group.latitude = l.latitude; cgrp.group.longitude = l.longitude; --set grp location to that of lead.
    cgrp.heading = l.heading; --make sure group heading set.
  else
    print("could not obtain group or group lead, aborting.");return;
  end

  for i=2,#cunits do
    local u = cunits[i]; --shortcut for less typing.
    local f = u.formation; --hold our changes.
    if i == 2 then f.bearing=135; f.distance=5.0; --set bearing and distance for location relative to lead 2nd unit.
    elseif i == 3 then f.bearing=225; f.distance=5.0; --set bearing and distance for location relative to lead 3nd unit.
    else f.bearing=180; f.distance = 5.0 +i; --not used in our sample
    end 
    u.latitude = l.latitude; -- force latlon to location of leadunit. cleans up bearing calcs
    u.longitude = l.longitude; --force latlon to location of leadunit. cleans up bearing calcs
    u.formation = f; --the 'set' replaces original with our modified copy.
    --if newly generated lat|lons exist post changes, teleport to that formation location if desired sample
    if u.formation.latitude ~=nil and u.formation.longitude ~=nil then
      u.latitude = u.formation.latitude; -- move unit to formation spot
      u.longitude = u.formation.longitude; -- move unit to formation spot
    end
  end
end

cleanupUnits('a');  -- CHANGE THE SIDENAME
setupUnits('a') -- CHANGE THE SIDENAME
setupFormation(createdUnits,createdGroup)
arnhrwd
Posts: 5
Joined: Fri Mar 18, 2022 8:42 am

Re: updating a unit's formation

Post by arnhrwd »

Thanks for that.
Post Reply

Return to “Lua Legion”