Page 1 of 1

How to Change a Unit's Side in Lua?

Posted: Fri Dec 09, 2016 8:49 pm
by ProdigyofMilitaryPride
What's the kind of table I need to pull it off successfully? A step-by-step example could help, please.

RE: How to Change a Unit's Side in Lua?

Posted: Fri Dec 09, 2016 9:51 pm
by stilesw
Here is an example of Lua scripting that changes an air base and its associated aircraft (13 F-16C and 13 Mirage F.1EQ-5) from one side to another. The nil check is there to account for any aircraft that have already been destroyed.

ScenEdit_SetUnitSide({side="Iraq", name="Al Asad AB", newside="Israel"})
--
i = 1;
while i < 13 do
if ScenEdit_GetUnit({Side='Iraq', Name="F-16C #"..i}) ~= nil then
ScenEdit_SetUnitSide({side="Iraq", name="F-16C #"..i, newside="Israel"});
end
i = i + 1;
end
--
i = 1;
while i < 13 do
if ScenEdit_GetUnit({Side='Iraq', Name="Mirage F.1EQ-5 #"..i}) ~= nil then
ScenEdit_SetUnitSide({side="Iraq", name="Mirage F.1EQ-5 #"..i, newside="Israel"});
end
i = i + 1;
end

Hope this helps.

-Wayne

RE: How to Change a Unit's Side in Lua?

Posted: Thu Oct 25, 2018 10:49 pm
by Whicker
is there a way to merge 2 sides?
I have a side that is friendly for narrative purposes but at a certain point I would like to give the player full control of it and merge everything on that side to the players side. There might be too many units to loop thru like this.

RE: How to Change a Unit's Side in Lua?

Posted: Fri Oct 26, 2018 6:57 am
by michaelm75au
Not really. Each side has an unique code (GUID) and only one of these codes is controlled by the player at a time.

RE: How to Change a Unit's Side in Lua?

Posted: Fri Oct 26, 2018 6:57 am
by michaelm75au
Not sure about the practicality of creating a function to do this bulk change.

RE: How to Change a Unit's Side in Lua?

Posted: Sat Oct 27, 2018 2:33 am
by Whicker
Challenge accepted.

I think I did it, a little complicated as some units are in groups and when you change one or the others side it seems to error. So I made 2 functions, one that goes thru the sides units and looks for groups and changes those, then when that is complete another function runs and does a new check for units on the old side and switches those. It uses up a fair amount of ram, 150 MB to change a few hundred units. When I did it over and over again testing it, it eventually locked up.

It seemed to work well when ran just once, and I have noticed other times that the game uses up too much memory and needs to be restarted so maybe it is no big deal. If it is just a few units to switch probably harmless.

I iterated thru the units of the side and pushed them into a new table, and then iterated over that to actually change the side, when I didn't do that I usually got errors.

That was a good learning experience for me, first time I actually made a blank array/table and pushed stuff into it and then used that somewhere else. Writing code for a game you enjoy is such a great way to learn, and lua is so user friendly.

local oldside = "Republic of Azure" --set name of old side
local newside = "test" --set name of new side, must exist

function changesSidesGroups(newSide, oldSide) --function number one
local a = VP_GetSide({Side =oldSide}) -- get all units from old side
local groups = {} --create empty array(table) to store group guids
for i = 1, #a.units do -- loops thru all units on the side
local u = ScenEdit_GetUnit({guid = a.units.guid}) --convert VP guid to Unit wrapper
if u.type == 'Group' then -- check if group, if so add the group guid to the table
table.insert(groups, u.guid)
print(u.name.. " side: "..u.side ) --testing by printing to console
end --closes if statement
end --closes loop
for i = 1, #groups do --loop thru new table of group guids
print(groups) --testing by printing to console
ScenEdit_SetUnitSide({side=oldSide, guid=groups, newside=newSide}) --change side from old to new
end --closes loop
changeSidesUnits(newSide, oldSide) -- call second function to change units that are not in groups
end --closes function

function changeSidesUnits(newSide,oldSide) --function number two for non grouped units
local a2 = VP_GetSide({Side =oldSide}) -- get all units from old side - won't include groups as they are already done
local units = {} --create another empty table
for i = 1, #a2.units do --loop thru units and push them into the table
table.insert(units, a2.units.guid)
end --closes loop

for i = 1, #units do --loop thru table of units
print(units) --testing by printing to console
ScenEdit_SetUnitSide({side=oldSide, guid=units, newside=newSide}) --change side
end --closes loop
end -- closes second function

