Jump to content

XML Problem


Sasu

Recommended Posts

I don't understand very well xml functions, so I am trying to learn. I have made this function but idk why it doesnt work.

Server - side:

function setPlayersPosition(path) 
    local xml = xmlLoadFile(":"..path.."/pos.xml") 
    if xml then 
        local terrorist = getPlayersInTeam(terroristTeam) 
        local terroristPos = xmlFindChild ( xml, "terrorist", 0 ) 
        local counterPos = xmlFindChild ( xml, "counter", 1 ) 
        for i,v in ipairs(terrorist) do 
            local x,y,z = loadstring("return "..xmlNodeGetChildren(terroristPos, math.random(4)))() 
            setElementPosition(v, x, y, z) 
            outputChatBox(x..", "..y..", "..z, root) 
        end 
    end 
end 

The XML:

<terrorists> 
    <terrorists>334.55, 585.32, 53.90</terrorists> 
    <terrorists>326.82, 585.09, 53.90</terrorists> 
    <terrorists>325.99, 592.19, 54.46</terrorists> 
    <terrorists>338.39, 592.23, 54.46</terrorists> 
</terrorists> 
<counter> 
    <counter>0, 0, 0</counter> 
    <counter>0, 0, 0</counter> 
    <counter>0, 0, 0</counter> 
    <counter>0, 0, 0</counter> 
</counter> 

I am trying to get a random position of terrorist, can you help me?

Debugscript 3: WARNING: server.lua:36: Bad argument @ 'xmlnodeGetChildren'

Line 36 = Line 8

Thank you very much

Link to comment
function setPlayersPosition ( path ) 
    local xml = xmlLoadFile  ( ":".. path .."/pos.xml" ) 
    if ( xml ) then 
        local terrorist = getPlayersInTeam ( terroristTeam ) 
        local terroristPos = xmlFindChild ( xml, "terrorist", 0 ) 
        local terroristPositions = xmlNodeGetChildren ( terroristPos ) 
        local counterPos = xmlFindChild ( xml, "counter", 1 ) 
        local counterPositions = xmlNodeGetChildren ( counterPos ) 
        for _, v in ipairs ( terrorist ) do 
            local node = terroristPositions [ math.random ( #terroristPositions ) ] 
            if ( node ) then 
                local x, y, z = split ( xmlNodeGetValue ( node ), ", " ) 
                setElementPosition ( v, x, y, z ) 
                outputChatBox ( x ..", ".. y ..", ".. z, root ) 
            end 
        end 
    end 
end 

Try it.

Link to comment
function setPlayersPosition ( path ) 
    local xml = xmlLoadFile  ( ":".. path .."/pos.xml" ) 
    if ( xml ) then 
        local terrorist = getPlayersInTeam ( terroristTeam ) 
        local terroristPos = xmlFindChild ( xml, "terrorist", 0 ) 
        local terroristPositions = xmlNodeGetChildren ( terroristPos ) 
        local counterPos = xmlFindChild ( xml, "counter", 1 ) 
        local counterPositions = xmlNodeGetChildren ( counterPos ) 
        for _, v in ipairs ( terrorist ) do 
            local node = terroristPositions [ math.random ( #terroristPositions ) ] 
            if ( node ) then 
                local x, y, z = split ( xmlNodeGetValue ( node ), ", " ) 
                setElementPosition ( v, x, y, z ) 
                outputChatBox ( x ..", ".. y ..", ".. z, root ) 
            end 
        end 
    end 
end 

Try it.

Debugscript 3 says: Bad argument @ 'xmlNodeGetChildren' on line 6 and 8

XML should have root tags ..

http://www.w3schools.com/xml/xml_syntax.asp

I added the root tags but still the same:

<root> 
    <terrorists> 
        <terrorists>334.55, 585.32, 53.90</terrorists> 
        <terrorists>326.82, 585.09, 53.90</terrorists> 
        <terrorists>325.99, 592.19, 54.46</terrorists> 
        <terrorists>338.39, 592.23, 54.46</terrorists> 
    </terrorists> 
    <counter> 
        <counter>0, 0, 0</counter> 
        <counter>0, 0, 0</counter> 
        <counter>0, 0, 0</counter> 
        <counter>0, 0, 0</counter> 
    </counter> 
</root> 

Link to comment

omg i just noticed this 'shame on me ><'

you wrote terrorists in your xml and terrorist in your script

local terroristPos = xmlFindChild ( xml, "terrorist", 0 ) 

try this :

function setPlayersPosition ( path ) 
    local xml = xmlLoadFile  ( ":".. path .."/pos.xml" ) 
    if ( xml ) then 
        local terrorist = getPlayersInTeam ( terroristTeam ) 
        local terroristPos = xmlFindChild ( xml, "terrorists", 0 ) 
        local terroristPositions = xmlNodeGetChildren ( terroristPos ) 
        local counterPos = xmlFindChild ( xml, "counter", 1 ) 
        local counterPositions = xmlNodeGetChildren ( counterPos ) 
        for _, v in ipairs ( terrorist ) do 
            local node = terroristPositions [ math.random ( #terroristPositions ) ] 
            if ( node ) then 
                local x, y, z = split ( xmlNodeGetValue ( node ), ", " ) 
                setElementPosition ( v, x, y, z ) 
                outputChatBox ( x ..", ".. y ..", ".. z, root ) 
            end 
        end 
    end 
end 

Link to comment

Thank you very much SoRa and Solid.

To help some people:

local x, y, z = split ( xmlNodeGetValue ( node ), ", " ) -> local x, y, z = unpack ( split ( xmlNodeGetValue ( node ), ", " ) ) 

Because split return a table.

EDIT1: Now it says Bad argument @ 'xmlNodeGetChildren' on line 8

EDIT2: I fixed the error that I said in the EDIT1(Line 7):

local counterPos = xmlFindChild ( xml, "counter", 1 ) -> local counterPos = xmlFindChild ( xml, "counter", 0 ) 

Link to comment
function setPlayersPosition ( path ) 
    local xml = xmlLoadFile  ( ":".. path .."/pos.xml" ) 
    if ( xml ) then 
        local terrorist = getPlayersInTeam ( terroristTeam ) 
        local terroristPos = xmlFindChild ( xml, "terrorists", 0 ) 
        local terroristPositions = xmlNodeGetChildren ( terroristPos ) 
        local counterPos = xmlFindChild ( xml, "counter", 0 ) 
        local counterPositions = xmlNodeGetChildren ( counterPos ) 
        for _, v in ipairs ( terrorist ) do 
            local node = terroristPositions [ math.random ( #terroristPositions ) ] 
            if ( node ) then 
                local x, y, z = unpack ( split ( xmlNodeGetValue ( node ), ", " ) ) 
                setElementPosition ( v, x, y, z ) 
                outputChatBox ( x ..", ".. y ..", ".. z, root ) 
            end 
        end 
    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...