stinkx Posted September 28, 2015 Posted September 28, 2015 Hello Everyone, as a new dcs scripting learner, i need some help understanding the events and event handling. As far as i can understand event is a table that is created when something happens in game. (Shooting, landing, hit etc.) I'm trying to dump that table into a file, so that i can analyze what kind of information i can get from the game. Pseudocode for what i want to do is function handler(table) convert table into formatted text open a file append that text into that file close file end function add handler to global eventhandler tableWhat i have done so far is like function testerhandler(event) local d mist.utils.serialize(d, event) if not fl then local fdir = lfs.writedir() .. "xxxhandler.lua" local fl,err = io.open(fdir,"a") end fl:write(d) fl:close() end mist.addEventHandler(testerhandler) But somehow it is not working, my mission scripting file looks like --Initialization script for the Mission lua Environment (SSE) dofile('Scripts/ScriptingSystem.lua') --Sanitize Mission Scripting environment --This makes unavailable some unsecure functions. --Mission downloaded from server to client may contain potentialy harmful lua code that may use these functions. --You can remove the code below and make availble these functions at your own risk. witchcraft = {} witchcraft.host = "localhost" witchcraft.port = 3001 dofile(lfs.writedir()..[[scripts\witchcraft.lua]]) local function sanitizeModule(name) _G[name] = nil package.loaded[name] = nil end do --sanitizeModule('os') --sanitizeModule('io') --sanitizeModule('lfs') --require = nil --loadlib = nil endThanks for your time in advance!
Grimes Posted September 28, 2015 Posted September 28, 2015 Check the documentation and choose the event you are interested in to see the table format :) In terms of how I'd actually get the table values for use in figuring out its there I'd either use tableShow or writeData. I use tableshow if its a smallish table, and I used writeData if I need to see how the table gets modified or for future reference of the actual figures. local function myEvent(event) env.info(mist.utils.tableShow(event)) -- The lazy way mist.debug.writeData(mist.utils.serialize,{'event', event}, 'eventID'.. event.id .. '.lua') -- Less lazy way end mist.addEventHandler(myEvent) The right man in the wrong place makes all the difference in the world. Current Projects: Grayflag Server, Scripting Wiki Useful Links: Mission Scripting Tools MIST-(GitHub) MIST-(Thread) SLMOD, Wiki wishlist, Mission Editing Wiki!, Mission Building Forum
stinkx Posted September 28, 2015 Author Posted September 28, 2015 Thanks for the link and the code. It will help me a lot. :pilotfly: One more question to you Grimes, since i got your attention. I tried to modify the writeData function as below (append instead of write). And called it with your example code above. It seems appending does not work somehow. Getting some popup errors stating that variable f is nil. mist.debug.writeData2 = function(fcn, fcnVars, fname) if lfs and io then local fdir = lfs.writedir() .. [[Logs\]] .. fname local f = io.open(fdir, 'a') f:write(fcn(unpack(fcnVars, 1, table.maxn(fcnVars)))) f:close() local errmsg = 'mist.debug.writeData2 wrote data to ' .. fdir env.info(errmsg) trigger.action.outText(errmsg, 10) else local errmsg = 'Error: insufficient libraries to run mist.debug.writeData2, you must disable the sanitization of the io and lfs libraries in ./Scripts/MissionScripting.lua' env.info(errmsg) trigger.action.outText(errmsg, 10) end end
FSFIan Posted September 30, 2015 Posted September 30, 2015 For small pieces of information, you can also use the witchcraft.log function. The output will appear in the console window of the DCS Witchcraft server process. For example, this snippet witchcraft.log("foo") witchcraft.log(42) witchcraft.log({1,2,3}) witchcraft.log({["bananas"] = 5, ["apples"] = "tasty"}) will result in the following output: *** LOG: foo *** LOG: 42 *** LOG: [ 1, 2, 3 ] *** LOG: { apples: 'tasty', bananas: 5 } Note that the value you pass to witchcraft.log() is converted to JSON to pass it to the server process (hence "[1,2,3]" instead of "{1,2,3}" in the output). It does not work with Lua tables that have both numeric and string keys in it, so the following would cause an error: witchcraft.log({ [1] = "one", ["two"] = "two" }) So if you pass an unknown value to witchcraft.log(), use mist.utils.tableShow to convert it to a string. witchcraft.log(mist.utils.tableShow({ [1] = "one", ["two"] = "two" })) Output: *** LOG: { [1] = "one", ["two"] = "two", } witchcraft.log() is not meant to log significant amounts of data, but it can be useful to quickly verify something, especially if you can watch the console window in real time on a second monitor. DCS-BIOS | How to export CMSP, RWR, etc. through MonitorSetup.lua
stinkx Posted September 30, 2015 Author Posted September 30, 2015 I'll definitely try that Ian. The console window was idling on the second monitor, now it has a new purpose :) Thanks a lot
Recommended Posts