Jump to content

XML-Functions


manuelhimmler

Recommended Posts

First of all, sorry for my bad english.

I am a newbie in lua scripting! I have read the tutorial but I haven't understood the xml-functions. Can anybody help me in my script and can tellme what I've done wrong by the xml-commands?

login = {} 
loggedin = {} 
register = {} 
registered = {} 
  
function join() 
  
        login[source]=0 
        register[source]=0 
        outputChatBox("Hallo "..getPlayerNametagText(source)..", willkommen auf SMH!", source) 
         
        local accfile = xmlLoadFile("/accounts/"..getPlayerNametagText(source)..".xml",getPlayerNametagText(source)) 
        if accfile then 
            outputChatBox("Dein Account ist registriert, log dich mit /log [passwort] ein",source) 
            login[source]=1 
            registered[source]=1 
        else 
            outputChatBox("Dein Account ist nicht registriert, registriere dich mit /reg [passwort]",source) 
            register[source]=1       
        end 
                 
end 
  
function LoginPlayer(playerid,command,password) 
  
    if(login[playerid] ~= 1) then 
        outputChatBox("Dein Account ist nicht registriert, registriere dich mit /reg [passwort]",source) 
    else 
         
        if(loggedin[playerid]==1) then 
            outputChatBox("Du bist schon eingeloggt!",playerid) 
        else 
         
            if(password==nil) then 
                outputChatBox("Verwendung: /log [passwort]",playerid) 
            else 
             
                local accfile = xmlLoadFile("/accounts/"..getPlayerNametagText(playerid)..".xml") 
                if(password==xmlNodeGetValue(xmlFindSubNode (accfile,"password",0))) then 
                     
                 
                    local x = tonumber(xmlNodeGetValue(xmlFindSubNode (accfile,"x",0))) 
                    local y = tonumber(xmlNodeGetValue(xmlFindSubNode (accfile,"y",0))) 
                    local z = tonumber(xmlNodeGetValue(xmlFindSubNode (accfile,"z",0))) 
                    local angle = tonumber(xmlNodeGetValue(xmlFindSubNode (accfile,"angle",0))) 
                    local interior = tonumber(xmlNodeGetValue(xmlFindSubNode (accfile,"interior",0))) 
                    local money = tonumber(xmlNodeGetValue(xmlFindSubNode (accfile,"money",0))) 
                    local skin = tonumber(xmlNodeGetValue(xmlFindSubNode (accfile,"skin",0))) 
                    xmlUnloadFile(accfile) 
                             
                    spawnPlayer(playerid,x,y,z,angle,skin,interior) 
                    fadeCamera(playerid, true) 
                    setCameraTarget(playerid, playerid) 
             
                    outputChatBox("Login erfolgreich!",playerid) 
                    loggedin[playerid]=1 
                else 
                    outputChatBox("Falsches Passwort!",playerid) 
                end 
            end              
        end 
    end 
end 
  
addCommandHandler("log", LoginPlayer) 
  
function RegisterPlayer(playerid,command,password) 
  
    if(register[playerid] ~= 1) then 
        outputChatBox("Dein Account ist schon registriert, log dich mit /log [passwort] ein",source) 
    else 
         
        if(registered[playerid]==1) then 
            outputChatBox("Du bist schon registriert!",playerid) 
        else 
             
            if(password==nil) then 
                outputChatBox("Verwendung: /reg [passwort]",playerid) 
            else                     
                local accfile = xmlCreateFile("/accounts/"..getPlayerNametagText(playerid)..".xml") 
                local x = xmlCreateSubNode(accfile,"x") 
                local y = xmlCreateSubNode(accfile,"y") 
                local z = xmlCreateSubNode(accfile,"z") 
                local angle = xmlCreateSubNode(accfile,"angle") 
                local interior = xmlCreateSubNode(accfile,"interior") 
                local skin = xmlCreateSubNode(accfile,"skin") 
                local money = xmlCreateSubNode(accfile,"money") 
                local bankmoney = xmlCreateSubNode(accfile,"bankmoney")                  
                xmlNodeSetValue("x", "367.7049") 
                xmlNodeSetValue("y", "-61.7833") 
                xmlNodeSetValue("z", "1001.5151") 
                xmlNodeSetValue("angle", "180") 
                xmlNodeSetValue("interior", "10") 
                xmlNodeSetValue("money","5000") 
                xmlNodeSetValue("bankmoney","50000") 
                xmlNodeSetValue("skin","0")              
                xmlSaveFile(accfile) 
                xmlUnloadFile(accfile)           
                         
                outputChatBox("Registrierung erfolgreich (Passwort: "..password..", log dich mit /log [passwort] ein!",playerid) 
                registered[playerid]=1 
                login[playerid]=1 
            end                  
        end 
    end 
