How to use Lua Script to mark a contact hostile?

All discussions & material related to Command's Lua interface

Moderators: angster, RoryAndersonCDT, michaelm75au, MOD_Command

Post Reply
gf85
Posts: 7
Joined: Thu May 24, 2018 6:20 am

How to use Lua Script to mark a contact hostile?

Post by gf85 »

Greeting, this is my first post here and I am new to CMNAO and Lua script, so sorry for dumb question.[:D]

I am learning to make a scenario, as seen in the picture, I am trying to simulate a Myanmar aircraft (Contact Attack #11) violate Chinese airspace. In my plan, an event should fire later changing the posture btw the two sides: "Myanmar" and "PLAAF" to hostile and hopefully, two J-7 that chasing the contact will initiate an attack.

But then I find it doesn't work well. After event fired the posture btw Myanmar and PLAAF do become hostile (Myanmar facilities do turn red), but the contact remains unfriendly. Then I find out it's because of the contact's side is not yet identified by PLAAF, remain unknown, so it wasn't marked hostile immediately after the event fired.

My question is can I manually get the contact's side for PLAAF or directly assign the contact as hostile though Lua script? I have looked for a while but can't find a handy function that seems to fit my need. Thanks!




Image
Attachments
cc9.jpg
cc9.jpg (600.7 KiB) Viewed 261 times
User avatar
NimrodX
Posts: 82
Joined: Sat May 12, 2018 8:34 pm
Contact:

RE: How to use Lua Script to mark a contact hostile?

Post by NimrodX »

My experience with this is limited but check these out:

https://commandlua.github.io/index.html#ScenEdit_UnitX (to get the unit that fired the event)

https://commandlua.github.io/index.html#Unit (use ascontact to get the contact table....)

https://commandlua.github.io/index.html#Contact (you can't set classificationlevel but you can set posture it looks like)

Use Lua console to examine the data and test out setting it.
gf85
Posts: 7
Joined: Thu May 24, 2018 6:20 am

RE: How to use Lua Script to mark a contact hostile?

Post by gf85 »

Thanks for NimrodX's answer, but I still don't know how to figure it out[:(]

I do have the unit's ID (it's added through scenario editor, not by code),
I have tried to set the unit's autodetectable = true but doesn't work

ScenEdit_SetUnit ({side="Tatmadaw", unitname="Recon AC", autodetectable= true})

and I can't find a function that can set the property of contact...
User avatar
Gunner98
Posts: 5881
Joined: Fri Apr 29, 2005 12:49 am
Location: The Great White North!
Contact:

RE: How to use Lua Script to mark a contact hostile?

Post by Gunner98 »

gf85

I am in now way, shape or forum the Lua expert. But I think I can help with this one.

My 'GoTo' guide to help is here: http://commandlua.github.io/

by running through the functions you can find:

ScenEdit_SetUnitSide (sidedesc)
Changes the side of a unit
Parameters:
sidedesc SideDescription The sides to change for the unit. Group will change attached units

The page is very well hyperlinked so you can follow up on some of the syntax

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/
User avatar
NimrodX
Posts: 82
Joined: Sat May 12, 2018 8:34 pm
Contact:

RE: How to use Lua Script to mark a contact hostile?

Post by NimrodX »

ORIGINAL: gf85

Thanks for NimrodX's answer, but I still don't know how to figure it out[:(]

I do have the unit's ID (it's added through scenario editor, not by code),
I have tried to set the unit's autodetectable = true but doesn't work

ScenEdit_SetUnit ({side="Tatmadaw", unitname="Recon AC", autodetectable= true})

and I can't find a function that can set the property of contact...

Ok, basically the problem is this:

There's a "Unit" and there's a "Contact". These are different tables.

A Unit is a table that describes what the unit physically is in reality according to the universe. There's only one Unit for a unit.

A Contact is what a specific side *thinks* the unit is. So there's actually more than one Contact for the same unit. If we have sides A, B, and C then Unit X will have three Contacts, one for "what side A thinks X is", one for "what side B thinks X is", and one for "what side C thinks X is".

So there will be a unit called X. Then there will be a Contact for what side PLAAF thinks X is and totally separate table for what every other side thinks X is.

I'm pretty sure I know how to code this but it would save me some time if you could upload your scenario file as you have it now so that I can just open the event editor and have everything else set up already. I don't think there's any function for this, you just need to grab the Contact table for PLAAF's "view" of the Unit and set the table member by key using the table[key] = "xxx" syntax sort of like this:

local contact = ScenEdit_GetContact({side="United States", guid="c4114322-900c-428d-a3e3-0af701e81a7a"})
contact["posture"] = "hostile"

Anyway, post or PM me your scenario file and I can show you.
Rory Noonan
Posts: 2418
Joined: Thu Dec 18, 2014 1:53 am
Location: Brooklyn, NY

RE: How to use Lua Script to mark a contact hostile?

Post by Rory Noonan »

Hey gf85,

As I understand it the problem you're running into is that an aircraft that is marked unfriendly (presumably by an exclusion zone) is not being fired on after its side turns hostile, which is because there is no positive ID.

It seems like a simple solution would be to change the RoE for the side you want to do the shooting to 'FREE' to allow them to attack without making a positive ID.

Give that a try and let us know how it goes.


If that doesn't work, you're definitely on the right track with the autodetect idea. Some sample code is below, try changing the values to suit your scenario:
ScenEdit_SetUnit({name='P-3B Orion AEW', guid='27f595a4-14b5-49aa-968c-cf59048f8e73',autodetectable=true})
A quick way to get the name and GUID of a unit is to click it and press Ctrl+C; that will put the data on the clipboard ( {name='P-3B Orion AEW', guid='27f595a4-14b5-49aa-968c-cf59048f8e73'} in the above example was collected by this method)

Also be aware that autodetection can take up to a minute to take effect.
Image
User avatar
NimrodX
Posts: 82
Joined: Sat May 12, 2018 8:34 pm
Contact:

RE: How to use Lua Script to mark a contact hostile?

Post by NimrodX »

It's also possible to make an exclusion zone which doesn't require any code at all, but this isn't really ideal.
gf85
Posts: 7
Joined: Thu May 24, 2018 6:20 am

RE: How to use Lua Script to mark a contact hostile?

Post by gf85 »

Thank you for NimrodX, Gunner98, and moderator kindly reply. [;)]

