ESAc_matador Posted September 12, 2016 Posted September 12, 2016 hello guys, I am doing more interesting updates to the Recon mission. I want to present the results in a file. so I am doing this. for k, v in pairs (_tabletarget) do local _redStaticObject = _tabletarget[k] local _redStaticObjecttypename = _redStaticObject:getTypeName() local _redStaticObjectname = _redStaticObject:getName() local _redStaticObjectpos = mist.getLLString({units = mist.makeUnitTable{tostring(_redStaticObjectname)} , acc = 2,}) [b]text2 = _redStaticObjecttypename.." in position ".._redStaticObjectpos exportFile = io.open("ground units report", "r+") exportFile:write(text2) exportFile:close() [/b] So, each _redStaticObjecttypename.." in position ".._redStaticObjectpos /[code] should be written to the "ground units report" file, but it only adds one. ANy help out here?. I am pretty sure each line is correct, since I can see them in the game.
FSFIan Posted September 12, 2016 Posted September 12, 2016 I see nothing obviously wrong with this code, so the error is probably in the part you removed to make it shorter. It's hard to give a definite answer without seeing the whole file, or at least the complete for loop. Right now, this would throw a syntax error due to the missing 'end' statement. And the indentation looks suspicious (drops back a level without closing the block). Are you sure _tabletarget contains more than one element? (Have you tried dumping it with mist.uitls.tableShow?) Also, while this shouldn't make the code incorrect, opening and closing the file for each small bit of info to add is inefficient. It's faster to open it once, make all your calls to write(), then close it when you are done, because then the data can get written to the hard drive in one big chunk instead of lots of small ones. DCS-BIOS | How to export CMSP, RWR, etc. through MonitorSetup.lua
ESAc_matador Posted September 12, 2016 Author Posted September 12, 2016 (edited) I tryed to put the full function. That, works if not using the io.write. In fact, the values an parameters are good because you can see in the generated file, but you only get one entry. function reconpresentationObjects() -- this is to get the current altitude local Position_vec3 = Unit.getByName('recon1'):getPoint() local _elevation = land.getHeight({x = Position_vec3.x, y = Position_vec3.z}) local _height = Position_vec3.y - _elevation if _height <= 2 then -- This makes the Display of enemy objects only if you are below 2 meters. Just write "--" in front of this line if you dont want to wait to see the targets display local _tabletarget = table_unique(targetObjects) -- All duplicated groups removed, this is because every loop, an enemy object is added in _tabletarget for k, v in pairs (_tabletarget) do local _redStaticObject = _tabletarget[k] local [b]_redStaticObjecttypename[/b] = _redStaticObject:getTypeName() -- Type Name. this works local _redStaticObjectname = _redStaticObject:getName() -- Name. this works local [b]_redStaticObjectpos[/b] = mist.getLLString({units = mist.makeUnitTable{tostring(_redStaticObjectname)} , acc = 2,}) -- faster way to get the LL position of a unit. It works --trigger.action.outText(_redStaticObjecttypename.." in position ".._redStaticObjectpos,20) -- if you want to display in game remove "--". It works. [b]local text2 = _redStaticObjecttypename.." in position ".._redStaticObjectpos -- this part should add a line every loop done in the previous loop with "_tabletargets" but only add one. exportFile = io.open("ground units report", "r+") exportFile:write(text2,'\n') exportFile:close() [/b] end else trigger.action.outText("You need to land to reveal the pictures",20) -- advice that you have to land to get the picture. end end Edited September 12, 2016 by ESAc_matador
nomdeplume Posted September 15, 2016 Posted September 15, 2016 Your open seems a strange. Why using "r+" mode, and why open the file for every single entry? Anyway, I believe r+ mode opens the file at the beginning of the stream, and since you never read from it or otherwise advance the file pointer, you just overwrite from the start of the file every time you write your new line. The easiest fix would be to just change the flag to "wa" mode (write append) so it opens the file and puts the file pointer at the end; but really I would be opening it before you enter the loop, and close it after you finish, so save all that unnecessary work within the loop itself. i.e. if _height <= 2 then local _tabletarget = table_unique(targetObjects) -- overwrite file each time, change to "wa" to append to it exportFile = io.open("ground units report", "w") for k, v in pairs (_tabletarget) do ... local text2 = ... if exportFile ~= nil then exportFile:write(text2,'\n') end end if exportfile ~= nil then exportFile:close() else trigger.action.outText("Unable to open output file!", 20) end else trigger.action.outText("You need to land to reveal the pictures",20) end Alternatively if you expect the output to be fairly small (no longer than a few tens or hundreds of kilobytes) you could just append it all to a single string, then write that string out at the end. if _height <= 2 then local _tabletarget = table_unique(targetObjects) local outputstr = '' for k, v in pairs (_tabletarget) do ... local text2 = ... outputstr = outputstr .. text2 .. "\n" end -- overwrite file each time, change to "wa" to append to it exportFile = io.open("ground units report", "w") if exportFile ~= nil then exportFile:write(outputstr); exportFile:close() else trigger.action.outText("Unable to open output file!", 20) end else trigger.action.outText("You need to land to reveal the pictures",20) end P.S. It might be easier for people to open the file if its name ends in .txt.
Recommended Posts