CodeMaster Posted March 9, 2008 Share Posted March 9, 2008 (edited) Hi. 'Till now I didn't have any problems with lua concluding that my scripts was only basics. This one is basic too, but I went on a problem when it wasn't able to add any additional text to a file (ex. new line). Its used to get coordinates to a file, like SA-MP debug mode, and then to I read it and get needed info. This is the current code: posFile = nil function loadScript(startedResource) if startedResource == getThisResource() then posFile = fileOpen("Positions.txt") if posFile then outputChatBox("Positions.txt loaded") else posFile = nil posFile = fileCreate("Positions.txt") end outputChatBox("MapMaker loaded...") outputConsole("MapMaker loaded...") end end addEventHandler("onResourceStart",getRootElement(),loadScript) function addLoc(playerSource) local rot = getPlayerRotation(playerSource) local posx,posy,posz = getElementPosition(playerSource) if posFile then fileWrite(posFile,"Positions " .. posx .. " " .. posy .. " " .. posz .. " " .. "Rotation " .. rot .. "\n") outputConsole("Saved...") outputChatBox(posx .. posy .. posz) outputChatBox("Saved") end end addCommandHandler("saveloc",addLoc) EDIT: I forgot to say that it gives in outputChatBox the right coords, but writes some weird to file... Edited March 10, 2008 by Guest Link to comment
eAi Posted March 9, 2008 Share Posted March 9, 2008 What appears in the file? You should make sure you call fileClose at some point or the data may not get written to the file. Link to comment
50p Posted March 9, 2008 Share Posted March 9, 2008 You should try opening file when it's needed and close it when you finish reading/writing. Are you able to open file if it's already opened by MTA? I don't think so, you'll get a message saying that file is opened by other program. That's why you should close it when you're done writing the coords to the file. And: "What appears in the file?" Link to comment
CodeMaster Posted March 9, 2008 Author Share Posted March 9, 2008 Sorry for slow response (was preparing for exams), I have changed code to use XML. function loadScript(startedResource) if startedResource == getThisResource() then outputChatBox("MapMaker loaded...") outputConsole("MapMaker loaded...") end end addEventHandler("onResourceStart",getRootElement(),loadScript) function addLoc(playerSource) local rot = getPlayerRotation(playerSource) local posx,posy,posz = getElementPosition(playerSource) posNode = xmlLoadFile("Positions.xml") if posFile then local subnode = xmlCreateSubNode(posNode,"spawnpoint") xmlNodeSetAttribute(subnode,"posX",tostring(posx)) xmlNodeSetAttribute(subnode,"posY",tostring(posy)) xmlNodeSetAttribute(subnode,"posZ",tostring(posz)) xmlNodeSetAttribute(subnode,"rot",tostring(rot)) else posNode = xmlCreateFile("Positions.xml",positions) local subnode = xmlCreateSubNode(posNode,"spawnpoint") xmlNodeSetAttribute(subnode,"posX",tostring(posx)) xmlNodeSetAttribute(subnode,"posY",tostring(posy)) xmlNodeSetAttribute(subnode,"posZ",tostring(posz)) xmlNodeSetAttribute(subnode,"rot",tostring(rot)) outputChatBox("Saved") end end addCommandHandler("saveloc",addLoc) But from line 20 to line 25 there is Bad argument error. how can I fix it? Link to comment
DiSaMe Posted March 9, 2008 Share Posted March 9, 2008 In line 20 posNode = xmlCreateFile("Positions.xml",positions) positions must be a string. Change this line to posNode = xmlCreateFile("Positions.xml","positions") Link to comment
CodeMaster Posted March 9, 2008 Author Share Posted March 9, 2008 Hankey's first example was written without testing and I followed it blindly Anyways thanks for the help of all of u, and Hankey thanks for pastebin code, i didn't spotted it... EDIT: I forgot xmlSave too EDIT 2: It writes only one line or replaces the same... http://mta.pastebin.com/d6504e355 Link to comment
DiSaMe Posted March 9, 2008 Share Posted March 9, 2008 Looks like "if posFile then" in the line 13 must be "if posNode then" Link to comment
CodeMaster Posted March 10, 2008 Author Share Posted March 10, 2008 Ok lately tonight i managed to find it, but wasn't able to write it (forgot to write it). So the problem is now [sOLVED] The final code is (for those who wants to use it as XML coords writing framework): function loadScript(startedResource) if startedResource == getThisResource() then outputChatBox("MapMaker loaded...") outputConsole("MapMaker loaded...") end end addEventHandler("onResourceStart",getRootElement(),loadScript) function addLoc(playerSource) local rot = getPlayerRotation(playerSource) local posx,posy,posz = getElementPosition(playerSource) posNode = xmlLoadFile("Positions.xml") if posNode then local subnode = xmlCreateSubNode(posNode,"spawnpoint") xmlNodeSetAttribute(subnode,"posX",tostring(posx)) xmlNodeSetAttribute(subnode,"posY",tostring(posy)) xmlNodeSetAttribute(subnode,"posZ",tostring(posz)) xmlNodeSetAttribute(subnode,"rot",tostring(rot)) xmlSaveFile(posNode) xmlUnloadFile(posNode) outputChatBox("Saved 1") else posNode = xmlCreateFile("Positions.xml","positions") local subnode = xmlCreateSubNode(posNode,"spawnpoint") xmlNodeSetAttribute(subnode,"posX",tostring(posx)) xmlNodeSetAttribute(subnode,"posY",tostring(posy)) xmlNodeSetAttribute(subnode,"posZ",tostring(posz)) xmlNodeSetAttribute(subnode,"rot",tostring(rot)) xmlSaveFile(posNode) xmlUnloadFile(posNode) outputChatBox("Saved 2") end end addCommandHandler("saveloc",addLoc) Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now