Grouping units

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

Grouping units

Post by Gunner98 »

Hay guys

Building a base on the AI side. This will happen at a certain point where a single unit airfield on a neutral side will be replaced by a multiple unit base on a bad guy side. I want it so the player can attack it if he does so before it is too heavily defended.

No issues adding all the units, that all works. Question is how do I group the units into a base?

Tx
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
hasler
Posts: 39
Joined: Mon Jul 31, 2017 8:28 pm

RE: Grouping units

Post by hasler »

I am away from my computer right now but I do unit and group creation like the linked example on the Lua got hub. http://commandlua.github.io/examples/sc ... t.lua.html
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Grouping units

Post by Gunner98 »

OK - never saw this example before, right there in front of me. Thanks
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Grouping units

Post by Gunner98 »

OK Just able to get back to this now:

Getting an error:
ERROR: [string "Console"]:32: '}' expected (to close '{' at line 26) near 'for'

Played around with it for a while but no joy - any ideas?

Tx

------------------------------------

function AddMultipleUnits()
-- list of units to add
-- there are 2 base types of elements in this table - unit and group
-- unit: contains data for adding the unit, and optional group or mission data. the mission data is not used in this example.
-- group: contains data for adding some group information. the group is assumed to have been created when it was assigned to a unit
local newUnits = {
{unit={type = 'Facility', name = 'Maribor main Runway', heading = 120, dbid = 55, side = 'Hugary', latitude='46.4803890511425', longitude='15.6878028852709'},
{unit={type = 'Facility', name = 'Maribor grass Runway', heading = 120, dbid = 945, side = 'Hugary', latitude='46.4814496219698', longitude='15.6896462040406'},
{unit={type = 'Facility', name = 'Maribor parking', dbid = 186, side = 'Hugary', latitude='46.4805743060371', longitude='15.6852357716592'},
{unit={type = 'Facility', name = 'Maribor parking', dbid = 186, side = 'Hugary', latitude='46.4804070784834', longitude='15.6854777981397'},
{unit={type = 'Facility', name = 'Maribor parking', dbid = 186, side = 'Hugary', latitude='46.4801134880231', longitude='15.6856922948778'},
{unit={type = 'Facility', name = 'Maribor parking', dbid = 186, side = 'Hugary', latitude='46.4796105298677', longitude='15.6864813619985'},
{unit={type = 'Facility', name = 'Maribor parking', dbid = 186, side = 'Hugary', latitude='46.4793804779242', longitude='15.6866653289655'},
{unit={type = 'Facility', name = 'Maribor parking', dbid = 186, side = 'Hugary', latitude='46.4792086519584', longitude='15.6867853295262'},
{unit={type = 'Facility', name = 'Maribor access', dbid = 306, side = 'Hugary', latitude='46.4804079946791', longitude='15.6865743476855'},
{unit={type = 'Facility', name = 'Maribor access', dbid = 306, side = 'Hugary', latitude='46.4798000404651', longitude='15.6871814779628'},
{unit={type = 'Facility', name = 'Maribor Control Twr', dbid = 3, side = 'Hugary', latitude='46.4793161568347', longitude='15.6851137247781'},
{unit={type = 'Facility', name = 'Maribor Hangar', dbid = 9, side = 'Hugary', latitude='46.4786876099595', longitude='15.68629942848'},
{unit={type = 'Facility', name = 'Maribor Ammo', dbid = 320, side = 'Hugary', latitude='46.4808465642364', longitude='15.6851757196959'},
{unit={type = 'Facility', name = 'Maribor Fuel', dbid = 944, side = 'Hugary', latitude='46.4782466613332', longitude='15.6881227119196'},
{unit={type = 'Facility', name = 'MANPADS', dbid = 416, side = 'Hugary', latitude='46.4821508520397', longitude='15.6832893809333'},
{unit={type = 'Facility', name = 'MANPADS', dbid = 416, side = 'Hugary', latitude='46.4768610848761', longitude='15.6848074901503'},
{unit={type = 'Facility', name = 'MANPADS', dbid = 416, side = 'Hugary', latitude='46.4793522402419', longitude='15.6913775009575'},
{unit={type = 'Facility', name = 'MANPADS', dbid = 416, side = 'Hugary', latitude='46.4834711133154', longitude='15.6877321512789'},
{unit={type = 'Facility', name = 'ZU-23-2', dbid = 2468, side = 'Hugary', latitude='46.4803890981919', longitude='15.684688367051'},
{unit={type = 'Facility', name = 'ZU-23-2', dbid = 2468, side = 'Hugary', latitude='46.4786662193619', longitude='15.6867238319341'},
{unit={type = 'Facility', name = 'ZU-23-2', dbid = 2468, side = 'Hugary', latitude='46.4802583518172', longitude='15.6899795108315'},
{unit={type = 'Facility', name = 'ZU-23-2', dbid = 2468, side = 'Hugary', latitude='46.4817310715893', longitude='15.6882468148245'},
{group={side='Hugary', name='Maribor Airbase',{latitude='46.4803890511425', longitude='15.6878028852709'}}}},
}
-- process the table, adding the units to the scenario
for i = 1, #newUnits, 1 do
-- the table element .unit contains data??
if newUnits.unit ~= nil then
print('adding #' .. i .. ' ' .. newUnits.unit.name)
-- the table element .unit contains the data to pass as the parameter to the SE_AddUnit() function
local u1 = ScenEdit_AddUnit( newUnits.unit )
-- check that the function worked. a 'unit' wrapper should be returned
if u1 == nil then
print( "failed to add unit")
else
-- does the same table entry contain an element .group. this is the group to assign unit to
if newUnits.group ~= nil then
u1.group=newUnits.group -- this will also create the group if it doesn't exist.
end
end
elseif newUnits.group ~= nil then
print('adding #' .. i .. ' ' .. newUnits.group.name)
-- update the group with the data from the table
local u1 = ScenEdit_SetUnit( newUnits.group )
if u1 == nil then
print( "failed to add unit")
end
end
end
end
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Grouping units

Post by michaelm75au »

There seems to be a missing '}' at the ends of the {unit=........}. They should be 'one block'
E.g.
{unit=
{
type = 'Facility', name = 'Maribor main Runway', heading = 120, dbid = 55, side = 'Hugary', latitude='46.4803890511425', longitude='15.6878028852709'
}
},
Michael
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Grouping units

Post by michaelm75au »

Actually there are a few things wrong. Let me drop in a full updated version
Michael
User avatar
michaelm75au
Posts: 12455
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Grouping units

Post by michaelm75au »

You forgot the index to the newUnits table. You may have done it when pasted but '[ i ]' means italic in the posting[:D]
The other thing is that if you want to assign the units to a group, you need to do so in the 'unit=' as shown.

Attached the code as a file as the I forgot the post formatting kills it.
Attachments
test.txt
(5.06 KiB) Downloaded 20 times
Michael
Whicker
Posts: 664
Joined: Tue Jun 19, 2018 9:54 pm

RE: Grouping units

Post by Whicker »

That example is sort of complex, the important part of adding a unit to a group is simply to set the units group value to the name of the group:

local u = SE_GetUnit({blah blah})
u.group="that big group"
Whicker
Posts: 664
Joined: Tue Jun 19, 2018 9:54 pm

RE: Grouping units

Post by Whicker »

I played around with making an air base via a lua function, it worked pretty well:
function W_CreateAirBase(basename,side,latlonTable, runways, taxiways, accesspoints, tarmacspaces, hangers, ammopads,detectable)
local lat=latlonTable['latitude']
local lon=latlonTable['longitude']
for i=1,runways do
local u = ScenEdit_AddUnit({side=side, type='Facility', name='Runway '..i, dbid=55, autodetectable=detectable, Lat=lat, Lon=lon})
u.group =basename
lat=lat +.004
end --runways

lat=latlonTable['latitude'] --reset lat and add to lon

lon=latlonTable['longitude'] + .002
for i=1,taxiways do
local u = ScenEdit_AddUnit({side=side, type='Facility', name='Taxiway '..i, dbid=1424, autodetectable=detectable, Lat=lat, Lon=lon})
u.group =basename
lat=lat +.004
end
lat=latlonTable['latitude'] --reset lat and add to lon

lon=latlonTable['longitude'] + .004
for i=1,accesspoints do
local u = ScenEdit_AddUnit({side=side, type='Facility', name='Access Point '..i, dbid=353, autodetectable=detectable, Lat=lat, Lon=lon})
u.group =basename
lat=lat +.002
end
lat=latlonTable['latitude'] --reset lat and add to lon

lon=latlonTable['longitude'] + .006
for i=1,tarmacspaces do
local u = ScenEdit_AddUnit({side=side, type='Facility', name='Tarmac Space '..i, dbid=344, autodetectable=detectable, Lat=lat, Lon=lon})
u.group =basename
lat=lat +.002
end
lat=latlonTable['latitude'] --reset lat and add to lon

lon=latlonTable['longitude'] + .008
for i=1,ammopads do
local u = ScenEdit_AddUnit({side=side, type='Facility', name='Ammo Pad '..i, dbid=1496, autodetectable=detectable, Lat=lat, Lon=lon})
u.group =basename
lat=lat +.002
end

lon=latlonTable['longitude'] - .002
for i=1,hangers do
local u = ScenEdit_AddUnit({side=side, type='Facility', name='Hanger '..i, dbid=9, autodetectable=detectable, Lat=lat, Lon=lon})
u.group =basename
lat=lat -.002
end
end --function end

W_CreateAirBase('Red FOB','Red', {latitude='-3.16785920176629', longitude='128.539230124161'},2,2,4,8,2,1,true)

User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Grouping units

Post by Gunner98 »

Thanks very much for the help guys, I'll play around with it today.

B
Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Grouping units

Post by Gunner98 »

Michael

That works like a charm - thank you very much once again[&o]

Whicker
I'm going to use your bit in my next scenario on Iceland - have been mulling over some ideas for that one quite some time and your code will come in very handy.

OK now I just need to write up some briefings and now that the prosecution zone bit is fixed I can get to testing! Cheers guys

B

Check out our novel, Northern Fury: H-Hour!: http://northernfury.us/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
Post Reply

Return to “Lua Legion”