Lua assigning Non-unique names to mission

Post new mods and scenarios here.

Moderator: MOD_Command

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

Lua assigning Non-unique names to mission

Post by Gunner98 »

Trying to make a random script where units pop up regularly and cause the player some minor issues. The script below works for the random generation but when I do the standard - ScenEdit_AssignUnitToMission, Lua does what it's supposed to and grabs the first unit with the name. Is there a way I can make it grab all units with that same name?

Thanks

B



a = math.random(1,6)

if a == 1 then
ScenEdit_AddUnit({type='Ship', side='Cuba', name='Bandito', dbid='330', latitude='22.448075078579', longitude='-78.9575872674641'})

elseif a == 2 then
ScenEdit_AddUnit({type='Ship', side='Cuba', name='Bandito', dbid='330', latitude='22.6824953509623', longitude='-79.5730956697378'})

elseif a == 3 then
ScenEdit_AddUnit({type='Ship', side='Cuba', name='Bandito', dbid='330', latitude='23.0265691618325', longitude='-80.451326876993'})

elseif a == 4 then
ScenEdit_AddUnit({type='Ship', side='Cuba', name='Bandito', dbid='330', latitude='23.1006901157562', longitude='-81.1965671192377'})

elseif a == 5 then
ScenEdit_AddUnit({type='Ship', side='Cuba', name='Bandito', dbid='330', latitude='21.8084144174213', longitude='-77.3702693649615'})

elseif a == 6 then
ScenEdit_AddUnit({type='Ship', side='Cuba', name='Bandito', dbid='330', latitude='24.1456777943727', longitude='-78.0061405217515'})
end
ScenEdit_AssignUnitToMission('Bandito', 'East Strike')
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/
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

RE: Lua assigning Non-unique names to mission

Post by Rory Noonan »

Not at my computer now (and not good enough at Lua to use my phone) but you could use a key library variable to increment the unit name and then reference it in the assignment function. Is it essential that the units all have the same name?
Image
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Lua assigning Non-unique names to mission

Post by Gunner98 »

No not essential. Its a minor action and I just didn't want to make it overly complex to script.

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/
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: Lua assigning Non-unique names to mission

Post by ckfinite »

To deal with units with the same name, you have three options:

* You can rename units as you examine them, then rename them back when you're done
* You can use GUIDs exclusively to select units
* You can use VP_GetSide to list all units of the side, then select from that

For an example of the last one, you could write

for _,v in pairs(VP_GetSide("Cuba").units) do
local unit = ScenEdit_GetUnit({guid=v.objectid})
if unit.name == "Bandito" then
unit.mission = "East Strike"
end
end
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Lua assigning Non-unique names to mission

Post by Gunner98 »

CK

That example is brilliant, quick and simple. Thank you

Just tested it and got:

ERROR: invalid arguments to method call

Do I need to load a library or something up first?

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
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

RE: Lua assigning Non-unique names to mission

Post by kevinkins »

Sorry to side track. But is there an easy way to copy and paste long. and lat. from the game map into your lua script. Or are you guys using an auxiliary map for that purpose? Thanks.
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Lua assigning Non-unique names to mission

Post by Gunner98 »

Point at the spot on the map. Cntl+X then in your script Cntl+V

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/
ckfinite
Posts: 208
Joined: Fri Jul 19, 2013 10:33 pm

RE: Lua assigning Non-unique names to mission

Post by ckfinite »

Sorry about the broken script, here's a fixed version

for _,v in pairs(VP_GetSide({name="Cuba"}).units) do
local unit = ScenEdit_GetUnit({guid=v.objectid})
if unit.name == "Bandito" then
unit.mission = "East Strike"
end
end
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: Lua assigning Non-unique names to mission

Post by Gunner98 »

Thanks CK

Works like a charm

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 “Mods and Scenarios”