Lua help change mission / loadout/ base

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
Kennetho
Posts: 65
Joined: Sat Mar 24, 2018 7:23 am
Location: Denmark

Lua help change mission / loadout/ base

Post by Kennetho »

Hi guys,

I am new to building scenarios with lua.

I’ve build a huge 24 hours scenario NATO vs Russia in baltics. Build on top of the “No Brexit” live scen.
As it is 24 hours, some units needs to change mission and loadout from ground strike to AAW patrol.

Also some Russian squadrons home base are deep inside Russia and should change home base to land in Belarus.

Can you guys help with these luas?
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Lua help change mission / loadout/ base

Post by KnightHawk75 »

ScenEdit_HostUnitToParent will help you move units to a new base, from their current base.
http://commandlua.github.io/#ScenEdit_HostUnitToParent

ScenEdit_AssignUnitToMission will help you reassign units to new missions, or you can just set the .mission property if you already have the unit object.
http://commandlua.github.io/#ScenEdit_A ... tToMission

ScenEdit_SetLoadout will help you change the loadout or loadout related properties.
http://commandlua.github.io/#ScenEdit_SetLoadout

Sample function to re-base everyone at one base to another no matter if they are currently at home-base or not:

Code: Select all

local function RelocateAllHostedUnitsToNewHost(sourcetbl,destinationtbl)
   local retval, alreadymoved, source,dest = false,{},nil,nil;
   retval,source = pcall(ScenEdit_GetUnit,sourcetbl);
   if (retval == false) or source == nil then print('invalid sourcetbl. param'); return false; end
   retval,dest = pcall(ScenEdit_GetUnit,destinationtbl);
   if (retval == false) or dest == nil then print('invalid destinationtbl. param'); return false; end
 
   --relocate the embarked units.
   for _,guid in ipairs(source.embarkedUnits.Aircraft) do
     ScenEdit_HostUnitToParent({HostedUnitNameOrID=guid,SelectedHostNameOrID=dest.guid});
     alreadymoved[guid]=true;
   end
   for _,guid in ipairs(source.embarkedUnits.Boats) do
     ScenEdit_HostUnitToParent({HostedUnitNameOrID=guid,SelectedHostNameOrID=dest.guid});
     alreadymoved[guid]=true;
   end
   
   --now assign units that are assigned to the base but not there (in the air etc), the new base without 'moving' them.
   for _,guid in ipairs(source.assignedUnits.Aircraft) do
     if alreadymoved[guid] == nil then
       ScenEdit_SetUnit({guid=guid,base=dest.guid});
       alreadymoved[guid]=true;
     end
   end
   
   for _,guid in ipairs(source.assignedUnits.Boats) do
     if alreadymoved[guid] == nil then
       ScenEdit_SetUnit({guid=guid,base=dest.guid});
       alreadymoved[guid]=true;
     end
   end
   return true;
 end
 RelocateAllHostedUnitsToNewHost({side="Blue",name="SUA__1"},{side="Blue",name="SUA__2"});
 
If you have specific questions about AssignUnitToMission or SetLoadout just ask.
Last edited by KnightHawk75 on Tue Mar 01, 2022 10:20 am, edited 1 time in total.
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: Lua help change mission / loadout/ base

Post by Parel803 »

Goodevening,
Always nice to learn for the forum. a question on the first part, I added the source and dest AB as local.
Runned but got an error on the alreadymoved. When I delete that line mt 3 A/C switch to other AB.
When I try to reverse (with alreadymoved in it) it transfered 1 on reported the error:
ERROR: [string "Console"]:14: attempt to index a nil value (global 'alreadymoved')

I added one end for the local funtion.
Just wondering the error, not important.

with regards GJ
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

Re: Lua help change mission / loadout/ base

Post by KnightHawk75 »

Parel803 wrote: Sun Feb 27, 2022 7:23 pm added the source and dest AB as local.

I don't follow\understand? why would you need to, they are already parameters?
When I delete that line
I don't follow why would you delete that line? It's critical to have the function work as designed as it keeps track of what's been moved already and what hasn't.
When I try to reverse (with alreadymoved in it) it transfered 1 on reported the error:
ERROR: [string "Console"]:14: attempt to index a nil value (global 'alreadymoved')
Because you removed a key line of code it sounds like.
I added one end for the local funtion.
shouldn't need to touch anything.
Last edited by KnightHawk75 on Wed Mar 02, 2022 3:44 am, edited 1 time in total.
Parel803
Posts: 941
Joined: Thu Oct 10, 2019 3:39 pm
Location: Netherlands

Re: Lua help change mission / loadout/ base

Post by Parel803 »

Thx for your time I'll try again with your directives.
best regards GJ

And I see my stupidity in the way I read it, sorry for any inconvinience
Post Reply

Return to “Lua Legion”