Jump to content

save xml when quit


Castillo

Recommended Posts

hey there, i want to save the player cordinates when he quits but the problem is that dunno why dosnt check my character or somthing like that,

i set the element data of "charname" when he selects it, that works fine but not this part. dosnt give any errors or warnings.

if someone can help me thanks :D

Edited by Guest
Link to comment

try little debugging

 

function onquitdb ()
outputDebugString("player "..getPlayerName(source).." logged out/quit")
local root = xmlLoadFile ("playerdb.xml")
local index = getUserIndex (getAccountName(getPlayerAccount(source)),root)
outputDebugString ("index: "..tostring(index))
if (index) then
outputDebugString("if condition was true")
local accountR2 = xmlFindChild (root,"user",index)
outputDebugString(tostring(accountR2)) -- i think it will be something like "res: 0388782" (random number) if i remember correctly, or it can be false/nil (not sure) if child cant be found
for _aux=0, #xmlNodeGetChildren(accountR2)-1 do
outputDebugString("for iteration")
local charname = getElementData(source,"charname")
outputDebugString("charname=="..tostring(charname))
local accountR = xmlFindChild (accountR2,"character",_aux)
outputDebugString(tostring(accountR)) -- as above
if (tostring(xmlNodeGetAttribute(accountR,"name")) == charname) then
outputDebugString("name was equal charname")
       getmoney = getPlayerMoney (source)
outputDebugString("money: "..tostring(getmoney))
       x, y, z = getElementPosition ( source )
outputDebugString("xyz: "..tostring(x).." "..tostring(y).." "..tostring(z))
outputDebugString(tostring(xmlNodeSetAttribute (accountR,"x",x))) -- should return true on success (not sure, check in wiki)
-- i assume others will be true too if first one is true
xmlNodeSetAttribute (accountR,"y",y)
xmlNodeSetAttribute (accountR,"z",z)
xmlNodeSetAttribute (accountR,"cash",getmoney)
xmlNodeSetAttribute (accountR, "w0", getPedWeapon ( source, 0 ))
xmlNodeSetAttribute (accountR, "w1", getPedWeapon ( source, 1 ))
xmlNodeSetAttribute (accountR, "w2", getPedWeapon ( source, 2 ))
xmlNodeSetAttribute (accountR, "a2", getPedTotalAmmo ( source, 2 ))
xmlNodeSetAttribute (accountR, "w3", getPedWeapon ( source, 3 ))
xmlNodeSetAttribute (accountR, "a3", getPedTotalAmmo ( source, 3 ))
xmlNodeSetAttribute (accountR, "w4", getPedWeapon ( source, 4 ))
xmlNodeSetAttribute (accountR, "a4", getPedTotalAmmo ( source, 4 ))
xmlNodeSetAttribute (accountR, "w5", getPedWeapon ( source, 5 ))
xmlNodeSetAttribute (accountR, "a5", getPedTotalAmmo ( source, 5 ))
xmlNodeSetAttribute (accountR, "w6", getPedWeapon ( source, 6 ))
xmlNodeSetAttribute (accountR, "a6", getPedTotalAmmo ( source, 6 ))
xmlNodeSetAttribute (accountR, "w7", getPedWeapon ( source, 7 ))
xmlNodeSetAttribute (accountR, "a7", getPedTotalAmmo ( source, 7 ))
xmlNodeSetAttribute (accountR, "w8", getPedWeapon ( source, 8 ))
xmlNodeSetAttribute (accountR, "a8", getPedTotalAmmo ( source, 8 ))
xmlNodeSetAttribute (accountR, "w9", getPedWeapon ( source, 9 ))
xmlNodeSetAttribute (accountR, "a9", getPedTotalAmmo ( source, 9 ))
xmlNodeSetAttribute (accountR, "w10", getPedWeapon ( source, 10 ))
xmlNodeSetAttribute (accountR, "w11", getPedWeapon ( source, 11 ))
xmlNodeSetAttribute (accountR, "w12", getPedWeapon ( source, 12 ))
end
end
end
end
addEventHandler ( "onPlayerQuit", getRootElement(), onquitdb )
addEventHandler ( "onPlayerLogout", getRootElement(), onquitdb )

try this :)

Link to comment

now you should know all, right?

do you understand what your script is doing?

lets check

-- your getUserIndex returned nil = returned nothing
 
function getUserIndex (username,root)
for i,v in ipairs (xmlNodeGetChildren (root)) do
local name = xmlNodeGetAttribute (v,"account")
-- after this "if" we have return, after end of this if we have nothing
-- so its easy to deduct that username IS NOT equal name
-- so, in your XML theres no node for that username,
-- and you have to create it
if (username == name) then
return i -1
end
end
end

Link to comment
  varez said:
  SolidSnake14 said:
says: Info: Player Solidsnake14 logged out/quit

and then info: index: nil

also says

name was equal charname

money: 5000

true

charname == Solidsnake14

this isn't all output, right?

right, its not all what it outputs it outputs also cordinates but still not saving.

Link to comment
  SolidSnake14 said:
  50p said:
You have to save the file. Where do you save it?
xmlSaveFile

aaaaaaaaaaaaaaaa my bad, thanks it worked now!

thank you all who helped me.

Edit: i got a problem, if i logout the data dosnt get saved

Inside your function you getPlayerAccount but when player logs out, his account is a "guest" account. You should modify your function so it gets player's current account IF onPlayerQuit called this function and use player's previous account IF onPlayerLogout called this function. You can use another secret variable eventName to check which event called this function. Like:

function onquitdb( prevAccount )
   ...
local index;
if eventName == "onPlayerQuit" then
       index = getUserIndex (getAccountName(getPlayerAccount(source)),root)
elseif eventName == "onPlayerLogout" then
       index = getUserIndex (getAccountName(prevAccount),root)
end
   ...
end

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