Jump to content

[SOLVED] HELP! XML ...


SpecT

Recommended Posts

  • Replies 66
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

function updatePlayed (songName) 
    local file = xmlLoadFile ( "songs.xml" ) 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
        if ( xmlNodeGetAttribute ( v, "name" ) == songName ) then 
            xmlNodeSetAttribute ( v, "played", xmlNodeGetAttribute ( v, "played" ) + 1 ) 
        end 
    xmlSaveFile ( file ) 
    xmlUnloadFile ( file ) 
    end 
end 

That's in the server-side.

Client side:

song = getSongName ... 
callServerFunction("updatePlayed ",songName) 

I have used the callServerFunction which is in the code.

Link to comment
function updatePlayed (songName) 
    local file = xmlLoadFile ( "songs.xml" ) 
    if ( not file ) then 
        outputChatBox ( "ERROR" ); 
        return; 
    end 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
        if ( xmlNodeGetAttribute ( v, "name" ) == songName ) then 
            xmlNodeSetAttribute ( v, "played", xmlNodeGetAttribute ( v, "played" ) + 1 ) 
        end 
    end 
    xmlSaveFile ( file ) 
    xmlUnloadFile ( file ) 
end 

Check if in chatbox won't be any error.

Link to comment
function updatePlayed (songName) 
    local file = xmlLoadFile ( "songs.xml" ) 
    if ( not file ) then 
        outputChatBox ( "ERROR" ); 
        return; 
    end 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
        if ( xmlNodeGetAttribute ( v, "name" ) == songName ) then 
            xmlNodeSetAttribute ( v, "played", xmlNodeGetAttribute ( v, "played" ) + 1 ) 
        end 
    end 
    xmlSaveFile ( file ) 
    xmlUnloadFile ( file ) 
end 

Check if in chatbox won't be any error.

Nope ... there is no message.

Link to comment

Ops .. my mistake. Didn't save the file.

Okay now there are no errors but actually it doesn't update the played value. It's still 0. The info about the songs.xml shows that it was changed seconds ago but when I check the song the played value is still 0.

Edited by Guest
Link to comment
function updatePlayed (songName) 
    local file = xmlLoadFile ( "songs.xml" ) 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
        if ( xmlNodeGetAttribute ( v, "name" ) == songName ) then 
            xmlNodeSetAttribute ( v, "played", tonumber ( xmlNodeGetAttribute ( v, "played" ) ) + 1 ) 
        end 
    end 
    xmlSaveFile ( file ) 
    xmlUnloadFile ( file ) 
end 

Link to comment
function updatePlayed (songName) 
    local file = xmlLoadFile ( "songs.xml" ) 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
        if ( xmlNodeGetAttribute ( v, "name" ) == songName ) then 
            xmlNodeSetAttribute ( v, "played", tonumber ( xmlNodeGetAttribute ( v, "played" ) ) + 1 ) 
        end 
    end 
    xmlSaveFile ( file ) 
    xmlUnloadFile ( file ) 
end 

Oh GAWD ... seriously :D It worked ...

Thanks man again for the great help!

Link to comment

Add 'break' line after changing value so it won't loop for other songs.

Like that

function updatePlayed (songName) 
    local file = xmlLoadFile ( "songs.xml" ) 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
        if ( xmlNodeGetAttribute ( v, "name" ) == songName ) then 
            xmlNodeSetAttribute ( v, "played", tonumber ( xmlNodeGetAttribute ( v, "played" ) ) + 1 ) 
            break; 
        end 
    end 
    xmlSaveFile ( file ) 
    xmlUnloadFile ( file ) 
end 

Link to comment
Add 'break' line after changing value so it won't loop for other songs.

Like that

function updatePlayed (songName) 
    local file = xmlLoadFile ( "songs.xml" ) 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
        if ( xmlNodeGetAttribute ( v, "name" ) == songName ) then 
            xmlNodeSetAttribute ( v, "played", tonumber ( xmlNodeGetAttribute ( v, "played" ) ) + 1 ) 
            break; 
        end 
    end 
    xmlSaveFile ( file ) 
    xmlUnloadFile ( file ) 
