DownloadFile: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(3 intermediate revisions by 2 users not shown)
Line 2: Line 2:
__NOTOC__
__NOTOC__
{{New items|3.0140|1.4|
{{New items|3.0140|1.4|
This function downloads a file from the HTTP server. This can only be used on files on the HTTP server associated with the MTA server and will only download files from within the folder of the resource that is calling it. The '''file''' should also be included in meta.xml with the '''download''' attribute set to "false", see [[meta.xml]] for more details. If the file has been previously downloaded and the CRC matches, the file will not be downloaded again but [[onClientFileDownloadComplete]] will still run.
This function ensures the requested resource file is correct and then triggers [[onClientFileDownloadComplete]]. If the file has been previously downloaded and the CRC matches, the file will not be downloaded again but [[onClientFileDownloadComplete]] will still run. The '''file''' should also be included in the resource meta.xml with the '''download''' attribute set to "false", see [[meta.xml]] for more details.
}}
}}
 
{{Tip|If you are only using [[downloadFile]] to download mod files after other resources, then do not use [[downloadFile]], and instead set<br/> '<download_priority_group>-1</download_priority_group>' in the resource [[meta.xml]]}}
{{Note|This function may cause performance issues with client and/or server.}}
{{Tip|Avoid using fileExists before calling [[downloadFile]]. Always call [[downloadFile]] and handle the result in [[onClientFileDownloadComplete]]}}
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 21: Line 23:
-- the function is called on resource start
-- the function is called on resource start
function onThisResourceStart ( )
function onThisResourceStart ( )
     downloadFile ( "test.txt" )
     downloadFile ( "test.xml" )
end
end
addEventHandler ( "onClientResourceStart", resourceRoot, onThisResourceStart )
addEventHandler ( "onClientResourceStart", resourceRoot, onThisResourceStart )

Latest revision as of 00:41, 3 February 2017

This function ensures the requested resource file is correct and then triggers onClientFileDownloadComplete. If the file has been previously downloaded and the CRC matches, the file will not be downloaded again but onClientFileDownloadComplete will still run. The file should also be included in the resource meta.xml with the download attribute set to "false", see meta.xml for more details.

[[{{{image}}}|link=|]] Tip: If you are only using downloadFile to download mod files after other resources, then do not use downloadFile, and instead set
'<download_priority_group>-1</download_priority_group>' in the resource meta.xml
[[{{{image}}}|link=|]] Note: This function may cause performance issues with client and/or server.
[[{{{image}}}|link=|]] Tip: Avoid using fileExists before calling downloadFile. Always call downloadFile and handle the result in onClientFileDownloadComplete

Syntax

bool downloadFile ( string fileName )

Required Arguments

  • fileName: A string referencing the name of the file to download

Returns

Returns true if file download has been queued, false otherwise.

Example

Example 1: This client side event downloads a file when the current resource has started.

-- the function is called on resource start
function onThisResourceStart ( )
    downloadFile ( "test.xml" )
end
addEventHandler ( "onClientResourceStart", resourceRoot, onThisResourceStart )

See Also