Lua - Trigger Altitude AND in inside Area

All discussions & material related to Command's Lua interface

Moderators: RoryAndersonCDT, michaelm75au, angster, MOD_Command

Post Reply
User avatar
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

Lua - Trigger Altitude AND in inside Area

Post by kevinkins »

Helicopter passes into a small area to "drop passengers" (rappel) but only < 100 feet triggers a scoring event. Any ideas for a code snippet? Right now the player would get points at any altitude. Need some sort of if then code.

Thanks

Kevin
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
User avatar
somi83
Posts: 59
Joined: Sat Feb 06, 2016 12:59 pm
Location: Novi Sad, Serbia

RE: Lua - Trigger Altitude AND in inside Area

Post by somi83 »

Hi,
Set up event as
1. Trigger: Unit inside area
2. Condition: lua script:

Code: Select all

local unit = ScenEdit_GetUnit({guid='82528034-b846-4200-b5f9-069b207db6f1'})
 x = (unit.altitude)
 if x < 100 
 then return true
 elseif x > 100
 then return false
 end
local unit = ScenEdit_GetUnit({guid='82528034-b846-4200-b5f9-069b207db6f1'}), ({guid = 'guid'}) 'guid' you can get it from sbr script, or you can use ({side = "name of the side", unitname = "name of the unit"})
3. Action lua script:

Code: Select all

local sc1=ScenEdit_GetScore("US")
 ScenEdit_SetScore('US',sc1 + 50,'Viper #1 took off from Nellis AFB')
sc1=ScenEdit_GetScore("US"), instead of "US" type the side of the unit
ScenEdit_SetScore('US',sc1 + 50,'Viper #1 took off from Nellis AFB')('side you want to add points', sc1 + 'number of points you want to add to the score', 'description why you get the points')
ExNusquam
Posts: 531
Joined: Mon Mar 03, 2014 11:26 pm
Location: Washington, D.C.

RE: Lua - Trigger Altitude AND in inside Area

Post by ExNusquam »

Instead of using the GUID, you could use

Code: Select all

ScenEdit_UnitX()
to make the condition work more generally.
User avatar
kevinkins
Posts: 2465
Joined: Wed Mar 08, 2006 11:54 am

RE: Lua - Trigger Altitude AND in inside Area

Post by kevinkins »

UnitX() works well ...

Event = "Test"
Trigger = "SealsEnterArea"
Action = lua

alt=UnitX().altitude
if alt < 40 then
ScenEdit_MsgBox('GOOD Job 10 points awarded !!!', 1)
score = ScenEdit_GetScore("USAJapan")
score = score + 10
ScenEdit_SetScore ("USAJapan",score,"GOOD Job")
end

Note: lua altitude is in meters not feet.
“The study of history lies at the foundation of all sound military conclusions and practice.”
Alfred Thayer Mahan
butch4343
Posts: 327
Joined: Thu Mar 26, 2015 2:09 pm

RE: Lua - Trigger Altitude AND in inside Area

Post by butch4343 »

ORIGINAL: kevinkin

UnitX() works well ...

Event = "Test"
Trigger = "SealsEnterArea"
Action = lua

alt=UnitX().altitude
if alt < 40 then
ScenEdit_MsgBox('GOOD Job 10 points awarded !!!', 1)
score = ScenEdit_GetScore("USAJapan")
score = score + 10
ScenEdit_SetScore ("USAJapan",score,"GOOD Job")
end

Note: lua altitude is in meters not feet.


Outstanding!

I was pondering how to score based on a units height just the other week mate, so thanks for that!

Cue 20 posts on where I have gone wrong in the next few days lol

Butch
butch4343
Posts: 327
Joined: Thu Mar 26, 2015 2:09 pm

RE: Lua - Trigger Altitude AND in inside Area

Post by butch4343 »

Guys,

Can I ask for some help on this one?

Im dead interested in this script, but I want to make it work for a slighlty different outcome, I wonder if someone can explain some LUA magic to me.

So I have a scenario where I have a SAM battery thats sitting in East Germany, its an SA-5 Site, so its got a far reach into the FRG, I wanted to set a script for so that NATO aircraft will trigger a SAM engagement if the aircraft is above say 5500 feet.

So my thinking is that I would have a trigger that specifically is tripped when NATO aircraft enter the area.

Then the action would be LUA script

alt=UnitX().altitude
if alt < 1500 then


So am i right in thinking this is the part of the script that would "check" the height of the aircraft that tripped the trigger?

So the next part would be the bit I kinda need explaining to get my head around, I was thinking that I could use.

ScenEdit_AttackContact(attackerID, contactId ,{mode='1', mount=438, weapon=1413, qty=10}) -- alloc 10 gunfire

I got this from the Command LUA documentation.

The attacker ID, is that just the name of the unit in my case, SAM1?

Contact ID, how would I know the contactID as I dont know what aircraft the player will choose to use?

The mount and weapon numbers that will be available in the database viewer?

So the script should look something like

alt=UnitX().altitude
if alt < 1500 then
ScenEdit_AttackContact(attackerID, contactId ,{mode='1', mount=XXX, weapon=XXX, qty=X})


Do I need a script such as : unit = ScenEdit_UnitX() in order to get the contact ID for the engagement?

Sorry if this seems dumb, but Im trying to understand LUA better.

Regards

Butch
User avatar
michaelm75au
Posts: 12463
Joined: Sat May 05, 2001 8:00 am
Location: Melbourne, Australia

RE: Lua - Trigger Altitude AND in inside Area

Post by michaelm75au »

Contact ID is how the other side sees the unit triggering the event.

Sample how it is used.
-- the side doing the detecting
local u = UnitY()
-- unit that triggered event
local contact = UnitX()
-- this unit seen as a contact by others
local con = contact.ascontact
-- not generated as a contact yet
if #con ~= 0 then
-- listed as a contact by some side
-- what is the side id for the base unit
local side = ScenEdit_GetSideOptions({side=u.side}).guid
-- find the side's entry in the triggering unit's contact list
for x = 1, #con do
if con[x].side == side then
-- contact on my side
local mycontact = con[x].guid
-- distance between my base unit and the contact
local dist = u:rangetotarget( mycontact)
if dist < 10 then
print('target in range (' .. dist ..'NM)')
ScenEdit_SetSidePosture( ScenEdit_GetSideOptions({side=contact.side}).guid, side ,'H')
answer=true
else
print('target not in range (' .. dist ..'NM)')
end
end
end
end

This is from my notes. Hope it is useful...I haven't tried this for awhile and not able to validate exactly at the moment. But it should give you idea on how to get the unit as a contact for the side that want to shoot it.
Michael
butch4343
Posts: 327
Joined: Thu Mar 26, 2015 2:09 pm

RE: Lua - Trigger Altitude AND in inside Area

Post by butch4343 »

Michael

Thanks so much for this, this really helsps me get to grips with it.

Thanks

Butch
Post Reply

Return to “Lua Legion”