end 
  
addCommandHandler("reg", RegisterPlayer) 
  
function dead() 
  
         
        Sleep(5000) 
        local x = 1959.55 
        local y = -1714.46 
        local z = 12 
        spawnPlayer(source, x, y, z) 
        fadeCamera(source, true) 
        setCameraTarget(source, source) 
end 
  
addEventHandler("onPlayerJoin", getRootElement(), join) 
addEventHandler("onPlayerWasted", getRootElement(), dead) 
  
function Vehicle(playerid,command,vehicleid) 
  
    local vid = tonumber(vehicleid) 
     
    local x,y,z = getElementPosition(playerid) 
    x = x + 5 
    local createdvehicle =  createVehicle(vid,x,y,z) 
     
    if (createdvehicle == false) then 
     
        if(vehicleid == "") then 
         
            outputChatBox("Die VehicleID ".. vid .." gibt es nicht.",playerid) 
             
        else 
         
            outputChatBox("Verwendung: /veh [vehicleid] (400-611)",playerid) 
             
        end 
         
    else 
     
        outputChatBox("Fahrzeug mit der ID ".. vid .." wurde gespawnt.",playerid) 
         
    end 
     
     
     
  
end 
  
addCommandHandler("veh", Vehicle) 
  

The Errors in the console are bad arguments in every xml-function exceptly the first.

Link to comment

A few points that might help:

1. Read only 1 XML file and read it only once: At server start. (ok and at the end to save it) In the meantime, save every bit of data in tables.

2. If I look at your xmlLoadFile, it seems like you're making a new resource for EVERY player? 1 XML file in the same resource as this script will do. :P

3. For next time I recommend to post 1 small piece of script if you don't understand something. Then when you've got help with it, use that one (and the explanation of it) as an example for your other ones.

I hope you understand it. Oh and also read some of the wiki documentation. It might help you quite a bit. For example: xmlNodeGetValue (especially the examples might be of your interest)

Link to comment

It is no answer for my question, but thanks. I wanted to know why my kind of script don't work. :?:

I try it now so as you said, but I don't understand small parts of the wiki.

For example:

maproot = getLoadedMapXMLRoot ()

optionsnode = xmlFindSubNode ( maproot, "options", 0 )

instructionsnode = xmlFindSubNode ( optionsnode, "instructions", 0 )

What the hell is getLoadedMapXMLRoot () , I didn't find these function.

Edit:

I have done it so as you said, but there are the same errors again. What a fuck. Why the file won't be created or where the file will be saved? Look at the errors:

errors.bmp.png

Is there another way to do it better as in my following script?

The code I've taken, sry but how do you want to say me what i've done wrong when you don't know about my code :

login = {} 
loggedin = {} 
register = {} 
registered = {} 
  
function ServerStart() 
  
    accfile = xmlLoadFile("accounts2.xml") 
  
    if not accfile then 
    accfile = xmlCreateFile("accounts2.xml", "accounts") 
    accfile = xmlLoadFile("accounts2.xml") 
    end 
  
end 
  
addEventHandler("onResourceStart",getRootElement(),ServerStart) 
  
function ServerEnd() 
  
    xmlSaveFile(accfile) 
    xmlUnloadFile(accfile) 
end 
  
addEventHandler("onResourceStop",getRootElement(), ServerEnd) 
  
function join() 
  
        login[source]=0 
        register[source]=0 
        outputChatBox("Hallo "..getPlayerNametagText(source)..", willkommen auf SMH!", source) 
  
        local checkaccount = xmlFindSubNode(xmlFindSubNode(accfile,"accounts",0),getPlayerNametagText(playerid),0) 
  
        if checkaccount then 
            outputChatBox("Dein Account ist registriert, log dich mit /log [passwort] ein",source) 
            login[source]=1 
            registered[source]=1 
        else 
            outputChatBox("Dein Account ist nicht registriert, registriere dich mit /reg [passwort]",source) 
            register[source]=1 
        end 
  
end 
  
addEventHandler("onPlayerJoin",getRootElement() ,join) 
  
