LUA Question.

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

LUA Question.

Post by tjhkkr »

I am not seeing what I am doing incorrectly. The LUA script does not throw me any diagnostics. But it also does not turn on the radars.
I do see the I have EMCON all caps, but the main LUA page shows that.
Neither works
====
math.random(os.time())
a = 0
b = 3
string.WorkUnit = {'Radar Alfa', 'Radar Bravo', 'Radar Charlie'}

repeat
a = a + 1
r = math.random(1,5)
if r < 3 then
ScenEdit_SetEMCON('Mission', string.WorkUnit[a], 'Radar=Active')
else
ScenEdit_SetEMCON('Mission', string.WorkUnit[a], 'Radar=Passive')
end
until a >= b
====
math.random(os.time())
a = 0
b = 4
string.WorkUnit = {'Vehicles Alfa', 'Vehicles Bravo', 'Vehicles Charlie', 'Radar Surface Alfa'}

repeat
a = a + 1
r = math.random(1,5)
if r < 3 then
ScenEdit_SetEMCON('Unit', string.WorkUnit[a], 'Radar=Active')
else
ScenEdit_SetEMCON('Unit', string.WorkUnit[a], 'Radar=Passive')
end
until a >= b
====
Does the EMCON for the unit and or mission have to be set a certain way for this to work?

Again no diagnostics but does not work. I printed the random number as well as the work unit... it all looks good.
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
Whicker
Posts: 665
Joined: Tue Jun 19, 2018 9:54 pm

RE: LUA Question.

Post by Whicker »

are you moving time forward? it won't happen without time advancing a second or two.
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: LUA Question.

Post by tjhkkr »

Yes, I did, several times to make sure I was not losing it.
The radar never came on.
Does the unit itself need a special setting?
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: LUA Question.

Post by KnightHawk75 »

Doesn't look like ScenEdit_SetEMCON problem, looks like logic issue.

Not entirely clear what you're trying to do from the code above.
Is it something like 40% chance of changing the emcom on all table entries, or is it pick a single random unit and have 40% active\60% passive change on that one only?

Attached text file (to maintain formatting) should provide both examples or help if it's some variation, slap it into the console, change the unitnames and try it.

Also note the line math.random(os.time) in your post is wrong, I think you meant math.randomseed(os.time) there, though you may not need any calls to randomseed at all (at least while working in console).

Hope this helps.

Edit:
btw not that it matters much, but the debug lines:
print('Invalid parameters, nil, or table with less than two entries.');
Should read:
print('Invalid parameters, table nil, or table with no rows');
Attachments
RandomEMCOM_Forum2.txt
(1.62 KiB) Downloaded 23 times
Whicker
Posts: 665
Joined: Tue Jun 19, 2018 9:54 pm

RE: LUA Question.

Post by Whicker »

Do you have the radar set to obey parent or whatever that top checkbox is? I think it needs to be checked? I do usually have trouble trying to do this, it always works in the end but the inheritance seems to give me trouble.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: LUA Question.

Post by KnightHawk75 »

ORIGINAL: Whicker

Do you have the radar set to obey parent or whatever that top checkbox is? I think it needs to be checked? I do usually have trouble trying to do this, it always works in the end but the inheritance seems to give me trouble.
Whicker,
Since he's not specifying 'Inherit' in the call parameters, it will auto disable inheritance by default even if already set, least this has been my experience in testing.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: LUA Question.

Post by KnightHawk75 »

That said in general you do have to be careful when doing missions,groups,etc to make sure your also not un-enabling the inheritance if ALSO screwing with units assigned to same mission\groups...unless such specifics are wanted. It's all a matter of what the person actually wants and the 'starting point' or assumed 'starting point' so to speak. :)

ie... assume the following sort of default
Mission1 - [inherited]
-Unit1 [inherited]
-Unit2 [inherited]
-Unit3 [inherited]

Mission2 - [inherited]
-Unit4 [inherited]
-Unit5 [no parent]

Mission3 - [no parent]
-Unit6 [no parent]
-Unit7 [no parent]

ScenEdit_SetEMCON('Mission', Mission1, 'Inherit;Radar=Active')
Will effectively set Mission1 to Active emcom along with Active on anything assigned to the mission whom already have inherited set. _However_ since it's STILL inheriting, it may get overridden from above.

ScenEdit_SetEMCON('Mission', Mission1, 'Radar=Active')
Will effectively set Mission1 to Active emcom along with active on anything assigned to mission one who already have inherited set. Mission1 should not get overridden from above it WILL be Active, As will be Units 1-3.

Then say you ran ...
ScenEdit_SetEMCON('Unit', Unit2, 'Inherit;Radar=Passive') --Would effectively do nothing.
ScenEdit_SetEMCON('Unit', Unit2, 'Radar=Passive') --Would make the change and flag Unit to as not using parent.


