Castillo Posted December 16, 2010 Share Posted December 16, 2010 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. Link to comment
botanist Posted December 17, 2010 Share Posted December 17, 2010 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 Link to comment
Castillo Posted December 17, 2010 Author Share Posted December 17, 2010 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 Link to comment
Aibo Posted December 17, 2010 Share Posted December 17, 2010 where's the getCarIndex function? and youre using the same index in xmlFindChild for user and car, idk what is the purpose of that. Link to comment
Castillo Posted December 17, 2010 Author Share Posted December 17, 2010 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 Link to comment
Aibo Posted December 17, 2010 Share Posted December 17, 2010 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. Link to comment
Castillo Posted December 17, 2010 Author Share Posted December 17, 2010 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. Link to comment
Aibo Posted December 17, 2010 Share Posted December 17, 2010 well you're always getting 0, because nodes dont have children. you should count children of accountRoot, not idsRoot, which is a node itself. Link to comment
Castillo Posted December 17, 2010 Author Share Posted December 17, 2010 thanks alot Aiboforcen, i will post the working code here (maybe someone in future gets same error ) 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 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