AKA_Clutter Posted June 19, 2023 Posted June 19, 2023 Hi all, I am trying to write information to a file and I am having an issue. I have commented out the sanitize lines in MissionScripting.lua and I am able to write to a file. However, I want to add a check to make sure that DCS sanitation has been disabled. I do this with an if statement, but this ends up generating an error. Here is the sbnipet of code that I am having issues with. local function Write_Mission_Stats () trigger.action.outTextForCoalition(2,MSG_Header .. "TEST - Simple test to write to file TEST" .. MSG_Footer, 15) -- Test_File_Handle = io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w") if io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w") ~= nil then local Generate_Mission_Config_Report_Tbl_Outstring = Mission_Config_Report_Tbl () Test_File_Handle = io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w") Test_File_Handle:write("===========================\n") Test_File_Handle:write("Mission Stats for AKA Campaign\n") Test_File_Handle:write("===============================\n") Test_File_Handle:write(Generate_Mission_Config_Report_Tbl_Outstring) else trigger.action.outTextForCoalition(2, "\n*************************************\n\nNot able to open wirte file. \n\n**********************", 15) end end -- Next line is to call the function to write mission states to a file. timer.scheduleFunction(Write_Mission_Stats, {}, timer.getTime() + 15*Sec_per_min) The error message is that the line with " Test_File_Handle:write("===========================\n")" attempt to index global Test_Handle(a nil) value). I check after the error and a file named "AKA_Test_File.txt" has been created,but is empty. If I remove the if statement, the code works fine and a file is written. I tried adding "Test_File_Handle = io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w")" as the text condition to the if statement, but it didn't like that at all. Can any tell me why this is happening, or a better way to check and see if I can write a file and not generate an error that stops the script. Thanks, ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
Kanelbolle Posted June 20, 2023 Posted June 20, 2023 (edited) 16 hours ago, AKA_Clutter said: Hi all, I am trying to write information to a file and I am having an issue. I have commented out the sanitize lines in MissionScripting.lua and I am able to write to a file. However, I want to add a check to make sure that DCS sanitation has been disabled. I do this with an if statement, but this ends up generating an error. Here is the sbnipet of code that I am having issues with. local function Write_Mission_Stats () trigger.action.outTextForCoalition(2,MSG_Header .. "TEST - Simple test to write to file TEST" .. MSG_Footer, 15) -- Test_File_Handle = io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w") if io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w") ~= nil then local Generate_Mission_Config_Report_Tbl_Outstring = Mission_Config_Report_Tbl () Test_File_Handle = io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w") Test_File_Handle:write("===========================\n") Test_File_Handle:write("Mission Stats for AKA Campaign\n") Test_File_Handle:write("===============================\n") Test_File_Handle:write(Generate_Mission_Config_Report_Tbl_Outstring) else trigger.action.outTextForCoalition(2, "\n*************************************\n\nNot able to open wirte file. \n\n**********************", 15) end end -- Next line is to call the function to write mission states to a file. timer.scheduleFunction(Write_Mission_Stats, {}, timer.getTime() + 15*Sec_per_min) The error message is that the line with " Test_File_Handle:write("===========================\n")" attempt to index global Test_Handle(a nil) value). I check after the error and a file named "AKA_Test_File.txt" has been created,but is empty. If I remove the if statement, the code works fine and a file is written. I tried adding "Test_File_Handle = io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w")" as the text condition to the if statement, but it didn't like that at all. Can any tell me why this is happening, or a better way to check and see if I can write a file and not generate an error that stops the script. Thanks, Look here how to write to files: Not sure if this works at all. : if io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt","w") ~= nil then Should maybe be: if Test_File_Handle == nil then The file is not open at this point, so why are you checking if it is open and opening it again in the code below ? and you should have this at the end of the write. Test_File_Handle:close() Test_File_Handle = nil You can also see here for a lua example for checking if the file is open or not: https://stackoverflow.com/questions/28867248/lua-check-if-a-file-is-open Edited June 20, 2023 by Kanelbolle WARGAMES Servers: https://forum.dcs.world/topic/301046-wargames-server-pvp-and-pve-campaign-servers/ Scripts: RGC & SPGG https://github.com/AGluttonForPunishment/
AKA_Clutter Posted June 20, 2023 Author Posted June 20, 2023 Thanks for the response Kanelbolle. The reason I am checking to see if I get an error when trying to open the file is to try and avoid a runtime error that stops the script. I also posted this question on the Stack Overflow board. The response I got there, was that I was opening the file TWICE is rapid succession., which may caused a Time of check to Time of Use error (Time-of-check_to_time-of-use). As being new to programming and lua, I would never have figured this out. The responder also suggested a better way to do what I wanted to do. The key seems to be to open the file and assign that to a handle (Variable) and also capture the error, if the file can't be opened. I think the key is the follwoing line. local Test_File_Handle, err = io.open(FILENAME, "w") Anyway, my revised code is shown below and seems to work. I haven't tested to see what happens if I can't write the file to start with. local function Write_Mission_Stats () trigger.action.outTextForCoalition(2,MSG_Header .. "TEST - Simple test to write to file TEST" .. MSG_Footer, 15) ---[[ ALternate Method local Test_File_Handle, err = io.open("C:\\Users\\Nathan\\Saved Games\\DCS.openbeta\\AKA_Test_File.txt", "w") -- For Clutter's Comuter -- local Generate_A2A_Summary_Stats_OutString = Summary_Stats_Report_Tbl (Index_TBL) if not Test_File_Handle then -- failed to open file, let's display the error -- print(err) -- or error(err), or whatever debugging function trigger.action.outTextForCoalition(2,MSG_Header .. "FIle was not opened\n" .. MSG_Footer, 15) trigger.action.outTextForCoalition(2,MSG_Header .. err .. MSG_Footer, 15) else local Generate_Mission_Config_Report_Tbl_Outstring = Mission_Config_Report_Tbl () trigger.action.outTextForCoalition(2,MSG_Header .. "TEST PRINT BEFORE WRITE CALL" .. MSG_Footer, 15) -- Output summary talbe of A2A groups trigger.action.outTextForCoalition(2,MSG_Header .. Generate_A2A_Summary_Stats_OutString .. MSG_Footer, 15) -- Output summary talbe of A2A groups -- opened the file, write some data Test_File_Handle:write("===============================\n") Test_File_Handle:write(" Mission Stats for AKA Campaign\n") -- Close the file Test_File_Handle:close() end return end -- Next line is to call the function to write mission states to a file. timer.scheduleFunction(Write_Mission_Stats, {}, timer.getTime() + 15*Sec_per_min) 1 ---------------- AKA_Clutter Win 10 Pro, Intel i7 12700k @4.6 GHz, EVGA RTX 3080 FTW, Ultra 64 GB G.Skill DDR4 3600 RAM, Acer 27" flat screen, HP Reverb G2, TM Warthog HOTAS with Virpil warBRD base, MFG Rudder Pedals, Virpil TCS Rotor Base with AH-64Dcollective, TrackIR 5 Pro w/Vector Expansion, PointCTRL.
Recommended Posts