Jump to content

xml problem


Castillo

Recommended Posts

Posted

hi there, i've got a error in a function using xml, its suposed to return the id he gets then do +1 every time i execute the function but it doesn't works, keeps returning id 0 every time.

my code:

function getCarCount (player) 
  local root = getResourceConfig("data.xml") 
  local index = getCarIndex (getAccountName(getPlayerAccount(player)),root) 
  if index then 
    local accountRoot = xmlFindChild (root,"user",index) 
    local idsRoot = xmlFindChild (accountRoot,"car",index) 
    local allIds = #xmlNodeGetChildren (idsRoot) 
    return allIds 
  end 
  xmlUnloadFile (root)   
end 

Tanks in advance.

Posted

1: Don't use return before you really want to end the function. Use it at least. Now you are returning, which means "stopping" too, before xmlUnloadFile gets called.

2: Where's the +1 you are talking about?

3: try my weird change:

  
function getCarCount (player) 
    local allIds = 0 
    local root = getResourceConfig("data.xml") 
    local index = getCarIndex (getAccountName(getPlayerAccount(player)),root) 
    if index then 
        local accountRoot = xmlFindChild (root,"user",index) 
        local idsRoot = xmlFindChild (accountRoot,"car",index) 
        allIds = xmlNodeGetChildren (idsRoot) -- Removed the #, idk if that will 
    end 
    xmlUnloadFile (root) 
    return #allIds -- Heres the # again, idk if this returns 0 if theres no table =/ 
end 
  

Posted

i'm afraid the problem still here,

look:

  
<playercars> 
    <user account="Snake"> 
        <car name="415" price="50000" id="0" /> 
        <car name="415" price="50000" id="0" /> 
    </user> 
</playercars> 

as you can see it gives same id to every car i buy :|

Posted

where's the getCarIndex function?

and youre using the same index in xmlFindChild for user and car, idk what is the purpose of that.

Posted
function getCarIndex (username,root) 
  for i,v in ipairs (xmlNodeGetChildren (root)) do 
    local name = xmlNodeGetAttribute (v,"account") 
    if (username == name) then 
      return i -1 
    end 
  end 
end  

Posted

still no sign of any «index+1» or xmlCreateChild.

and you've posted XML with 0's but not the part which writes the XML itself

and:

      
local idsRoot = xmlFindChild (accountRoot,"car",index) 
local allIds = #xmlNodeGetChildren (idsRoot) 

here you're getting some car node and the trying to get it's children, which it doesnt have.

well, judging by XML you've posted.

Posted

i use getCarCount function when i buy a car,

local carCount = tonumber(getCarCount(source)) 
xmlNodeSetAttribute (accountRoot,"id", carCount) 

and yes, the code is generated by the script himself when i buy a car.

Posted

well you're always getting 0, because nodes dont have children.

you should count children of accountRoot, not idsRoot, which is a node itself.

Posted

thanks alot Aiboforcen, i will post the working code here (maybe someone in future gets same error :D)

function getCarCount (player) 
    local allIds = 0 
    local root = getResourceConfig("data.xml") 
    local index = getCarIndex (getAccountName(getPlayerAccount(player)),root) 
    if index then 
    local accountRoot = xmlFindChild (root,"user",index) 
    allIds = xmlNodeGetChildren (accountRoot) 
    end 
    xmlUnloadFile (root) 
    return #allIds 
end 

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