Jump to content

[SOLVED]Problem with file handling (has final code in posts)


Recommended Posts

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 by Guest
Link to comment

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...