end 

Oh okay.

Link to comment
Give me code you have.

Client side ?

--on click event 
    local songName = get the song name 
    local length = songList[songName][1] 
    local played = songList[songName][2] 
  
    musicStore.length = length 
    musicStore.played = played 
  
    outputChatBox(""..songName.."|"..played.."") 

It gives me the song name and 0

Load part (client):

songList = { } 
  
function updateSongs ( ) 
    local file = xmlLoadFile ( "songs.xml" ) 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
        local name, length, played = xmlNodeGetAttribute ( v, "name" ), xmlNodeGetAttribute ( v, "length" ), tonumber ( xmlNodeGetAttribute ( v, "played" )) 
        songList[name] = {length, played} 
    end 
    xmlUnloadFile ( file ) 
end 
  
function loadSongsInGridlist() 
    updateSongs ( ) 
    for name, v in pairs ( songList ) do 
        table.insert(musicStore.songName,name) 
        table.insert(musicStore.songCache,name) 
    end 
end 

Edited by Guest
Link to comment
So there is an error with loadng the file? You are loading it clientside?

Ye I load it clientside. When click the button it calls the server to edit the played value. And when click on a song it loads the length and played times.

EDIT:So if it shows 0 then probably something sets it to be 0.

Link to comment
You can try that. That may be causing a problem. Whole file should be opened servisde I guess. Just get 'xmlNodeGetChrildren' serverside, trigger it to client and loop it there.

I'm trying to trigger from serverside to clientside but it tells me that there is no such event .. Tried so much things and again the same :oops::oops::oops::cry:

Link to comment

Clientside:

songList = { } 
  
function loadTable(table) 
    local name, length, played = xmlNodeGetAttribute ( table, "name" ), xmlNodeGetAttribute ( table, "length" ), tonumber ( xmlNodeGetAttribute ( table, "played" )) 
    songList[name] = {length, played} 
end 
addEvent("onLoadTable",true) 
addEventHandler("onLoadTable",localPlayer,loadTable) 

Serverside:

songList = { } 
  
function updateSongs ( ) 
    local file = xmlLoadFile ( "songs.xml" ) 
    for _, v in ipairs ( xmlNodeGetChildren ( file ) ) do 
       triggerClientEvent("onLoadTable",getRootElement(),v) 
    end 
    xmlUnloadFile ( file ) 
end 
addEventHandler("onResourceStart",getRootElement(),updateSongs) 

I'm sure that it's wrong but I don't know what.

Link to comment

Serverside scripts are loading much faster than clientside script. You are triggering event which doesn't really exist. Do like that:

s-side

addEvent ( "requestUpdating", true ); 
function updateSongs ( ) 
    local file = xmlLoadFile ( "songs.xml" ) 
    if ( file ) then 
        triggerClientEvent ( source, "onLoadTable", source, xmlNodeGetChildren ( file ) ) 
    end 
    xmlUnloadFile ( file ) 
end 
addEventHandler ( "requestUpdating", root, updateSongs ) 

c-side

songList = { } 
  
addEventHandler ( "onClientResourceStart", resourceRoot, 
    function ( ) 
        triggerServerEvent ( "requestUpdating", localPlayer ); 
    end 
); 
  
function loadTable ( table ) 
    if ( type ( table ) == "table" ) then 
        for _, v in pairs ( table ) do 
            local name, length, played = xmlNodeGetAttribute ( v, "name" ), xmlNodeGetAttribute ( v, "length" ), tonumber ( xmlNodeGetAttribute ( v, "played" )) 
            songList[name] = {length, played} 
        end 
    end 
end 
addEvent ( "onLoadTable", true ) 
addEventHandler ( "onLoadTable", root, loadTable ) 

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