Lua Question
Moderator: MOD_Command
Lua Question
So I am adding random units to a scenario - works fine:
a = math.random(1,9)
if a <= 3 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
~30% chance of a Victor II, 50% chance of a Victor I and 10% chance of a Victor III. They all pop in a the same location but that's not a problem.
Now I want to add that new unit to a mission - this event will happen several times (possibly many times) over a long scenario (hmmm wonder which one...) so I think its simpler to just name them the same. Maybe this is wrong.
Because they are named the same - I need to find the GUID of the new unit. Have tried this:
local unit = ScenEditUnitX();
if unit ~= nil then
ScenEditAssignUnitToMission(unit.guid, "EastLant pt")
end
But I get this: attempt to call global 'ScenEditUnitX' (a nil value)
Cannot figure out how ScenEdit_GetUnit(table) would work in this case
So Lua doesn't know what I'm talking about and I'm not sure how to tell it to:
Get the unit you just created and assign it to a mission.
Any ideas?
a = math.random(1,9)
if a <= 3 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
~30% chance of a Victor II, 50% chance of a Victor I and 10% chance of a Victor III. They all pop in a the same location but that's not a problem.
Now I want to add that new unit to a mission - this event will happen several times (possibly many times) over a long scenario (hmmm wonder which one...) so I think its simpler to just name them the same. Maybe this is wrong.
Because they are named the same - I need to find the GUID of the new unit. Have tried this:
local unit = ScenEditUnitX();
if unit ~= nil then
ScenEditAssignUnitToMission(unit.guid, "EastLant pt")
end
But I get this: attempt to call global 'ScenEditUnitX' (a nil value)
Cannot figure out how ScenEdit_GetUnit(table) would work in this case
So Lua doesn't know what I'm talking about and I'm not sure how to tell it to:
Get the unit you just created and assign it to a mission.
Any ideas?
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/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
RE: Lua Question
There's probably a reason why you aren't doing it, but I thought of just bundling the AddUnit and AssignUnitToMission in the same script.
ie
if a == 1 then
ScenEdit_AddUnit({type=’Submarine’, side=’WP’, name=’K-___’, dbid=’___′, latitude=’______′, longitude=’-_________})
ScenEdit_AssignUnitToMission(‘K-___’, ‘Patrol’)
elseif a == 2
ScenEdit_AddUnit({type=’Submarine’, side=’WP’, name=’B-___’, dbid=’___′, latitude=’______′, longitude=’-_________})
ScenEdit_AssignUnitToMission(‘B-___’, ‘Patrol’)
ie
if a == 1 then
ScenEdit_AddUnit({type=’Submarine’, side=’WP’, name=’K-___’, dbid=’___′, latitude=’______′, longitude=’-_________})
ScenEdit_AssignUnitToMission(‘K-___’, ‘Patrol’)
elseif a == 2
ScenEdit_AddUnit({type=’Submarine’, side=’WP’, name=’B-___’, dbid=’___′, latitude=’______′, longitude=’-_________})
ScenEdit_AssignUnitToMission(‘B-___’, ‘Patrol’)
RE: Lua Question
Hay Coiler12
The script above would still require a unique unit name for each event ('K-___, or B-___) which I could do but that would mean I would need a slightly different script each time the event fired.
Plan is to have the event checked every hour with a low probability of firing, if it does fire, the Lua script kicks in.
I may be going about this the wrong way...
The script above would still require a unique unit name for each event ('K-___, or B-___) which I could do but that would mean I would need a slightly different script each time the event fired.
Plan is to have the event checked every hour with a low probability of firing, if it does fire, the Lua script kicks in.
I may be going about this the wrong way...
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/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
- michaelm75au
- Posts: 12455
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Lua Question
Are you really calling ScenEditUnitX()?
The command is ScenEdit_UnitX(). There is always a _ after ScenEdit
The command is ScenEdit_UnitX(). There is always a _ after ScenEdit
Michael
RE: Lua Question
OK, that's fixed:
Also realized the 'name' was probably wrong.
a = math.random(1,9)
if a <= 3 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
local unit = Scen_EditUnitX();
if unit ~= nil then
ScenEdit_AssignUnitToMission('unit', 'EastLant Ptl')
end
Still getting the same error though ERROR: [string "chunk"]:11: attempt to call global 'Scen_EditUnitX' (a nil value)
Also realized the 'name' was probably wrong.
a = math.random(1,9)
if a <= 3 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
local unit = Scen_EditUnitX();
if unit ~= nil then
ScenEdit_AssignUnitToMission('unit', 'EastLant Ptl')
end
Still getting the same error though ERROR: [string "chunk"]:11: attempt to call global 'Scen_EditUnitX' (a nil value)
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/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
- michaelm75au
- Posts: 12455
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Lua Question
OK - I knew that! Just not paying attention.
Return is now: Couldn't find the unit unit,nil
Return is now: Couldn't find the unit unit,nil
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/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
RE: Lua Question
I'm not very good with Lua but maybe someone can go further. If you assign a variable to the unit beforehand:
test1 = ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
print (test1)
test1 returns the GUID. How to select it though eludes me... I normally individually name all my units but it would be great if you solved this as it would help everyone. Keep us updated!
test1 = ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
print (test1)
test1 returns the GUID. How to select it though eludes me... I normally individually name all my units but it would be great if you solved this as it would help everyone. Keep us updated!
"Is game hard to pick up?" <- easier to pick up than most women.
- michaelm75au
- Posts: 12455
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Lua Question
The returned variable test1 is an object for the the unit added, not the GUID itself. You access the properties as such:
test1.name
test1.altitude
test1.guid
See http://commandlua.github.io/index.html#Unit
test1.name
test1.altitude
test1.guid
See http://commandlua.github.io/index.html#Unit
Michael
- michaelm75au
- Posts: 12455
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
-
- Posts: 2418
- Joined: Thu Dec 18, 2014 1:53 am
- Location: Brooklyn, NY
RE: Lua Question
ORIGINAL: Gunner98
OK, that's fixed:
Also realized the 'name' was probably wrong.
a = math.random(1,9)
if a <= 3 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
local unit = Scen_EditUnitX();
if unit ~= nil then
ScenEdit_AssignUnitToMission('unit', 'EastLant Ptl')
end
Still getting the same error though ERROR: [string "chunk"]:11: attempt to call global 'Scen_EditUnitX' (a nil value)
I noticed that in the final line of code you previously had:
ScenEdit_AssignUnitToMission('unit.guid', 'EastLant Ptl')
Could it be that the without .guid specified then the UnitX call is returning the whole table and causing the error?
- michaelm75au
- Posts: 12455
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Lua Question
Needs refining but I think I got it to work:
a = math.random(1,9)
if a <= 3 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
local test2=test1.guid
ScenEdit_AssignUnitToMission(test2, 'EastLant Ptl')
This worked for me. Although I am unsure if you can local the "test1", if so that would be better. Anyone know if this is possible?
a = math.random(1,9)
if a <= 3 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
local test2=test1.guid
ScenEdit_AssignUnitToMission(test2, 'EastLant Ptl')
This worked for me. Although I am unsure if you can local the "test1", if so that would be better. Anyone know if this is possible?
"Is game hard to pick up?" <- easier to pick up than most women.
- michaelm75au
- Posts: 12455
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Lua Question
Yes you can. And you can use the 'guid' property in the mission assignment..
Also I find using 'local' for the variables tend to be safer as it keeps them only for the current script.
local test1
a = math.random(1,9)
if a <= 3 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
ScenEdit_AssignUnitToMission(test1.guid, 'EastLant Ptl')
Also I find using 'local' for the variables tend to be safer as it keeps them only for the current script.
local test1
a = math.random(1,9)
if a <= 3 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
ScenEdit_AssignUnitToMission(test1.guid, 'EastLant Ptl')
Michael
RE: Lua Question
I am away from the game at the moment, will have to try this out over the weekend.
Yes the event has both the add unit and the assign mission script, and the UnitX() is referring to the unit created in the same script.
B
Yes the event has both the add unit and the assign mission script, and the UnitX() is referring to the unit created in the same 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/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
RE: Lua Question
Was not sure where to ask this so why not here:
Will Command evolve to a point where lua modules might be beneficial:
require ("myModule")
These would hold custom functions and data tables. I don't see a real need for them now. But maybe something to look into.
Separably, if everyone is getting frustrated with the search engine perhaps we can circulate a petition. The point I made above could have been answered with a search. I admired newer players for working through the situation.
Kevin
Will Command evolve to a point where lua modules might be beneficial:
require ("myModule")
These would hold custom functions and data tables. I don't see a real need for them now. But maybe something to look into.
Separably, if everyone is getting frustrated with the search engine perhaps we can circulate a petition. The point I made above could have been answered with a search. I admired newer players for working through the situation.
Kevin
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
Alfred Thayer Mahan
RE: Lua Question
Its a big ask of Slitherine and I'm not really sure it actually solves the problem unless they move to a different forum platform or hire some considerable Php expertise to re-engineer and deal with a back end db they don't want to screw up. Instead I think its better to use the help platform we've already implemented and is designed for q and a stuff. Its been great when its been used.
http://qa.commandmodernairnavaloperations.com/
Not sure on the Lua direction just yet. Its probably worth bringing up in beta with Michaelm.
Thanks
Mike
http://qa.commandmodernairnavaloperations.com/
Not sure on the Lua direction just yet. Its probably worth bringing up in beta with Michaelm.
Thanks
Mike
RE: Lua Question
OK, played around with this one again today:
local test1
a = math.random(1,9)
if a <= 3 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
ScenEdit_AssignUnitToMission(test1.guid, 'EastLant Ptl')
Returns: 'Couldn't find the unit EastLant Ptl'
played around with it a bit, tried Peter66's, script. Tried replacing 'Test2' with 'Reft', moving the 'end' to the last line etc - no luck. if I put test1.guid in'..', the return is the same but its looking for the unit test1.guid
I feel like this is close but am still missing something
Any other ideas? I do appreciate the help.
B
local test1
a = math.random(1,9)
if a <= 3 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
ScenEdit_AssignUnitToMission(test1.guid, 'EastLant Ptl')
Returns: 'Couldn't find the unit EastLant Ptl'
played around with it a bit, tried Peter66's, script. Tried replacing 'Test2' with 'Reft', moving the 'end' to the last line etc - no luck. if I put test1.guid in'..', the return is the same but its looking for the unit test1.guid
I feel like this is close but am still missing something
Any other ideas? I do appreciate the help.
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/
And our blog: http://northernfury.us/blog/post2/
Twitter: @NorthernFury94 or Facebook https://www.facebook.com/northernfury/
RE: Lua Question
Try this:
local test1
a = math.random(1,9)
if a <= 3 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
local test2=test1.guid
ScenEdit_AssignUnitToMission(test2, 'EastLant Ptl')
You forgot to turn test1 in to a guid.
local test1
a = math.random(1,9)
if a <= 3 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='161', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a == 4 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='386', latitude='57.4605029013587', longitude='-29.0748187917671'})
elseif a >= 5 then
test1=ScenEdit_AddUnit({type='Submarine', side='WP', name='Reft', dbid='186', latitude='57.4605029013587', longitude='-29.0748187917671'})
end
local test2=test1.guid
ScenEdit_AssignUnitToMission(test2, 'EastLant Ptl')
You forgot to turn test1 in to a guid.
"Is game hard to pick up?" <- easier to pick up than most women.
- michaelm75au
- Posts: 12455
- Joined: Sat May 05, 2001 8:00 am
- Location: Melbourne, Australia
RE: Lua Question
ORIGINAL: Gunner98
I am away from the game at the moment, will have to try this out over the weekend.
Yes the event has both the add unit and the assign mission script, and the UnitX() is referring to the unit created in the same script.
B
UnitX() is the unit that triggered the event, so not sure what you mean by 'the unit created in the same script'.
Michael