How to Change a Unit's Side in Lua?
Moderator: MOD_Command
-
- Posts: 106
- Joined: Thu Apr 16, 2015 11:54 pm
How to Change a Unit's Side in Lua?
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
"Anything built by human hands can be destroyed. This is no exception." - Kei "Edge" Nagase, Ace Combat 5: The Unsung War
RE: How to Change a Unit's Side in Lua?
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
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)
Charles Edward Montague, English novelist and essayist
~Disenchantment, ch. 15 (1922)
RE: How to Change a Unit's Side in Lua?
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.
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.
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: How to Change a Unit's Side in Lua?
Not really. Each side has an unique code (GUID) and only one of these codes is controlled by the player at a time.
Michael
- michaelm75au
- Posts: 12457
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: How to Change a Unit's Side in Lua?
Not sure about the practicality of creating a function to do this bulk change.
Michael
RE: How to Change a Unit's Side in Lua?
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.
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?
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
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 88 times
-
- Posts: 98
- Joined: Mon Dec 20, 2021 3:22 pm
- Location: Russia
RE: How to Change a Unit's Side in Lua?
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.
-
- Posts: 98
- Joined: Mon Dec 20, 2021 3:22 pm
- Location: Russia
Re: How to Change a Unit's Side in Lua?
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
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