Jump to content

LUA - Identify specific ship?


Recommended Posts

Hi everyone,

I'm writing a script that will do an action based on a specific ship firing missiles or guns (or torpedoes, etc.), and I'm not sure how to identify an individual ship by group or unit, but not by a ship type. I believe I know how to identify a specific aircraft, though, like this:

Unit.getByName('XYZ')

Hoggitworld seems to be down, or at least I can't get to it, and I know it's a good resource for this kind of thing. But until I'm able to get there, does anyone have any suggestions? I'll keep digging around in the meantime.

EDIT

I poked around a bit and found that someone used Unit.getByName for ships, too, but it didn't work for me. With aircraft, it seems to use the aircraft callsigns, which of course don't exist for ships. I created custom group and unit names for my test ship and nothing happened. For the ship, I used...

world.event.S_EVENT_SHOOTING_START (appears to be for aircraft guns)

and

world.event.S_EVENT_SHOT (appears to be for aircraft missiles and bombs?)

Neither worked for a ship firing missiles or guns (CG Ticonderoga in this case).

Aside from all that, I have a feeling there'd be a market for a book, real or virtual, about DCS LUA scripting.

END EDIT

Thanks,

Dave


Edited by DaveSD
Link to comment
Share on other sites

From the wiki:

The SHOOTING START event table is in the following format.

Event = {
  id = 23,
  time = Time,
  initiator = Unit,
  target = Object,
  weapon_name = string
}


