bunkerjunker Posted November 10, 2023 Posted November 10, 2023 I'm working on a mission where I would like to have a script to tally the tasking BDA's when triggered based on Unit Name if possible. For example, when the tasking ends, I would like to be able to have the Units categorized and reported by the category ie Tactical Units, Vehicles, Tanks, Emplacement, etc. Units are named starting with TF or VEH. Here is the code I've been working hours to try to get to work, but my limited scripting ability is showing. Any advice/help would be much appreciated. Spoiler function DeadUnitsList:OnEventKill(EventData) if EventData and type(EventData) == "table" then local DEADUNITNAME = EventData.IniDCSUnitName local FIGHTERCount = 0 local vehicleCount = 0 if DEADUNITNAME == "^TF" then FIGHTERCount = FIGHTERCount + 1 elseif DEADUNITNAME == "^VEH" then vehicleCount = vehicleCount + 1 end if FIGHTERCount > 0 then trigger.action.outText("Fighters destroyed: " .. FIGHTERCount, 15) end if vehicleCount > 0 then trigger.action.outText("Vehicles destroyed: " .. vehicleCount, 15) end end end timer.scheduleFunction(function() DeadUnitsList:OnEventKill({ IniDCSUnitName = "TestUnit" }) -- Simulate an event for testing end, {}, timer.getTime() + 0.51)
cfrag Posted November 10, 2023 Posted November 10, 2023 1 hour ago, bunkerjunker said: Here is the code I've been working hours to try to get to work, but my limited scripting ability is showing. You maybe should also tell us what is not working for you or what you expected, and what you got instead -- so we can home in on those issues. By taking a look at the code, it appears that you are using a framework (MOOSE?), and since I'm not conversant in that particular framework, much of what I'm saying below may be obsolete. But here's my initial take: 2 hours ago, bunkerjunker said: local FIGHTERCount = 0 If you are attempting to collect data over any amount of time, using a 'local' variable will not work because their values are transient, they vanish when the function is exited. Since you initialize it to 0 anyway, that is moot, but I just wanted to remark on that given the goal of your code is to summarize data 2 hours ago, bunkerjunker said: if DEADUNITNAME == "^TF" then Unless the "==" operator is overloaded for your mission, "^TF" will not return true if the string DEADUNITNAME begins with "TF". In Lua, the equivalent would be if string.sub(DEADUNITNAME, 1, 2) == "TF" then I'm sorry I can't help you more until you go into deeper details about what part of your code is not working for you, and which framework you are using. Hope this helps, -ch
bunkerjunker Posted November 10, 2023 Author Posted November 10, 2023 8 hours ago, cfrag said: I'm sorry I can't help you more until you go into deeper details about what part of your code is not working for you, and which framework you are using. Thanks for the input. As I said I'm very new to lua scripting and so your explanations taught me something already. I'm not using any framework at this time. I have been trying to create this mission using default .api as much as possible. I pick and choose things I see work from others scripting and try to put them all together into some kind of functioning "frankenlua" and it doesn't work most of the time. I wish I understood scripting better, but I'm trying on my spare time to learn. The thing is, I actually enjoy the process and the learning. As for the part of the code that isn't working, It is not recognizing the killed units and when I call it. Every adjustment I have made has always returned "0" units killed, even though the DCS event log clearly shows the kills. Perhaps what I need is just some guidance on how to get the script to read the DCS events for that specific period of time, looking for those units.
cfrag Posted November 10, 2023 Posted November 10, 2023 (edited) 1 hour ago, bunkerjunker said: I'm not using any framework at this time. I have been trying to create this mission using default .api as much as possible. Understood. In that case, most of the code that you have written may be "contaminated" with code that was created for scripts that are based on frameworks. This happens a lot if you are using chat bots to 'help' you create DCS scripts; those bots are singularly inept at creating usable code for DCS, so my advice is never to go down that road, it will waste lots of your time. So let us begin with the event processor. Your approach to tap into DCS game events is indeed a good one. It's a mystery to me where you got the idea of an OnEventKill() method from (probably a script written for MOOSE), but that does not exist in plain vanilla DCS scripting. Hooking into the event loop takes two parts: a function to invoke on events, and one invocation to world.addEventHandler() to make it active. So let's begin. Set up a small 'program' (actually it's a table) that provides an entry for the event loop to call you back myCode = {} function myCode:onEvent(event) -- do event processing here. event.id contains the ID of -- the event. Depending on the event id, other table -- entries may or may not exist trigger.action.outText("Received event: " .. event.id, 30) end Above is a minimalistic table that defines a callable onEvent method (and it MUST be called 'onEvent') entry that you can pass to addEventHandler() So, add the following line to your code world.AddEventHandler(myCode) and run the entire script at mission start as a DOSCRIPT action. From that moment on, every time that an event occurs, your code is invoked as well. Since the code simply outputs the id to the screen, it's not much to write home about. but you now have a shell to work with. Next step is to look at a list of all the events that are defined, and then pick the ones that you are interested in, access the fields in the event record to gather data, and grow your code from there. That Hoggitworld site is your new best friend by the way. Once you understand how this things fit together, and have experimented a bit, your are probably 80% done writing what you set out to do. If you run into any issues along the way, come back here, show what you got, and I'm sure we can help you quickly. Just be advised that mission scripting can be highly addictive, so know what you are getting yourself into. Edited November 10, 2023 by cfrag 1
Recommended Posts