function LoginPlayer(playerid,command,password) 
  
    if(login[playerid] ~= 1) then 
        outputChatBox("Dein Account ist nicht registriert, registriere dich mit /reg [passwort]",source) 
    else 
  
        if(loggedin[playerid]==1) then 
            outputChatBox("Du bist schon eingeloggt!",playerid) 
        else 
  
            if(password==nil) then 
                outputChatBox("Verwendung: /log [passwort]",playerid) 
            else 
  
                local nametag=xmlFindSubNode(xmlFindSubNode(accfile,"accounts"),getPlayerNametagText(playerid),0) 
  
                if(password==xmlNodeGetValue(xmlFindSubNode(nametag,"password",0))) then 
  
  
                    local x = tonumber(xmlNodeGetValue(xmlFindSubNode (nametag,"x",0))) 
                    local y = tonumber(xmlNodeGetValue(xmlFindSubNode (nametag,"y",0))) 
                    local z = tonumber(xmlNodeGetValue(xmlFindSubNode (nametag,"z",0))) 
                    local angle = tonumber(xmlNodeGetValue(xmlFindSubNode (nametag,"angle",0))) 
                    local interior = tonumber(xmlNodeGetValue(xmlFindSubNode (nametag,"interior",0))) 
                    local money = tonumber(xmlNodeGetValue(xmlFindSubNode (nametag,"money",0))) 
                    local skin = tonumber(xmlNodeGetValue(xmlFindSubNode (nametag,"skin",0))) 
  
                    spawnPlayer(playerid,x,y,z,angle,skin,interior) 
                    fadeCamera(playerid, true) 
                    setCameraTarget(playerid, playerid) 
  
                    outputChatBox("Login erfolgreich!",playerid) 
                    loggedin[playerid]=1 
                else 
                    outputChatBox("Falsches Passwort!",playerid) 
                end 
            end 
        end 
    end 
end 
  
addCommandHandler("log", LoginPlayer) 
  
function RegisterPlayer(playerid,command,password) 
  
    if(register[playerid] ~= 1) then 
        outputChatBox("Dein Account ist schon registriert, log dich mit /log [passwort] ein",source) 
    else 
  
        if(registered[playerid]==1) then 
            outputChatBox("Du bist schon registriert!",playerid) 
        else 
  
            if(password==nil) then 
                outputChatBox("Verwendung: /reg [passwort]",playerid) 
            else 
                local account = xmlCreateSubNode(accfile,getPlayerNametagText(playerid)) 
                local x = xmlCreateSubNode(account,"x") 
                local y = xmlCreateSubNode(account,"y") 
                local z = xmlCreateSubNode(account,"z") 
                local angle = xmlCreateSubNode(account,"angle") 
                local interior = xmlCreateSubNode(account,"interior") 
                local skin = xmlCreateSubNode(account,"skin") 
                local money = xmlCreateSubNode(account,"money") 
                local bankmoney = xmlCreateSubNode(account,"bankmoney") 
                xmlNodeSetValue(x, "367.7049") 
                xmlNodeSetValue(y, "-61.7833") 
                xmlNodeSetValue(z, "1001.5151") 
                xmlNodeSetValue(angle, "180") 
                xmlNodeSetValue(interior, "10") 
                xmlNodeSetValue(money,"5000") 
                xmlNodeSetValue(bankmoney,"50000") 
                xmlNodeSetValue(skin,"0") 
                xmlSaveFile(accfile) 
  
                outputChatBox("Registrierung erfolgreich (Passwort: "..password.."), log dich mit /log [passwort] ein!",playerid) 
                registered[playerid]=1 
                login[playerid]=1 
            end 
        end 
    end 
end 
  
addCommandHandler("reg", RegisterPlayer) 

Link to comment

First of all, load file whenever you need and unload it when you're done with it. Do not load the file onResourceStart and unload it onResourceStop, you may have problems when server crash.

Second of all, paste long codes on http://pastebin.com

You should debug the code by outputing messages (outputChatBox or outputDebugString (with debugscript window enabled)). As you can see the errors say "bad argument", that means the argument passed to the function does not match the argument it expects. Some functions returns elements and false (if it fails), so check if the returned value is not false, especially when working with XML.

Link to comment

A debug window is what appears when you type "/debugscript " (usually use 3 because it will show all the errors/warnings when executing your script) in chatbox or withoug '/' in console (which can be shown with ` the key under Esc).

From the code you posted I can't say you're a noob because you know what table and nil in Lua are (there is no noob that knows this :)).

Make sure your xml file doesn't have any errors: http://www.w3schools.com/Dom/dom_validate.asp

Link to comment

thx for info withdebug window.

I know that thing about nil, because I've seen this, but I didn't know what you mean with table (a function maybe).

What should i test for error, when no xml file was created.

BUT: Thanks, I will test the debug window!!

DDo you now a command to get the VehicleID from the Player, like getPlayerVehicle(thePlayer) ?

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...