Page 1 of 1

Lua assigning Non-unique names to mission

Posted: Sun Sep 25, 2016 10:31 am
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')

RE: Lua assigning Non-unique names to mission

Posted: Sun Sep 25, 2016 12:28 pm
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?

RE: Lua assigning Non-unique names to mission

Posted: Sun Sep 25, 2016 1:26 pm
by Gunner98
No not essential. Its a minor action and I just didn't want to make it overly complex to script.

B

RE: Lua assigning Non-unique names to mission

Posted: Sun Sep 25, 2016 2:36 pm
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

RE: Lua assigning Non-unique names to mission

Posted: Sun Sep 25, 2016 2:46 pm
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

RE: Lua assigning Non-unique names to mission

Posted: Sun Sep 25, 2016 11:49 pm
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.

RE: Lua assigning Non-unique names to mission

Posted: Sun Sep 25, 2016 11:51 pm
by Gunner98
Point at the spot on the map. Cntl+X then in your script Cntl+V

B

RE: Lua assigning Non-unique names to mission

Posted: Mon Sep 26, 2016 7:08 pm
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

RE: Lua assigning Non-unique names to mission

Posted: Mon Sep 26, 2016 7:25 pm
by Gunner98
Thanks CK

Works like a charm

B