Sorry for late reply, I will try the suggest code later.
Sure I have read Gunner98's guide, it's help me a lot.[;)]
Attached with my scenario any(still very beginning though)
I possibly will have to ask more dumb question later on[:D]
Attachments
KoKangConflict2015.zip
(58.19 KiB) Downloaded 8 times
gf85
Posts: 7
Joined: Thu May 24, 2018 6:20 am

RE: How to use Lua Script to mark a contact hostile?

Post by gf85 »

autodetectable works but as apache85 said it takes some time to take effect.
Then I tried NimrodX's code and it works, only have to change the line contact["posture"] = "H", "hostile" seems not work.
Thanks!
User avatar
NimrodX
Posts: 82
Joined: Sat May 12, 2018 8:34 pm
Contact:

RE: How to use Lua Script to mark a contact hostile?

Post by NimrodX »

Oops, sorry for the misinfo there. I think the documentation kind of lied to me, or maybe "Hostile" or 3 or something works in some cases or not others. Anyway, here's what I was talking about:
-- We might not know what unit it is! But we can find it easily with this.
local contact = ScenEdit_UnitC()
print(contact)
-- No matter what it is, now it will be considered hostile.
contact.posture = 'H'

local msg = string.format([[Your tell your pilots: "I don't care what this %s is! I order you to consider it hostile considering where you found it!"]], contact)

ScenEdit_SpecialMessage("PLAAF", msg)

If you do it like that you can change what unit will trigger it simply by editing the event trigger to specify a vague range of contacts or a specific unit without even editing the code.

The attached file will quickly result in an ID and posture change once it is unpaused.
Attachments
KoKangCon..5edited.zip
(63.64 KiB) Downloaded 12 times
gf85
Posts: 7
Joined: Thu May 24, 2018 6:20 am

RE: How to use Lua Script to mark a contact hostile?

Post by gf85 »

Nevermind, I get the right function anyway.[;)]
And thanks for the help I can make it now.
Post Reply

Return to “Lua Legion”