So that means that the 'initiator' is the unit doing the shooting. There will probably be lots of this event going on
 (depending on the mission and what's going on),
 so you'll need to have a test to see if the unit is the same as the ship you're looking for.

And 
local thisUnit = Unit.getByName("Cargo-20-1") 
Will get the ship you want. Then just compare


if event.initiator == thisUnit then
  
  ...
  
end



naval.jpg

Link to comment
Share on other sites

59 minutes ago, DaveSD said:

world.event.S_EVENT_SHOT

You are on the right track. That callback will (as part of) event tell you which unit (including naval units and ground units) fired a weapon, with the exception of guns, which is as you already know shooting_start

The event table holds most of the information you are looking for, there is no need to use Unit.getByName() when you get the event, IIRC it's event.initiator that holds who fired, event.target at whom, and event.weapon what kind of weapon.

-ch

 

Link to comment
Share on other sites

5 hours ago, Elphaba said:

From the wiki:

The SHOOTING START event table is in the following format.

Event = {
  id = 23,
  time = Time,
  initiator = Unit,
  target = Object,
  weapon_name = string
}


So that means that the 'initiator' is the unit doing the shooting. There will probably be lots of this event going on
 (depending on the mission and what's going on),
 so you'll need to have a test to see if the unit is the same as the ship you're looking for.

And 
local thisUnit = Unit.getByName("Cargo-20-1") 
Will get the ship you want. Then just compare


if event.initiator == thisUnit then
  
  ...
  
end
 

naval.jpg

Thanks Elphaba! I have some questions (I'm looking through the Wiki but there's so much there I get lost.) I'm going to try some experiments using what you've told me, but I'm going that tomorrow - tonight I'm dedicated to fighting off a nice case of bronchitis, and it's hard to concentrate.

So the questions...

For id = 23, is that a value I specify?

For time = Time, is that a delay I can set, optional perhaps?

Is it necessary to specify a target? I currently have units set to attack targets via advanced waypoint actions. Eliminate those actions and go strictly with LUA in this case?

weapon_name could be AnyWeapon? (See? I did get that from the Wiki!)

I think I need to sit my rump down and commit to reading at least one wiki page about scripting per day. Right now I'm doing it piecemeal. When I was doing the same with some web scripting languages (JavaScript, PHP, etc.) it was easier and quicker to see what was happening. Not a complaint about DCS, just a difference.

Link to comment
Share on other sites

5 hours ago, cfrag said:

You are on the right track. That callback will (as part of) event tell you which unit (including naval units and ground units) fired a weapon, with the exception of guns, which is as you already know shooting_start

The event table holds most of the information you are looking for, there is no need to use Unit.getByName() when you get the event, IIRC it's event.initiator that holds who fired, event.target at whom, and event.weapon what kind of weapon.

-ch

 

Thanks cfrag! I have the DCS wiki open right now and I see references to various things but nothing for event table. I assume it's in a category I haven't examined, but I took at look through the Part 1 items and a few of the Objects in Part 2. Can you tell me where I can find it? All my internal cerebral NAV stuff is dead.

I just now saw that Hoggitworld is back up (or at least available to me again). When I first posted my initial question I wasn't able to get there. I'll check it to see if it's got an event table.

Dave

Link to comment
Share on other sites

Events are generated by DCS and represent things that happen. You don't set any value with them, but you can run other code to get information about the event or for reference purposes. Every single event has an id and time value telling you what the event is and when it happened. From there the additional contents of the event depend on what it is. Most will have an initiator value which corresponds to the unit that caused the event. initiator, target, place, and weapon are all going to be objects and have access to whichever sub-class of objects they are. 

Check the example provided for the shot event. https://wiki.hoggitworld.com/view/DCS_event_shot It directly uses the initiator and weapon from the event table to get information about it. Then adds that information to a table entry which is ultimately a fancy way to concatenate text. 

 

The right man in the wrong place makes all the difference in the world.

Current Projects:  Grayflag ServerScripting Wiki

Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread)

 SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum

Link to comment
Share on other sites

2 hours ago, Grimes said:

Events are generated by DCS and represent things that happen. You don't set any value with them, but you can run other code to get information about the event or for reference purposes. Every single event has an id and time value telling you what the event is and when it happened. From there the additional contents of the event depend on what it is. Most will have an initiator value which corresponds to the unit that caused the event. initiator, target, place, and weapon are all going to be objects and have access to whichever sub-class of objects they are. 

Check the example provided for the shot event. https://wiki.hoggitworld.com/view/DCS_event_shot It directly uses the initiator and weapon from the event table to get information about it. Then adds that information to a table entry which is ultimately a fancy way to concatenate text. 

 

Ah, I"m starting to get it. I thought an event table was literally a human-compiled, readable list of potential events in DCS, rather than a collection of data generated on-the-fly (to some degree, at least, like the ID and time) by DCS. The other data in the examples is pulled in from what the user has specified, such as unit name. Thanks for the assistance!

Link to comment
Share on other sites

7 hours ago, DaveSD said:

I thought an event table was literally a human-compiled, readable list of potential events in DCS

My apologies for being obtuse. 'Table' in Lua refers to any structured data (i.e. anything other than nil, bool or number (official docs may also list some more arcane stuff). A Lua Table is structured data, essentially like an array or dictionary in other languages (another note: although Lua may count 'string' as a basic type, we all know that it's an array, therefore a specific instance of a table)

So when you implement the myScript:onEvent(event) method in your script, the variable <event> will contain a structure with many attributes that hold (and often not, some pitfalls to avaid here, so bring your guards!) the information that you are looking for.

Have fun with DCS scripting and be aware that you are messing with a highly addictive subject.

-ch 

 

Link to comment
Share on other sites

2 hours ago, cfrag said:

My apologies for being obtuse. 'Table' in Lua refers to any structured data (i.e. anything other than nil, bool or number (official docs may also list some more arcane stuff). A Lua Table is structured data, essentially like an array or dictionary in other languages (another note: although Lua may count 'string' as a basic type, we all know that it's an array, therefore a specific instance of a table)

So when you implement the myScript:onEvent(event) method in your script, the variable <event> will contain a structure with many attributes that hold (and often not, some pitfalls to avaid here, so bring your guards!) the information that you are looking for.

Have fun with DCS scripting and be aware that you are messing with a highly addictive subject.

-ch 

 

Nah, you weren't obtuse, you were using standard language for the topic. For me, learning what it means is another step toward LUA enlightenment! I can see that it will eventually give me the ability to create an infinite number of scenarios. I'm not good enough at flying yet to keep from embarrassing myself in multi-player events, so scripting will give me the next best thing for a solo player. (I hate to use the terms "play" and "player," because DCS is so far removed from being a game.)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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