Page 1 of 1

Lua Command for Creating a Group?

Posted: Mon May 25, 2020 3:57 am
by ProdigyofMilitaryPride
Anyone got any recommendations? Hints or tips? Thanks.

RE: Lua Command for Creating a Group?

Posted: Mon May 25, 2020 4:07 am
by michaelm75au
There is a dedicated sub-forum for Lua questions - Lua legion under Modding. I think there are some answers to this there.

RE: Lua Command for Creating a Group?

Posted: Mon May 25, 2020 6:58 am
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.



RE: Lua Command for Creating a Group?

Posted: Mon May 25, 2020 4:57 pm
by ProdigyofMilitaryPride
Thanks for that. I'll keep the aircraft note in mind, too.

RE: Lua Command for Creating a Group?

Posted: Sun May 31, 2020 2:25 pm
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?

RE: Lua Command for Creating a Group?

Posted: Sun May 31, 2020 9:46 pm
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]


RE: Lua Command for Creating a Group?

Posted: Mon Jun 01, 2020 4:03 am
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)

RE: Lua Command for Creating a Group?

Posted: Thu Jun 04, 2020 7:32 pm
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?

RE: Lua Command for Creating a Group?

Posted: Thu Jun 04, 2020 9:18 pm
by KnightHawk75
Far as I know, nope.

RE: Lua Command for Creating a Group?

Posted: Sat Jun 13, 2020 5:00 am
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!

RE: Lua Command for Creating a Group?

Posted: Sat Jun 13, 2020 5:40 am
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.

RE: Lua Command for Creating a Group?

Posted: Sun Jun 14, 2020 1:41 am
by caohailiang
thanks! it works now

RE: Lua Command for Creating a Group?

Posted: Tue Aug 11, 2020 9:40 pm
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

RE: Lua Command for Creating a Group?

Posted: Wed Aug 12, 2020 4:13 am
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