changesSidesGroups(newside, oldside) --call function number one to do all the magic stuff, it will call function number two.

RE: How to Change a Unit's Side in Lua?

Posted: Wed Apr 10, 2019 10:01 am
by morphin
Hi Whicker

Thank's for your script.
When you post your script, it looses the array indicator ""

I have attached the script that include the array indicator




RE: How to Change a Unit's Side in Lua?

Posted: Mon Dec 20, 2021 6:07 pm
by DmitriyBlade
ORIGINAL: morphin

I have attached the script that include the array indicator
local oldside = "Pakistan" --set name of old side
local newside = "China" --set name of new side, must exist

function changesSidesGroups(newSide, oldSide) --function number one
local a = VP_GetSide({Side =oldSide}) -- get all units from old side

local groups = {} --create empty array(table) to store group guids
for i = 1, #a.units do -- loops thru all units on the side
local u = ScenEdit_GetUnit({guid = a.units.guid}) --convert VP guid to Unit wrapper
if u.type == 'Group' then -- check if group, if so add the group guid to the table
table.insert(groups, u.guid)
print(u.name.. " side: "..u.side ) --testing by printing to console
end --closes if statement
end --closes loop
for i = 1, #groups do --loop thru new table of group guids
print(groups) --testing by printing to console
ScenEdit_SetUnitSide({side=oldSide, guid=groups, newside=newSide}) --change side from old to new
end --closes loop
changeSidesUnits(newSide, oldSide) -- call second function to change units that are not in groups
end --closes function
function changeSidesUnits(newSide,oldSide) --function number two for non grouped units
local a2 = VP_GetSide({Side =oldSide}) -- get all units from old side - won't include groups as they are already done
local units = {} --create another empty table
for i = 1, #a2.units do --loop thru units and push them into the table
table.insert(units, a2.units.guid)
end --closes loop

for i = 1, #units do --loop thru table of units
print(units) --testing by printing to console
ScenEdit_SetUnitSide({side=oldSide, guid=units, newside=newSide}) --change side
end --closes loop
end -- closes second function


changesSidesGroups(newside, oldside) --call function number one to do all the magic stuff, it will call function number two.

Re: How to Change a Unit's Side in Lua?

Posted: Wed Jun 25, 2025 12:58 pm
by DmitriyBlade
If you playing scenario where some sides is your ai ally, and you want to change it to your control
change those as you wish:
local oldside = "South Korea"
local newside = "United States"

run script multiple times until all units under your control

Code: Select all

local oldside = "South Korea"
local newside = "United States"

function changesSidesGroups(newSide, oldSide)
    local a = VP_GetSide({Side = oldSide})
    local groups = {}

    -- Process groups (0-based indexing)
    for i = 0, #a.units - 1 do
        local unitDescriptor = a.units[i]
        if unitDescriptor and unitDescriptor.guid then
            local success, u = pcall(ScenEdit_GetUnit, {guid = unitDescriptor.guid})
            if success and u and u.type == 'Group' then
                table.insert(groups, unitDescriptor.guid)
                print(u.name .. " side: " .. u.side)
            else
                print("Skipping invalid group GUID: " .. unitDescriptor.guid)
            end
        end
    end

    -- Move groups to new side
    for i = 1, #groups do
        local guid = groups[i]
        print("Moving group: " .. guid)
        ScenEdit_SetUnitSide({guid = guid, newside = newSide, side = oldSide})
    end

    changeSidesUnits(newSide, oldSide) -- Process non-group units
end

function changeSidesUnits(newSide, oldSide)
    local a2 = VP_GetSide({Side = oldSide})

    -- Process non-group units (0-based indexing)
    for i = 0, #a2.units - 1 do
        local unitDescriptor = a2.units[i]
        if unitDescriptor and unitDescriptor.guid then
            local success, u = pcall(ScenEdit_GetUnit, {guid = unitDescriptor.guid})
            if success and u and u.type ~= 'Group' then
                print("Moving unit: " .. unitDescriptor.guid)
                ScenEdit_SetUnitSide({guid = unitDescriptor.guid, newside = newSide, side = oldSide})
            elseif success and u then
                print("Skipping group unit: " .. unitDescriptor.guid)
            else
                print("Skipping invalid unit GUID: " .. unitDescriptor.guid)
            end
        end
    end
end

changesSidesGroups(newside, oldside) -- Start the process