OnClientFileDownloadComplete: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Improved example)
 
(13 intermediate revisions by 7 users not shown)
Line 1: Line 1:
__NOTOC__
{{Client event}}
{{Client event}}  
{{New items|3.0140|1.4|
 
__NOTOC__
my test example until I get a chance to document properly
This event is triggered when a file has been downloaded after [[downloadFile]] has been successfully called.
 
}}
==Parameters==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
currentTrack = nil
string fileName, bool success, resource requestResource
function onStart ( )
</syntaxhighlight>
downloadFile ( "test.mp3" )
end
addEventHandler ( "onClientResourceStart", getRootElement(), onStart )


function onDownloadFinish ( file, success )
*'''fileName''': the file downloaded.
if ( success ) then
*'''success''': whether successful or not.
if ( file == "test.mp3" ) then
{{New items|4.0157|1.5.7-20468|
currentTrack = playSound ( "test.mp3" )
*'''requestResource''': the resource that called [[downloadFile]].
end
}}
else
if ( file == "test.mp3" ) then
outputChatBox ( "test.mp3 failed to download" )
end
end
end
addEventHandler ( "onClientFileDownloadComplete", getRootElement(), onDownloadFinish )


==Source==
The [[event system#Event source|source]] of this event is the [[root element]] of the resource that downloaded file.


function onSoundStopped ( reason )
==Example==  
if ( source == currentTrack ) then
This example plays a sound if it was downloaded successfully
if ( reason == "destroyed" ) then
<syntaxhighlight lang="lua">
outputChatBox ( "sound destroyed" )
addEventHandler("onClientFileDownloadComplete", root, function(file, success)
elseif ( reason == "finished" ) then
    -- if the file relates to other resource
outputChatBox ( "end of sound" )
    if source ~= resourceRoot then
elseif ( reason == "paused" ) then
        return
outputChatBox ( "sound paused" )
    end
end
end
end
addEventHandler ( "onClientSoundStopped", getRootElement(), onSoundStopped )


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


function onSoundStarted ( reason )
    -- check if filename ends with .mp3
if ( source == currentTrack ) then
    if file:sub(-4) ~= '.mp3' then
if ( reason == "play" ) then
        return
outputChatBox ( "sound started" )
elseif ( reason == "resumed" ) then
outputChatBox ( "sound resumed" )
end
end
end
addEventHandler ( "onClientSoundStarted", getRootElement(), onSoundStarted )
 
 
function stopSoundFunction ()
stopSound ( currentTrack )
end   
addCommandHandler ( "stop1", stopSoundFunction )
 
function songPause()
    local pause = isSoundPaused ( currentTrack )
    if ( pause == true ) then
setSoundPaused ( currentTrack, false )
    else
setSoundPaused ( currentTrack, true )
     end
     end
end
addCommandHandler("pause", songPause)


function startSoundFunction ()
    -- if so, play the sound
currentTrack = playSound ( "test.mp3" )
    playSound(file)
end  
end)
addCommandHandler ( "start1", startSoundFunction )
</syntaxhighlight>
</syntaxhighlight>


Line 75: 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