Jump to content

Lua scripting : monitoring static object status ?


Recommended Posts

Posted (edited)

Hi Guys,

 

I would like to check the status of some static objects and in particular to use the S_EVENT_DEAD event to check if it is destroyed and which one it is. Although I am able to catch the event I am unable to get name of the "object" that is detroyed...

Edit : just to make things clear and as I have a doubt about what DCS is considering as a static and a scenery object, I put a "red flag" named let's say "whatever" somewhere in the DCS world and would like to get the information when it is destroyed the coalition it belongs to and who destroyed it i.e. somethinhg like that :"Whatever" (red camp) has been destroyed by "whoever"

I tried to apply the mecanism I use for exemple to get the name of a pilot that take off (with the unit:getName() ) or it's flight name (with unit:getGroup():getName() with unit define as unit = event.initiator) but it doesn't work in that case...

 

What am I doing wrong ?

How to do it ?

 

Thanks in advance.

Edited by CougarFFW04
Posted (edited)

I've tested this using MOOSE and noticed that statics can't be captured using EventData.initiator (since that returns a table value...in MOOSE, at least).

 

However, I managed to capture them using EventData.IniUnitName instead (except for the new "sea shelf object" statics, which only seem to work with EventData.IniObjectCategory )

 

I've attached a MOOSE demo mission + script.

