How to Change a Unit's Side in Lua?
Posted: Fri Dec 09, 2016 8:49 pm
What's the kind of table I need to pull it off successfully? A step-by-step example could help, please.
What's your Strategy?
https://forums.matrixgames.com:443/
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.
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.
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