OnClientFileDownloadComplete: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Improved example)
 
(14 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__
{{Client event}}
{{Client event}}  
{{New items|3.0140|1.4|
__NOTOC__  
This event is triggered when a file has been downloaded after [[downloadFile]] has been successfully called.
}}
==Parameters==
<syntaxhighlight lang="lua">
string fileName, bool success, resource requestResource
</syntaxhighlight>
 
*'''fileName''': the file downloaded.
*'''success''': whether successful or not.
{{New items|4.0157|1.5.7-20468|
*'''requestResource''': the resource that called [[downloadFile]].
}}


my test example until I get a chance to document properly
==Source==
The [[event system#Event source|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
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function onStart ( )
addEventHandler("onClientFileDownloadComplete", root, function(file, success)
outputChatBox ( "downloading test.mp3" )
    -- if the file relates to other resource
downloadFile ( "test.mp3" )
    if source ~= resourceRoot then
end
        return
addEventHandler ( "onClientResourceStart", getRootElement(), onStart )
    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


function onDownloadFinish ( file, success )
    -- if so, play the sound
if ( success ) then
    playSound(file)
if ( file == "test.mp3" ) then
end)
outputChatBox ( "test.mp3 downloaded successfully" )
playSound ( "test.mp3" )
outputChatBox ( "playing test.mp3" )
end
else
if ( file == "test.mp3" ) then
outputChatBox ( "test.mp3 failed to download" )
end
end
end
addEventHandler ( "onClientFileDownloadComplete", getRootElement(), onDownloadFinish )
</syntaxhighlight>
</syntaxhighlight>


Line 30: Line 46:
===Other client events===
===Other client events===
{{Client_other_events}}
{{Client_other_events}}
===Client event functions===
{{Client_event_functions}}

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

Shared