How to Change a Unit's Side in Lua?

Post new mods and scenarios here.

Moderator: MOD_Command

Post Reply
ProdigyofMilitaryPride
Posts: 106
Joined: Thu Apr 16, 2015 11:54 pm

How to Change a Unit's Side in Lua?

Post by ProdigyofMilitaryPride »

What's the kind of table I need to pull it off successfully? A step-by-step example could help, please.
"The courageous must protect freedom." - Dwight D. Eisenhower
"Anything built by human hands can be destroyed. This is no exception." - Kei "Edge" Nagase, Ace Combat 5: The Unsung War
User avatar
stilesw
Posts: 1569
Joined: Wed Jun 25, 2014 10:08 pm
Location: Hansville, WA, USA

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

Post 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
“There is no limit to what a man can do so long as he does not care a straw who gets the credit for it.”

Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
Whicker
Posts: 664
Joined: Tue Jun 19, 2018 9:54 pm

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

Post 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.
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

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

Post 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.
Michael
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

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

Post by michaelm75au »

Not sure about the practicality of creating a function to do this bulk change.
Michael
Whicker
Posts: 664
Joined: Tue Jun 19, 2018 9:54 pm

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

Post 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.
morphin
Posts: 687
Joined: Fri Apr 26, 2002 6:51 pm
Location: Switzerland

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

Post 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



Attachments
changeSideAllUnits.txt
(1.79 KiB) Downloaded 84 times
DmitriyBlade
Posts: 73
Joined: Mon Dec 20, 2021 3:22 pm
Location: Russia

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

Post 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.
Post Reply

Return to “Mods and Scenarios”