Lua Command for Creating a Group?

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

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

Lua Command for Creating a Group?

Post by ProdigyofMilitaryPride »

Anyone got any recommendations? Hints or tips? Thanks.
"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
michaelm75au
Posts: 12464
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua Command for Creating a Group?

Post by michaelm75au »

There is a dedicated sub-forum for Lua questions - Lua legion under Modding. I think there are some answers to this there.
Michael
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Lua Command for Creating a Group?

Post by KnightHawk75 »

@ProdigyofMilitaryPride - What issues are you having\running into assigning units to groups?

while creating:
see https://www.matrixgames.com/forums/tm.asp?m=4816227

After:
local u;
u = ScenEdit_GetUnit({side='someside',name='somename'})
u.group = 'my new or existing groupname';

Doing a bunch with similar names 1-6:
local u;
for i=1,6 do
u = ScenEdit_GetUnit({side='someside',name='somename #' .. i})
u.group = 'my new or existing groupname';
end
Note: If the unit type is an aircraft, remember the models & loadouts need to be the same.


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

RE: Lua Command for Creating a Group?

Post by ProdigyofMilitaryPride »

Thanks for that. I'll keep the aircraft note in mind, too.
"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
orca
Posts: 545
Joined: Wed Nov 06, 2013 4:59 pm

RE: Lua Command for Creating a Group?

Post by orca »

Would this work if the units are already in a different preexisting group? ie would it change the unit from being in the original group to being in the new group?

If no how do you remove a unit from a group?
User avatar
michaelm75au
Posts: 12464
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua Command for Creating a Group?

Post by michaelm75au »

A unit can only belong to one group. So changing a unit's group is a simple as changing the '.group'.
To remove a unit from a group, just blank out the unit's '.group'. [ <unit_wrapper_variable>.group = '' from memory]

Michael
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Lua Command for Creating a Group?

Post by KnightHawk75 »

ORIGINAL: orca

Would this work if the units are already in a different preexisting group? ie would it change the unit from being in the original group to being in the new group?

If no how do you remove a unit from a group?
local u;
u = ScenEdit_GetUnit({side='someside',name='somename'})
u.group = ""; -- removed from existing. (you may be able to skip this step but dont lol.)
u.group = "MyNewGroup"; -- added to another (if it doesn't exist yet it should be created automagically)
jkgarner
Posts: 175
Joined: Thu Apr 30, 2020 12:42 pm

RE: Lua Command for Creating a Group?

Post by jkgarner »

Is it possible to assign a unit to group when you create it with ScenEdit_AddUnit() by setting the group='somegroup' in the unit descriptor?
If this works, must the group exist before the first unit is assigned thusly, or will the group be created with the AddUnit call?
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Lua Command for Creating a Group?

Post by KnightHawk75 »

Far as I know, nope.
caohailiang
Posts: 48
Joined: Sun Jun 07, 2020 10:17 am

RE: Lua Command for Creating a Group?

Post by caohailiang »

a follow up question with this:
how do i generate a unique group id each time?
what i did was initializing the group id when scenario is loaded, every time after the group id is assigned, groupid=groupid+1. but you will start to get duplicated group names if the scenario is saved and loaded, since the groupid will be re-initialized at origin value.
thanks!
User avatar
michaelm75au
Posts: 12464
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua Command for Creating a Group?

Post by michaelm75au »

You could look thru the unit names for the matching group name, and use the last 'number' found as the starting number, or keep the last issued number in the KeyStore (which is saved as part of the scenario) with ScenEdit_SetKeyValue/ScenEdit_GetKeyValue.
Michael
caohailiang
Posts: 48
Joined: Sun Jun 07, 2020 10:17 am

RE: Lua Command for Creating a Group?

Post by caohailiang »

thanks! it works now
orca
Posts: 545
Joined: Wed Nov 06, 2013 4:59 pm

RE: Lua Command for Creating a Group?

Post by orca »

ORIGINAL: KnightHawk75
ORIGINAL: orca

Would this work if the units are already in a different preexisting group? ie would it change the unit from being in the original group to being in the new group?

If no how do you remove a unit from a group?
local u;
u = ScenEdit_GetUnit({side='someside',name='somename'})
u.group = ""; -- removed from existing. (you may be able to skip this step but dont lol.)
u.group = "MyNewGroup"; -- added to another (if it doesn't exist yet it should be created automagically)

Thanks I am able to get this to work. But I want to ensure that I don't get an error if the unit no longer exists in the scenario (ie is destroyed). Is there a script utilizing something like below? I tried but the name must be a string. Any other way to do this?

local u:
if u = ScenEdit_GetUnit({side='someside',name=u}) ~= nil then
u.group = ""; -- removed from existing. (you may be able to skip this step but dont lol.)
u.group = "MyNewGroup"; -- added to another (if it doesn't exist yet it should be created automagically)
end
User avatar
michaelm75au
Posts: 12464
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua Command for Creating a Group?

Post by michaelm75au »

local u = ScenEdit_GetUnit({name='Group_2612', guid='cd015e09-d208-4e67-9af0-3e8e0412209c'})
if u == nil then
-- unit destroyed
else
-- do something
end
Michael
Post Reply

Return to “Lua Legion”