Query sub 'Altitude' and set comms on/off

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

So this should be simple, but for some reason I can't figure out where to start (querying a unit's 'altitude'). I assume (possibly a bad move) that below -40 m /-131 ft a sub would loose comms. So what I'm trying to do is to query this unit's...

{name='Kasan (K-561)', guid='XMJRWB-0HME8AIUVIKKH'}

...'Altitude" every 30 seconds and if they are above 40 m turn comms on and turn comms off below there. Where I'm stuck in particular is querying the unit's altitude.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

I stand corrected I don't have turning off the comms because "outofcomms" is apparently a read only parameter???
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
musurca
Posts: 168
Joined: Wed Jul 15, 2020 10:06 pm
Contact:

RE: Query sub 'Altitude' and set comms on/off

Post by musurca »

The read-only attributes can usually be set with ScenEdit_SetUnit(). Here's how I might do it (enclosing the calls to ScenEdit_Set/GetUnit in a pcall, to avoid any silent exceptions if something happens to Kasan):

-----

Code: Select all

local kasan_guid = 'XMJRWB-0HME8AIUVIKKH'
 local success, unit = pcall(ScenEdit_GetUnit, {guid=kasan_guid})
 if unit then
     if unit.altitude < -40 then
         if unit.outofcomms == false then
             pcall(ScenEdit_SetUnit, {guid=kasan_guid, outofcomms=true})
         end
     else
         if unit.outofcomms == true then
             pcall(ScenEdit_SetUnit, {guid=kasan_guid, outofcomms=false})
         end
     end
 end
----

But isn't this also essentially what the 'Realistic Sub Comms' setting does automatically?
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

But isn't this also essentially what the 'Realistic Sub Comms' setting does automatically?

So there is only one operational unit in the scenario, the sub controlled by the player. Then there are the satellites and the intel they provide. I'm looking to have the player (in the sub) able to look at the satellite info when their unit is above -40 m but have it unavailable to them below -40 m.

The game function you loose control of the submarine, I don't want that, just the loss of other data coming into the sub.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
musurca
Posts: 168
Joined: Wed Jul 15, 2020 10:06 pm
Contact:

RE: Query sub 'Altitude' and set comms on/off

Post by musurca »

Oh I see — in that case, you’d want to mark the satellite units out of comms, not the submarine.

If for whatever reason that’s not possible, you could instead put the satellite units on a separate side friendly to the player side so that they share contact information. When the sub descends below -40m, you’d change the side posture to neutral.
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

ORIGINAL: musurca

Oh I see — in that case, you’d want to mark the satellite units out of comms, not the submarine.

If for whatever reason that’s not possible, you could instead put the satellite units on a separate side friendly to the player side so that they share contact information. When the sub descends below -40m, you’d change the side posture to neutral.

I like it! Thank you! simple is better!
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

musurca, Thanks for your help!!!!

I modified your code to this...
local kasan_guid = 'XMJRWB-0HME8AIUVIKKH'
local success, unit = pcall(ScenEdit_GetUnit, {guid=kasan_guid})
if unit then
if unit.altitude < -40 then
if unit.outofcomms == false then
ScenEdit_SetSidePosture("Russia","Satellites","F")
ScenEdit_SetSidePosture("Satellites","Russia","F")
end
else
if unit.outofcomms == true then
ScenEdit_SetSidePosture("Russia","Satellites","H")
ScenEdit_SetSidePosture("Satellites","Russia","H")
end
end
end

It doesn't fail in the Lua Console, but it also doesn't seem to be seeing Kasam's altitude.
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

It's finding the altitude as the print is giving -27 meters.
local unit = ScenEdit_GetUnit ({side="Russia", Name="Kasan (K-561)", guid='XMJRWB-0HME8AIUVIKKH'})
print(unit.altitude)
if unit.altitude > -40 then
if unit.altitude == false then
ScenEdit_SetSidePosture("Russia","Satellites","F")
ScenEdit_SetSidePosture("Satellites","Russia","F")
end
else
if unit.altitude == true then
ScenEdit_SetSidePosture("Russia","Satellites","H")
ScenEdit_SetSidePosture("Satellites","Russia","H")
end
end
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

Success!!!!!!!

musruca, THANK YOU for your help, you sent me in teh right direction!!!!
local unit = ScenEdit_GetUnit ({side="Russia", Name="Kasan (K-561)", guid='XMJRWB-0HME8AIUVIKKH'})
print(unit.altitude)
ldepth = unit.altitude
print(ldepth)
if ldepth < -41 then
ScenEdit_SetSidePosture("Russia","Satellites","N")
ScenEdit_SetSidePosture("Satellites","Russia","N")
end

if ldepth > -41 then
ScenEdit_SetSidePosture("Russia","Satellites","F")
ScenEdit_SetSidePosture("Satellites","Russia","F")
end
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

ORIGINAL: musurca

The read-only attributes can usually be set with ScenEdit_SetUnit(). Here's how I might do it (enclosing the calls to ScenEdit_Set/GetUnit in a pcall, to avoid any silent exceptions if something happens to Kasan):

-----

Code: Select all

local kasan_guid = 'XMJRWB-0HME8AIUVIKKH'
 local success, unit = pcall(ScenEdit_GetUnit, {guid=kasan_guid})
 if unit then
     if unit.altitude < -40 then
         if unit.outofcomms == false then
             pcall(ScenEdit_SetUnit, {guid=kasan_guid, outofcomms=true})
         end
     else
         if unit.outofcomms == true then
             pcall(ScenEdit_SetUnit, {guid=kasan_guid, outofcomms=false})
         end
     end
 end
