OnClientFileDownloadComplete: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Improved example)
 
(5 intermediate revisions by 3 users not shown)
Line 6: Line 6:
==Parameters==  
==Parameters==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string fileName, bool success
string fileName, bool success, resource requestResource
</syntaxhighlight>  
</syntaxhighlight>  


*'''fileName''': the file downloaded.
*'''fileName''': the file downloaded.
*'''success''': whether successful or not.
*'''success''': whether successful or not.
{{New items|4.0157|1.5.7-20468|
*'''requestResource''': the resource that called [[downloadFile]].
}}


==Source {مصدر}==
==Source==
The [[event system#Event source|source]] of this event is the [[root element]] of the resource that called [[downloadFile]].
The [[event system#Event source|source]] of this event is the [[root element]] of the resource that downloaded file.


==Example==  
==Example==  
This example plays a sound if it was downloaded successfully
This example plays a sound if it was downloaded successfully
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onDownloadFinish ( file, success )
addEventHandler("onClientFileDownloadComplete", root, function(file, success)
     if ( source == resourceRoot ) then                            -- if the file relates to this resource
     -- if the file relates to other resource
        if ( success ) then                                      -- if the file was downloaded successfully
    if source ~= resourceRoot then
            if ( file == "test.mp3" ) then                        -- if the file name is what we were expecting
         return
                currentTrack = playSound ( "test.mp3" )
            end
        else                                                      -- if the file wasn't downloaded successfully
            if ( file == "test.mp3" ) then
                outputChatBox ( "test.mp3 failed to download" )
            end
         end
     end
     end
end
 
addEventHandler ( "onClientFileDownloadComplete", root, onDownloadFinish )
    -- if the file download failed
    if not success then
        outputChatBox(file..' failed to download')
        return
    end
 
    -- check if filename ends with .mp3
    if file:sub(-4) ~= '.mp3' then
        return
    end
 
    -- if so, play the sound
    playSound(file)
end)
</syntaxhighlight>
</syntaxhighlight>



Latest revision as of 14:36, 21 May 2024

This event is triggered when a file has been downloaded after downloadFile has been successfully called.

Parameters

string fileName, bool success, resource requestResource
  • fileName: the file downloaded.
  • success: whether successful or not.
ADDED/UPDATED IN VERSION 1.5.7-20468 :

Source

The source of this event is the root element of the resource that downloaded file.

Example

This example plays a sound if it was downloaded successfully

addEventHandler("onClientFileDownloadComplete", root, function(file, success)
    -- if the file relates to other resource
    if source ~= resourceRoot then
        return
    end

    -- if the file download failed
    if not success then
        outputChatBox(file..' failed to download')
        return
    end

    -- check if filename ends with .mp3
    if file:sub(-4) ~= '.mp3' then
        return
    end

    -- if so, play the sound
    playSound(file)
end)

See Also

Other client events


Client event functions