(Whenever a static object is destroyed, you'll get a text message indicating its ME name, type and category. In the case of sea shelf objects, only their category will be indicated)

 

Here's the script:

 

local StaticSet = SET_UNIT:New()

:FilterPrefixes("Static_") -- All relevant statics in ME must use this prefix!

:FilterStart()

 

StaticSet:HandleEvent(EVENTS.Dead)

 

function StaticSet:OnEventDead(EventData)

 

local DeadStatic = EventData.IniUnitName -- This doesn't work for Sea Shelf Objects

local DeadStaticType = EventData.IniTypeName -- This doesn't work for Sea Shelf Objects

local DeadStaticCategory = EventData.IniObjectCategory -- 3 = Ground Vehicle/Plane/Helicopter/Structure , 5 = Airport structure, 6 = Cargo, 0 = Sea Shelf Object

 

 

if DeadStaticCategory ~= 0

then

MESSAGE:New("Dead initiator object ME name --> "..DeadStatic.."\nDead initiator object type --> "..DeadStaticType.."\nDead initiator object category --> "..DeadStaticCategory,10):ToAll()

 

elseif DeadStaticCategory == 0

then

MESSAGE:New("Sea shelf object destroyed!\nDead initiator object category --> "..DeadStaticCategory,10):ToAll()

end

end

 

 

You can use this capture method to create elaborate "alive checks" for each static object (I'd use flags and name checks for that, I guess).

 

Hope it helps! :thumbup:

 

PS: Check out the new Su34 model in the mission, it looks great!

OnEventDead Static Test.miz

OnEventDead initiator test.lua

Edited by Hardcard
Posted (edited)

Hi HardCard,

 

Thanks for your help althought as I do not use Moose and would like to use only DCS things for "simple" (not so simple actually as I failed) stuff I am confused and still not yet sucessfull to run it.

 

Where is this IniUnitName documantation... I cant find anything about that in the wiki... is this a Moose thing ora standart DCS one...

 

I am also a little bit confused with DCS convention : what is the difference between a static object and a scenery object ? When I spawn let's say a red flag somewhere in DCS world is this a static or scenery object ?

 

Still digging to have this simple thing (getting the name and coalition (red or blue) of an object (actually a red flflag) when it is destroyed) working :joystick:

Edited by CougarFFW04
Posted (edited)

Where is this IniUnitName documantation... I cant find anything about that in the wiki... is this a Moose thing ora standart DCS one...

 

It's in the MOOSE documentation, specifically, Core.Event section

 

Yes, it's a "MOOSE thing", but keep in mind that all frameworks use DCS scripting engine functionalities in the end. Frameworks are meant to make things easier, not harder :smilewink:

 

 

I am also a little bit confused with DCS convention : what is the difference between a static object and a scenery object ? When I spawn let's say a red flag somewhere in DCS world is this a static or scenery object ?

 

I don't know that either. All I can tell you based on my tests is that flags belong to category 3, just like static airplanes, helicopters, vehicles, ships and structures.

 

Still digging to have this simple thing (getting the name and coalition (red or blue) of an object (actually a red flflag) when it is destroyed) working :joystick:

 

I've added a couple of flags for each coalition to the demo mission (on the runway), also, I've expanded the script so it'll return the coalition value for destroyed statics as well (except for sea shelf objects).

1 = Red coalition, 2 = Blue coalition.

 

Expanded script:

 

local StaticSet = SET_UNIT:New()

:FilterPrefixes("Static_") -- All relevant statics in ME must use this prefix!

:FilterStart()

 

StaticSet:HandleEvent(EVENTS.Dead)

 

function StaticSet:OnEventDead(EventData)

 

local DeadStatic = EventData.IniUnitName -- This doesn't work for Sea Shelf Objects

local DeadStaticType = EventData.IniTypeName -- This doesn't work for Sea Shelf Objects

local DeadStaticCategory = EventData.IniObjectCategory -- 3 = Ground Vehicle/Plane/Helicopter/Structure , 5 = Airport structure, 6 = Cargo, 0 = Sea Shelf Object

local DeadStaticCoalition = EventData.IniCoalition -- 1 = Red , 2 = Blue (This doesn't work for Sea Shelf Objects either!)

 

if DeadStaticCategory ~= 0

then

MESSAGE:New("Dead initiator object ME name --> "..DeadStatic.."\nDead initiator object type --> "..DeadStaticType.."\nDead initiator object category --> "..DeadStaticCategory.."\nDead initiator object coalition --> "..DeadStaticCoalition,10):ToAll()

 

elseif DeadStaticCategory == 0

then

MESSAGE:New("Sea shelf object destroyed!\nDead initiator object category --> "..DeadStaticCategory,10):ToAll()

end

end

 

 

I put a "red flag" named let's say "whatever" somewhere in the DCS world and would like to get the information when it is destroyed the coalition it belongs to and who destroyed it i.e. somethinhg like that :"Whatever" (red camp) has been destroyed by "whoever"

 

As far as I can tell, event DEAD will only give you access to information about the static object that was destroyed, it won't tell you who destroyed it (I'm not 100% sure, though).

If you want to know who destroyed it, you'll have to use event SHOT instead. This will complicate things, but it's doable.

OnEventDead Static Test.miz

OnEventDead initiator test.lua

Edited by Hardcard
Posted (edited)

Ok, after some more tweaking and testing, I've managed to create a script that does exactly what you asked (it uses event HIT instead of event SHOT, btw).

 

I've attached the modified demo mission + script.

 

Whenever a static object is destroyed, you'll receive a message indicating its ME name, the coalition it belongs to and the ME unit name of the aircraft that destroyed it.

 

Here's the modified script:

EDIT: StaticSet was using the wrong class, I fixed it and uploaded the corrected files

 

 

local RedSet = SET_GROUP:New()

:FilterPrefixes("Red_") -- All relevant attack groups in ME must use this prefix!

:FilterStart()

 

local StaticSet = SET_STATIC:New()

:FilterPrefixes("Static_") -- All relevant statics in ME must use this prefix!

:FilterStart()

 

RedSet:HandleEvent(EVENTS.Hit)

 

function RedSet:OnEventHit(EventData)

Attacker = EventData.IniUnitName -- This will capture the initiator of the HIT event. We'll use the global variable "Attacker" in the message objects below, to indicate the specific aircraft unit that dealt the "killing blow"

end

 

StaticSet:HandleEvent(EVENTS.Dead)

 

function StaticSet:OnEventDead(EventData)

 

local DeadStatic = EventData.IniUnitName -- This doesn't work for Sea Shelf Objects

local DeadStaticCategory = EventData.IniObjectCategory -- 3 = Ground Vehicle/Plane/Helicopter/Structure , 5 = Airport structure, 6 = Cargo, 0 = Sea Shelf Object

local DeadStaticCoalition = EventData.IniCoalition -- 1 = Red , 2 = Blue (This doesn't work for Sea Shelf Objects either!)

 

if DeadStaticCategory == 3 or DeadStaticCategory == 6 -- I've restricted this part of the logic to categories 3 and 6, since the relevant static objects belong to these

then

if DeadStaticCoalition == 1 -- Remember, 1 = Red coalition

then

MESSAGE:New(DeadStatic.." (Red coalition) has been destroyed by "..Attacker,10):ToAll()

 

elseif DeadStaticCoalition == 2 -- Remember, 2 = Blue coalition

then

MESSAGE:New(DeadStatic.." (Blue coalition) has been destroyed by "..Attacker,10):ToAll()

end

 

elseif DeadStaticCategory == 0 -- Remember, 0 = Sea shelf object

then

MESSAGE:New("Sea shelf object destroyed by "..Attacker,10):ToAll()

end

end

OnEventDead Static Test v3 (corrected).miz

OnEventDead initiator test (corrected).lua

Edited by Hardcard
Posted

Hi Hardcard,

 

Ok, after some more tweaking and testing, I've managed to create a script that does exactly what you asked (it uses event HIT instead of event SHOT, btw).

 

I've attached the modified demo mission + script.

 

Whenever a static object is destroyed, you'll receive a message indicating its ME name, the coalition it belongs to and the ME unit name of the aircraft that destroyed it.

 

I came to a similar conclusion (using HIT instead of DEAD and reading the life point) and I got it.

Thanks a lot because our discussion pointed me in the right direction :thumbup:

 

I have just downloaded your script (and mission) and will compare/mix with what I did.

 

Thanks again :smilewink: You rock :thumbup:

Posted (edited)

Glad I could help!

 

I came to a similar conclusion (using HIT instead of DEAD and reading the life point) and I got it.

 

Careful with that method, I don't recommend dispensing with DEAD events.

 

From what I've seen in my tests, sometimes the static objects are destroyed even though their last HIT life readings are greater than 0 (their life becomes 0 after the HIT reading happens, I think).

 

For instance, in my percentage test mission (from the other thread) you'll notice that Tarawa is destroyed even when its last HIT life reading is ~5%.

 

So (if I understand what you're doing), if you make a check like...

function Attacker:OnEventHit(EventData) 
if Tarawa:getLife() == 0 
then
Tarawa is ded!
end 
end

 

...it might fail to detect the static's destruction in situations where its last HIT life reading isn't 0.

 

That's why I'd rather use the DEAD event, you can't go wrong with that one. :thumbup:

Edited by Hardcard
  • 1 month later...
Posted

Hardcard,

I was trying out your OnEventDead scrip and it all works fine, and thanks for sharing. But (always a but) if Mist is loaded it doesn't want to play well.

 

I was going to use your script it in one of my missions but I couldn't get it to work. So I went back to your demo miz from above and added a new MISSION START -> load Mist (4.3.74) and changed nothing else and then ran you mission, (just to make sure it wasn't something I was doing in my mission)

 

I would get the "dead message" for the first 4 targets and then nothing.The debriefing shows about 20 dead targets

 

Take Mist out and everything back to normal.

I would almost swear I have used Mist and Moose together before with out any problems. This is on the latest OB.

 

Is there something else I could try ?

Posted (edited)

@Olddog

 

Post the missions that are giving you trouble here.

 

Also, if you provide the specific script requirements, it'll be easier for others to contribute with their own solutions. I might be able to write a "standard" DCS script for you (as soon as I beat this nasty flu :cold:)

Edited by Hardcard
Posted

Hardcard,

First, I hope you get over the Flu soon, that can really suck.

 

This is your .miz with Mist added and nothing else. You will get the first 4 or 5 destroyed messages and then no more messages when the rest of the static targets are destroyed. When you get time, give it a run through and see what you think.

 

The first targets are destroyed at the exact same time so the OnEvent message is triggered once, and then seems to just quit.

 

Just for the heck of it I separated the targets on the runway with more room between them and only got two destroyed messages.(both at the same time)

 

In all cases, all the targets were destroyed, just didn't get the messages.

Again this only happens when Mist is loaded, with just Moose everything is as advertised.

 

I'm using Mist for all the other usual stuff and is the only reason I brought this up.

The info from your script would be nice to have.

OnEventDead Static Test v3 (corrected)_mist.miz

Posted (edited)

There seems to be a problem with event handlers when combining MOOSE and MIST.

 

No worries, though, here you have a "standard" DCS script that doesn't seem to conflict with MIST (also, it doesn't require name prefixes in ME, since it targets all statics and cargo objects in the mission):

 

 

local Event_Handler = {}

 

function Event_Handler:onEvent(Event)

 

if Event.id == 2 then -- If it's a HIT event

 

local InitiatorHITObject = Event.initiator

InitiatorHITName = InitiatorHITObject:getName()

 

end

 

if Event.id == 8 and Event.initiator:getCategory() ~= nil then -- If it's a DEAD event and the initiator returns a DCS category

 

local InitiatorObject = Event.initiator

local InitiatorName = InitiatorObject:getName()

local InitiatorCategory = InitiatorObject:getCategory()

 

if InitiatorCategory == 3 or InitiatorCategory == 6 then

 

trigger.action.outText(InitiatorName.." has been destroyed by "..InitiatorHITName,5)

 

elseif InitiatorCategory == 0 then

trigger.action.outText("Sea shelf object destroyed by "..InitiatorHITName,5)

end

end

end

 

world.addEventHandler(Event_Handler)

 

 

Now, there's some minor nonsense going on with sea shelf objects, but it shouldn't be a problem.

Also, keep in mind that this script will generate a message for EVERY static/cargo object destroyed in the mission. If you want to target only specific objects, you'll need to add name checks as well.

Edited by Hardcard
Posted

Thanks Hardcard, I appreciate your help , that script works just fine and it is a plus that the static names don't have to be changed.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...