----

But isn't this also essentially what the 'Realistic Sub Comms' setting does automatically?
-----
local kasan_guid = 'XMJRWB-0HME8AIUVIKKH'
local success, unit = pcall(ScenEdit_GetUnit, {guid=kasan_guid})
if unit then
if unit.altitude < -40 then
if unit.outofcomms == false then
pcall(ScenEdit_SetUnit, {guid=kasan_guid, outofcomms=true})
end
else
if unit.outofcomms == true then
pcall(ScenEdit_SetUnit, {guid=kasan_guid, outofcomms=false})
end
end
end

----

BTW, how do you get the indentions in the message board/Forum post????
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
musurca
Posts: 168
Joined: Wed Jul 15, 2020 10:06 pm
Contact:

RE: Query sub 'Altitude' and set comms on/off

Post by musurca »

Happy to help!

You can get the indentations by using the 'code' tag from the buttons above the text box. But actually I generally avoid it unless the code is very short-- longer segments break the layout on most browsers.
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Query sub 'Altitude' and set comms on/off

Post by KnightHawk75 »

ORIGINAL: musurca

Happy to help!

You can get the indentations by using the 'code' tag from the buttons above the text box. But actually I generally avoid it unless the code is very short-- longer segments break the layout on most browsers.

Yup at about the 20 line barrier it get garbled, so about every 15/20 lines end the original code tag and insert a new one, it adds extra unavoidable spacing though, which while annoying is less so then no indentation most the time. ;)
User avatar
BeirutDude
Posts: 2799
Joined: Sat Apr 27, 2013 9:44 am
Location: Jacksonville, FL, USA

RE: Query sub 'Altitude' and set comms on/off

Post by BeirutDude »

I found this code in the Silent Service DLC and there is a part I don't understand...

x = ScenEdit_GetUnit({guid='c0e0b7a4-2ec7-4c75-add0-c53dead2fad8'})
if x.altitude < -21 then
ScenEdit_MsgBox('Satellite connection lost.', 6)
assets = {
{name='Kosmos 2221',guid='7861ff62-d7fd-405d-b768-8e9271bc414f'},
{name='Kosmos 2227',guid='0e7f4bc3-3185-4c6c-9403-de8aae1f43a6'},
{name='Kosmos 2237',guid='18cc2131-a9db-4a2c-a6ae-3e1637c66d3f'},
{name='Kosmos 2263',guid='1bdaacb3-94b9-45b0-9548-071973fb6689'},
{name='Kosmos 2263',guid='1bdaacb3-94b9-45b0-9548-071973fb6689'},
{name='Kosmos 2278',guid='f4c61907-6802-4fa3-83df-7433def6d8d8'},
{name='Kosmos 2297',guid='585ed1d5-920d-42be-b57c-2a83d5260bb5'},
{name='Kosmos 2333',guid='bdcf7c78-eb2b-4e84-ba7d-7f57129f1756'},
{name='Kosmos 2335',guid='b6b7296d-1684-47fc-9ac6-8c6c86f8d0bd'},
{name='Kosmos 2347',guid='0e012194-01b5-46fe-8b78-4abaf814b5fe'},
{name='Kosmos 2359',guid='98ba4ce3-6686-46ec-b21b-5081dfb9ac16'},
{name='Kosmos 2360',guid='9e968956-a328-491e-90c2-8052cdf25161'},
{name='FV Castro el Gato',guid='5d4c309d-0894-4bd8-b0e8-2f54c0d02c95'},
{name='FV Viktor Kot', guid='fcee49f9-5df5-451f-a721-9d763083e876'},
}
for i = 1,#assets do
unit = ScenEdit_GetUnit({guid=assets.guid})
if unit ~= nil then
ScenEdit_SetUnit({side='Russia',guid=assets.guid,OutOfComms=true})
end
end
ScenEdit_SetEvent('SatelliteCheck', {isactive=false})
end



I'm not familiar with a "for" statement what is this doing? "for i = 1,#assets do"
And this as well? " unit = ScenEdit_GetUnit({guid=assets.guid})"
"Some people spend an entire lifetime wondering if they made a difference. The Marines don't have that problem."
PRESIDENT RONALD REAGAN, 1985

I was Navy, but Assigned TAD to the 24th MAU Hq in Beirut. By far the finest period of my service!
KnightHawk75
Posts: 1850
Joined: Thu Nov 15, 2018 7:24 pm

RE: Query sub 'Altitude' and set comms on/off

Post by KnightHawk75 »

ORIGINAL: BeirutDude

I found this code in the Silent Service DLC and there is a part I don't understand...
...

I'm not familiar with a "for" statement what is this doing? "for i = 1,#assets do"
And this as well? " unit = ScenEdit_GetUnit({guid=assets.guid})"

The for statement sets up a loop which exits based on the value of a variable here it's 'i' that will get auto incremented by default upon each loop.
for i = 1 (initial value of i is 1) to #assets (the count of or number of entries in the assets table) do.

So say #assets is 14... the statement says 'Do the following while i is between 1 and 14'
...does iteration based on i=1... gets to end starts over at top again with i=2...etc..etc.. till 15.


unit = ScenEdit_GetUnit({guid=assets.guid})
-- Get a unit wrapper for unit with the guid from the assets table ass and assign it to var unit. However I think that should read guid=assets.guid if it's to function right.








Post Reply

Return to “Lua Legion”