HiJack Posted April 3, 2014 Posted April 3, 2014 Thanks. OK I use Android so I'll test a couple of script apps to play with on the bus to work ;) I'm rewriting some of my functions to use the passed variable as a local variable inside the function then. Makes it better to read ;)
RagnarDa Posted April 3, 2014 Posted April 3, 2014 I'm rewriting some of my functions to use the passed variable as a local variable inside the function then. Makes it better to read ;) Just so we understand each other, this will not have any effect because the passed variable is already local in the function. DCS AJS37 HACKERMAN There will always be bugs. If everything is a priority nothing is.
xcom Posted April 24, 2014 Posted April 24, 2014 List of event tables that can be used in the event handler - 1:S_EVENT_SHOT { ["id"] = 1, ["weapon"] = table: 000000004DFEC200 { ["weapon"]["id_"] = 33591553, }, ["time"] = 43207.608, ["initiator"] = table: 000000004DFEC1B0 { ["initiator"]["id_"] = 16780801, }, } 2:S_EVENT_HIT { ["time"] = 43208.044, ["initiator"] = table: 000000004E0A3830 { ["initiator"]["id_"] = 16781569, }, ["target"] = table: 000000004E0A38D0 { ["target"]["id_"] = 16783617, }, ["id"] = 2, ["weapon"] = table: 000000004E0A3970 { ["weapon"]["id_"] = 33571073, }, } 3: S_EVENT_TAKEOFF { ["subPlace"] = 0, ["time"] = 43285.759, ["initiator"] = table: 000000002A87B270 { ["initiator"]["id_"] = 16786945, }, ["place"] = table: 000000004E091B00 { ["place"]["id_"] = 5000024, }, ["id"] = 3, } 4:S_EVENT_LAND { ["subPlace"] = 0, ["time"] = 43298.319, ["initiator"] = table: 000000004E0A3FE0 { ["initiator"]["id_"] = 16786945, }, ["place"] = table: 000000004E0A4030 { ["place"]["id_"] = 5000024, }, ["id"] = 4, } 5:S_EVENT_CRASH { ["id"] = 5, ["time"] = 43327.14, ["initiator"] = table: 0000000010198AA0 { ["initiator"]["id_"] = 16778497, }, } 6:S_EVENT_EJECTION { ["id"] = 6, ["time"] = 43445.22, ["initiator"] = table: 000000001135CE00 { ["initiator"]["id_"] = 16783105, }, } 8:S_EVENT_DEAD { ["id"] = 8, ["time"] = 43443.853, ["initiator"] = table: 000000004D77F7E0 { ["initiator"]["id_"] = 16786177, }, } 9:S_EVENT_PILOT_DEAD { ["id"] = 9, ["time"] = 43328.39, ["initiator"] = table: 000000004F207400 { ["initiator"]["id_"] = 16787201, }, } 12:S_EVENT_MISSION_END { ["id"] = 12, ["time"] = 43548.781, } 15:S_EVENT_BIRTH { ["subPlace"] = 13, ["time"] = 0, ["initiator"] = table: 0000000023F6E158 { ["initiator"]["id_"] = 16787201, }, ["place"] = table: 00000000240B6678 { ["place"]["id_"] = 5000024, }, ["id"] = 15, } 17:S_EVENT_ENGINE_STARTUP { ["subPlace"] = 0, ["time"] = 43310.108, ["initiator"] = table: 0000000023569E60 { ["initiator"]["id_"] = 16787201, }, ["place"] = table: 000000002356A320 { ["place"]["id_"] = 5000024, }, ["id"] = 17, } 18:S_EVENT_ENGINE_SHUTDOWN { ["id"] = 18, ["time"] = 43302.7, ["initiator"] = table: 000000002A823720 { ["initiator"]["id_"] = 16778753, }, } 19:S_EVENT_PLAYER_ENTER_UNIT { ["id"] = 19, ["time"] = 43278.899, } 22:S_EVENT_SHOOTING_END { ["id"] = 22, ["target"] = table: 000000004FBD9CD0 { ["target"]["id_"] = 16783873, }, ["time"] = 43278.5, ["initiator"] = table: 000000004FBD9B20 { ["initiator"]["id_"] = 16781569, }, } 23:S_EVENT_MAX { ["id"] = 23, ["time"] = 43277.456, ["initiator"] = table: 000000004FB60B30 { ["initiator"]["id_"] = 16780801, }, } This is handy to see when working with the event handler. [sIGPIC][/sIGPIC] BuddySpike Website | Live Map & Statistics BuddySpike Twitch Channel Buddyspike Discord Buddyspike Facebook
ENO Posted April 27, 2014 Author Posted April 27, 2014 Okay Xcom- sorry for me being me... but given that this is a beginner thread on purpose- can you tell me what the above does in terms an elementary school child would understand? Not meaning to sound condescending if that's how it comes across but to be honest these days I still feel that my grasp on some of this stuff- in spite of my best efforts to overcome- is still even WAY below that of a 5th grader. It sounds like you think that is useful- I guess my question to you (or another) is how does one apply what you have posted above? "ENO" Type in anger and you will make the greatest post you will ever regret. "Sweetest's" Military Aviation Art
xcom Posted April 28, 2014 Posted April 28, 2014 I think I'm on the same boat as you are ENO, I'm not a programmer and I'm trying to figure this stuff as I go. Because we needed some statistics drawn out for our campaign, I started looking at how I could get those statistics out. First I went and tried working with MIST cuz I remember that it has a table that keeps destroyed units information, then I decided to try it on my own and started to figure out how to use the event handler. Basically, The event handler picks up events, any events according to the following - world.event = { S_EVENT_SHOT, S_EVENT_HIT, S_EVENT_TAKEOFF, S_EVENT_LAND, S_EVENT_CRASH, S_EVENT_EJECTION, S_EVENT_REFUELING, S_EVENT_DEAD, S_EVENT_PILOT_DEAD, S_EVENT_BASE_CAPTURED, S_EVENT_MISSION_START, S_EVENT_MISSION_END, S_EVENT_TOOK_CONTROL, S_EVENT_REFUELING_STOP, S_EVENT_BIRTH, S_EVENT_HUMAN_FAILURE, S_EVENT_ENGINE_STARTUP, S_EVENT_ENGINE_SHUTDOWN, S_EVENT_PLAYER_ENTER_UNIT, S_EVENT_PLAYER_LEAVE_UNIT } The above returns a number not the name, it is explained in the scripting engine. Each of these events are logged by the event handler, the list I gave in the post before shows us how these events are written and what we can use in each event. Generic event (not all the fields are avaialble for all events, that's why i published the list in the post before) - Event = { id = enum world.event, time = Time, initiator = Unit, target = Unit, place = Unit, subPlace = enum world.BirthPlace, weapon = Weapon } Let's say you want to know when a unit has ejected and created at that spot an area for SAR, you would use the EJECTION event that the event handler catches and get the location with vec3 to know exactly where the ejection was. The event handler works constantly with any event, think of it kind of the same as in the mission editor trigger - Continues (On event...) To setup the event handler - local eventHandlerTable= {} function eventHandlerTable:onEvent(e) DO SOMETHING end world.addEventHandler(eventHandlerTable) This structure should run the function in anytime there's any of the events I wrote before, and that way you can do stuff when events occur, I for example write statistics when there are units/pilots dead among other things. Hope this helps, I know I was struggling with it a few weeks ago. 1 [sIGPIC][/sIGPIC] BuddySpike Website | Live Map & Statistics BuddySpike Twitch Channel Buddyspike Discord Buddyspike Facebook
ENO Posted April 28, 2014 Author Posted April 28, 2014 Okay... so now what I'm going to try and do is take the information you've just given me and apply it to a script I know enables some of these same techniques... in particular I think I'll scan through the CSAR script... as well as look over those scripts where I was trying to capture ejection events... And with luck I'll be able to learn a little bit about how to apply that information. It's frustrating since I just don't ever have enough time to focus on it and be consistent enough with it to retain it so thanks for helping out! "ENO" Type in anger and you will make the greatest post you will ever regret. "Sweetest's" Military Aviation Art
xcom Posted April 28, 2014 Posted April 28, 2014 Glad to hear I could help. If someone with experience in all the available functions that the scripting engine offers would write a small guide on how to apply all of the scripts in the scripting engine it would be great, there's a lot of information missing from the DCS Scripting engine wiki. Right now it's back to trial and error. I really suggest using mist.gettable or something similar (forgot the name of the function), it will let you see a lot of info that is returned and be able to deduct a lot from it. [sIGPIC][/sIGPIC] BuddySpike Website | Live Map & Statistics BuddySpike Twitch Channel Buddyspike Discord Buddyspike Facebook
ENO Posted April 28, 2014 Author Posted April 28, 2014 Did we ever figure out a reliable way to detect dynamically spawned infantry units in a zone? "ENO" Type in anger and you will make the greatest post you will ever regret. "Sweetest's" Military Aviation Art
Stonehouse Posted April 30, 2014 Posted April 30, 2014 (edited) Hoping someone can help me. As I understand it every time a DO SCRIPT or DO SCRIPT FILE is executed from a DCS trigger it is in essence a new instance of the script. So all vars are reinitialised etc just as if - in old fashioned terms - you ran the same program twice with slightly different input data. The basic concept I'm hoping to arrive at is a table holding a 3D position and a number of secs and each flak zone will have a continuous trigger script that checks for target aircraft every sec, figures out a time of flight from the zone centre to target in secs rounded to the nearest sec (if this ends up zero will need to handle the explosion creation differently and immediately - point blank fire or maybe too close to fire) and thereby derives a forecast target position for time of flight secs in the future using the current velocity vector and current position and assuming the vector doesn't change. That is it creates target positions and a time in secs from now to when the flak burst should happen every second for every target in the zone the script is attached to. eg aircraft enters the zone at position A. The script calcs a time of flight for the shell from the zone centre to the target. Using position A and the velocity vector and time of flight it figures out a future position A1. (I know this introduces an error as the time of flight will be actually slightly different to position A1 but am ignoring it) Position A1 and time of flight is written to table. Next trigger cycle a second later does the same thing for the aircraft position and vector in this cycle. ie the simulated gun fires every sec in this model at a predicted position based on the time of flight and current position and vector as per GGTharos's suggestion. Modelling gun rof can come later if I get things working along with min/max ranges and skill. Another script on a continuous trigger reads through the table and for every position it decrements the number of secs by 1 each cycle until the number of secs gets to zero and then it creates explosions around the forecast position. That is this script creates explosions at the recorded positions at the appropriate time. Kind of a explosion manager for the whole map and it doesn't care about zones or targets only creating explosions at the right place and time. eg for the above aircraft it reads the entry with position A1 and time t. It reduces t by 1 and if zero it creates the explosion box at A1 and removes the entry from the table. If t-1 >0 it writes the new time t - 1 back to the table entry for the next time around. The pivotal idea for this to work is that the two scripts have to share the table or at least somehow share the position and timing data. A lot of the logic for the above is done in draft but for the sharing of data I've been reading things and tried a lot of stuff last night but can't see how to get the table to work the way I hoped. How do I set up a table or data structure that can be written to and have data retrieved from so that the table entries persist across the cycles/executions? In the environments I'm used to I would probably create a true database file and read and write to it but that either seems impossible or way past my understanding level in lua. Any ideas or possible approaches that might work? Thanks, Stonehouse Edited April 30, 2014 by Stonehouse
ENO Posted May 17, 2014 Author Posted May 17, 2014 (edited) I think this should be an easy one for you guys but I've been looking at this for so long I can barely read it anymore... I'm sure it's right out in the open. I'm getting this error: 00045.848 ERROR DCS: Mission script error: : [string "mist.flagFunc.units_in_zones{..."]:2: '=' expected near '[' These are all my units in zones scripts: mist.flagFunc.units_in_zones{ units = {'[blue][vehicle]','[-g]GEO OFFENSE #012','[-g]GEO OFFENSE #013','[-g]GEO OFFENSE #014','[-g]GEO OFFENSE #015'}, zones = {'tsa'}, flag = 110, req_num = 8, } mist.flagFunc.units_in_zones{ units = {'[blue][vehicle]'}, zones = {'zugdidi'}, flag = 110, req_num = 8, } mist.flagFunc.units_in_zones{ units = {'[blue][vehicle]'}, zones = {'chk'}, flag = 110, req_num = 8, } mist.flagFunc.units_in_zones{ units = {'[blue][vehicle]'}, zones = {'dzh'}, flag = 110, req_num = 8, } mist.flagFunc.units_in_zones{ units = {'[blue][plane]','[blue][helicopter]'}, zones = {'inbound'}, flag = 10001, req_num = 1 } Can anyone pick it out? Rrrrrrrrrrr. Mission file is attached... usually I'd go to the affected line in the mission file to see what's up but I'm pretty sure line 2 isn't the issue. Thanks guys. Edited May 17, 2014 by ENO "ENO" Type in anger and you will make the greatest post you will ever regret. "Sweetest's" Military Aviation Art
ENO Posted May 17, 2014 Author Posted May 17, 2014 Whoops- sorry, disregard... found a trigger I forgot about that had a problem. "ENO" Type in anger and you will make the greatest post you will ever regret. "Sweetest's" Military Aviation Art
RagnarDa Posted May 17, 2014 Posted May 17, 2014 Hoping someone can help me. As I understand it every time a DO SCRIPT or DO SCRIPT FILE is executed from a DCS trigger it is in essence a new instance of the script. So all vars are reinitialised etc just as if - in old fashioned terms - you ran the same program twice with slightly different input data. The basic concept I'm hoping to arrive at is a table holding a 3D position and a number of secs and each flak zone will have a continuous trigger script that checks for target aircraft every sec, figures out a time of flight from the zone centre to target in secs rounded to the nearest sec (if this ends up zero will need to handle the explosion creation differently and immediately - point blank fire or maybe too close to fire) and thereby derives a forecast target position for time of flight secs in the future using the current velocity vector and current position and assuming the vector doesn't change. That is it creates target positions and a time in secs from now to when the flak burst should happen every second for every target in the zone the script is attached to. eg aircraft enters the zone at position A. The script calcs a time of flight for the shell from the zone centre to the target. Using position A and the velocity vector and time of flight it figures out a future position A1. (I know this introduces an error as the time of flight will be actually slightly different to position A1 but am ignoring it) Position A1 and time of flight is written to table. Next trigger cycle a second later does the same thing for the aircraft position and vector in this cycle. ie the simulated gun fires every sec in this model at a predicted position based on the time of flight and current position and vector as per GGTharos's suggestion. Modelling gun rof can come later if I get things working along with min/max ranges and skill. Another script on a continuous trigger reads through the table and for every position it decrements the number of secs by 1 each cycle until the number of secs gets to zero and then it creates explosions around the forecast position. That is this script creates explosions at the recorded positions at the appropriate time. Kind of a explosion manager for the whole map and it doesn't care about zones or targets only creating explosions at the right place and time. eg for the above aircraft it reads the entry with position A1 and time t. It reduces t by 1 and if zero it creates the explosion box at A1 and removes the entry from the table. If t-1 >0 it writes the new time t - 1 back to the table entry for the next time around. The pivotal idea for this to work is that the two scripts have to share the table or at least somehow share the position and timing data. A lot of the logic for the above is done in draft but for the sharing of data I've been reading things and tried a lot of stuff last night but can't see how to get the table to work the way I hoped. How do I set up a table or data structure that can be written to and have data retrieved from so that the table entries persist across the cycles/executions? In the environments I'm used to I would probably create a true database file and read and write to it but that either seems impossible or way past my understanding level in lua. Any ideas or possible approaches that might work? Thanks, Stonehouse Global variables are shared between all scripts. Just remove "local" when declaring it, eg Explosions = {} DCS AJS37 HACKERMAN There will always be bugs. If everything is a priority nothing is.
Stonehouse Posted May 18, 2014 Posted May 18, 2014 Thanks Ragnar I'd roughly got that over the last few weeks but still am struggling with how scope works in the DCS environment. If I declare a global table in scriptA.lua and run it near the start of the mission is that table viewable to scriptB.lua firing on a different latter trigger? The closest I seen to something that might eventually work is how the CGICAP script by SNAFU does things where it declares some stuff including global tables in the main body of the script and the script is run on a mission start type trigger and that script then schedules a function from within the script to be run every x secs using the schedule function command. However I'm still struggling with the nuances of how and why he does things and things like why he has made the scheduled function a local function but all the others seem global escapes me at present. All the sort of thing reference manuals don't cover and needs more of a programmers guide. I think this is kind of how Mist works too? Anyhow I'm not sure but I think perhaps this concept could be used run the script I'm planning on a mission start and schedules the logic that checks the shared table of co-ords & explosion times and produces the flak burst to run every sec while for each flak zone on a continuous trigger would run a function from the mission start script that would add info to the shared table. I know I don't fully understand SNAFUs script yet however so I'm still a long way from the flight time of the shell version of the flak script. Can you provide any guidance or advice from your experiences as to whether using something along the lines of SNAFU's process flow might be workable for what I want to do? Just so I have a better idea of whether it's worth pursuing that line of thought. Thanks, Stoney
xcom Posted May 18, 2014 Posted May 18, 2014 To your first question - yes, its also rather simple to test. RagnarDa has given me advise recently on scopes in lua, I suggest you read - http://www.lua.org/pil/4.2.html http://lua-users.org/wiki/ScopeTutorial [sIGPIC][/sIGPIC] BuddySpike Website | Live Map & Statistics BuddySpike Twitch Channel Buddyspike Discord Buddyspike Facebook
Stonehouse Posted May 18, 2014 Posted May 18, 2014 Thanks xcom. A bit more reading for the morning train trip ;D
RagnarDa Posted May 18, 2014 Posted May 18, 2014 Variable-scope was one of those things I struggled most with when I started writing scripts. It created all sorts of unexpected behavior for me and I had no idea what was happening. I would have had a lot more hair on my head right now if I had started with reading some documentation about it :) I could write some examples, but I believe the links xcom posted does a pretty good job explaining it. Write here if you need more explanation. DCS AJS37 HACKERMAN There will always be bugs. If everything is a priority nothing is.
ata_sa Posted August 29, 2023 Posted August 29, 2023 (edited) If I have wrote a lua function how can I call it in DCS ? I have studied this post and some others but I cannot figure out how ! Edited August 29, 2023 by ata_sa
cfrag Posted September 3, 2023 Posted September 3, 2023 On 8/29/2023 at 9:44 AM, ata_sa said: If I have wrote a lua function how can I call it in DCS ? That depends on when you want to call it. Usually, you'd use a trigger, and when the trigger's conditions are met, you have DCS make a DOSCRIP action. To make things easier for you, why don't you tell us what you want to do, show the script (it may not do what you want it to do, so maybe also explain what the script is supposed to do), and we can then piece it together in a way that helps you to proceed further into mission scripting. Mission scripting is a bit more difficult in DCS than necessary, buit in can be very rewarding once you get over the initial frustration of getting things to work.
ata_sa Posted September 4, 2023 Posted September 4, 2023 (edited) Hi Thanks for your comment @cfrag please check my topic. Grimes helped alot on this but I got stuck at the end https://forum.dcs.world/topic/328903-scripting-with-mist-unit-spawn-by-unhooking-cargo/#comment-5280864 Edited September 4, 2023 by ata_sa 1
Recommended Posts