More..
ScenEdit_SetEMCON('Mission', Mission2, 'Radar=Active')
Will effectively set Mission2 to Active emcom along with active on anything assigned to mission2 who already have inherited set. Unit4 will be Active, Unit5 WILL NOT get set Active, till specifically told to inherit, or changed specifically.

Then say...
ScenEdit_SetEMCON('Unit', Unit5, 'Inherit') Would set Unit5 to Inherit and it would take the Active setting for Mission2 just set above.

ScenEdit_SetEMCON('Mission', Mission3, 'Radar=Passive')
Will set the mission3 Passive but not change jack on unit6 or unit7...till they are marked to inherit in code, or someone manually resets them or presses the reset on all effective units button for mission3.


Yeah it can get involved if you're screwing with units and the mission they belong to at basically the same time.
If you want to ensure things are sticky when setting it on Mission or Groups, I recommend looping though all members of said mission\group and applying an "inherit;" flag. That way you know whatever specific units were members are forced to take the change. That is ...unless you're doing something cool like trying to get a flight of say 4 f-35's to have 1 guy be the cec-radar and the others missile trucks,etc. But I digress. :)
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: LUA Question.

Post by tjhkkr »

ORIGINAL: KnightHawk75
Doesn't look like ScenEdit_SetEMCON problem, looks like logic issue.

Not entirely clear what you're trying to do from the code above.
Is it something like 40% chance of changing the emcom on all table entries, or is it pick a single random unit and have 40% active\60% passive change on that one only?

Attached text file (to maintain formatting) should provide both examples or help if it's some variation, slap it into the console, change the unitnames and try it.

Also note the line math.random(os.time) in your post is wrong, I think you meant math.randomseed(os.time) there, though you may not need any calls to randomseed at all (at least while working in console).

Hope this helps.

Edit:
btw not that it matters much, but the debug lines:
print('Invalid parameters, nil, or table with less than two entries.');
Should read:
print('Invalid parameters, table nil, or table with no rows');

I think you meant math.randomseed(os.time) there
Thank you; I did not know that...
Or maybe I did and I am mixing BASIC with LUA [:D]

Rather than have a dozen triggers/events, I put all the sensors in the table:
string.WorkUnit = {'Vehicles Alfa', 'Vehicles Bravo', 'Vehicles Charlie', 'Radar Surface Alfa'}
And then using an incrementing variable, walk each one through the code. It advances correctly when I have run the diagnostics.
It simply does not turn on the lights when it reaches the 40% threshold... and yes, it is triggered every 5 minutes with a 60% chance of being off and a 40% chance of being on. I would like to test every three minutes, but one problem at a time.

So the consensus is: check the inherit box.
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: LUA Question.

Post by KnightHawk75 »

uh, that wasn't my consensus per say tjhkkr.

The inheritance examples was specific to settings on 'mission' or 'group' emcom settings and understanding the effects vs desired or assumed effects, ie need know what your changing and what you are not under different circumstances, which can lead to unexpected results if not careful.

As for what you want I think I captured it all in the attached example scenario.
It flips a bunch of units radars and oecm on off every 3 minutes by default no matter wtf inheritance they already have or don't have from above, 40% chance of on every cycle, 60% off chance every cycle. Works perfectly for me. Run it for a minute or two on 5x time on side red you'll see it working.

Copy and paste as need for you own scenario from the two triggers & action scripts (one on-startup, one every minute).


TimedRadarFlipper.zip
Attachments
TimedRadar..rExample.zip
(18.37 KiB) Downloaded 30 times
User avatar
tjhkkr
Posts: 2431
Joined: Wed Jun 02, 2010 11:15 pm
Contact:

RE: LUA Question.

Post by tjhkkr »

ORIGINAL: KnightHawk75
uh, that wasn't my consensus per say tjhkkr.
The inheritance examples was specific to settings on 'mission' or 'group' emcom settings and understanding the effects vs desired or assumed effects, ie need know what your changing and what you are not under different circumstances, which can lead to unexpected results if not careful.
As for what you want I think I captured it all in the attached example scenario.
It flips a bunch of units radars and oecm on off every 3 minutes by default no matter wtf inheritance they already have or don't have from above, 40% chance of on every cycle, 60% off chance every cycle. Works perfectly for me. Run it for a minute or two on 5x time on side red you'll see it working.
Copy and paste as need for you own scenario from the two triggers & action scripts (one on-startup, one every minute).
TimedRadarFlipper.zip
Thank you Knighthawk! Appreciate it.
Remember that the evil which is now in the world will become yet more powerful, and that it is not evil which conquers evil, but only love -- Olga Romanov.
Post Reply

Return to “Lua Legion”