Jump to content

problem with download function


John Smith

Recommended Posts

hello i found one code on forum not sure where but i have been using it and i tried editing it but theres a problem

local screenWidth,screenHeight = guiGetScreenSize() 
local downloadFiles = { 
    { "mods/alpha.txd" }, 
    { "mods/alpha.dff" }, 
} 
  
 function downloadState() 
 dxDrawText("Downloading... (0/2)",screenWidth,screenHeight,screenWidth,screenHeight,tocolor(255,255,255,255),1,"bankgothic") 
 end 
 addEventHandler("onClientRender",root,downloadState) 
  
for _, downFile in pairs( downloadFiles ) do 
    downloadFile ( downFile[1] ) 
    downloadState() 
end 
  
addEventHandler ( "onClientFileDownloadComplete", resourceRoot, 
    function ( file, success ) 
        if ( success ) then 
            -- alpha 
            if ( file == "mods/alpha.txd" and fileExists("mods/alpha.dff") ) or ( file == "mods/alpha.dff" and fileExists("mods/alpha.txd") ) then 
                local txdalp = engineLoadTXD ( "mods/alpha.txd" ) 
                local dffalp = engineLoadDFF ( "mods/alpha.dff", 602 ) 
                engineImportTXD ( txdalp, 602 ) 
                engineReplaceModel ( dffalp, 602 ) 
                removeEventHandler("onClientRender",root,downloadState) 
           end     
       end 
  end 
) 
  
function checkIfPlayerAlreadyHasDownloaded() 
if ( fileExists("mods/alpha.dff") and fileExists("mods/alpha.txd")) then 
outputChatBox("Loading carmods...") 
                local txdalp = engineLoadTXD ( "mods/alpha.txd" ) 
                local dffalp = engineLoadDFF ( "mods/alpha.dff", 602 ) 
                engineImportTXD ( txdalp, 602 ) 
                engineReplaceModel ( dffalp, 602 ) 
                else 
                outputChatBox("client has no carmods, and has triggered the download!") 
                for _, downFile in pairs( downloadFiles ) do 
    downloadFile ( downFile[1] ) 
    downloadState() 
end 
end 
end 
addEventHandler("onClientPlayerSpawn",root,checkIfPlayerAlreadyHasDownloaded) 

i have already previously downloaded files but when i spawn i need to wait 20 seconds of mta loading gif image and it spams chat with loading carmods...i cant even count how much there were,its whole chat like 10 times

i am not sure what is causing this freeze please help

Link to comment
  • Replies 58
  • Created
  • Last Reply

Top Posters In This Topic

You don't need to call downloadState().

Its bound to onClientRender, so it will be called on every frame automatically.

Instead, you could add the Handler there. But not inside the loop.

Also, whats with that for loop in line 12? Its not needed and wrong placed anyway.

Link to comment

so like this?

local screenWidth,screenHeight = guiGetScreenSize() 
local downloadFiles = { 
    { "mods/alpha.txd" }, 
    { "mods/alpha.dff" }, 
} 
  
 function downloadState() 
 dxDrawText("Downloading... (0/2)",screenWidth,screenHeight,screenWidth,screenHeight,tocolor(255,255,255,255),1,"bankgothic") 
 end 
  
  
addEventHandler ( "onClientFileDownloadComplete", resourceRoot, 
    function ( file, success ) 
        if ( success ) then 
            -- alpha 
            if ( file == "mods/alpha.txd" and fileExists("mods/alpha.dff") ) or ( file == "mods/alpha.dff" and fileExists("mods/alpha.txd") ) then 
                local txdalp = engineLoadTXD ( "mods/alpha.txd" ) 
                local dffalp = engineLoadDFF ( "mods/alpha.dff", 602 ) 
                engineImportTXD ( txdalp, 602 ) 
                engineReplaceModel ( dffalp, 602 ) 
                removeEventHandler("onClientRender",root,downloadState) 
           end     
       end 
  end 
) 
  
function checkIfPlayerAlreadyHasDownloaded() 
if ( fileExists("mods/alpha.dff") and fileExists("mods/alpha.txd")) then 
outputChatBox("Loading carmods...") 
                local txdalp = engineLoadTXD ( "mods/alpha.txd" ) 
                local dffalp = engineLoadDFF ( "mods/alpha.dff", 602 ) 
                engineImportTXD ( txdalp, 602 ) 
                engineReplaceModel ( dffalp, 602 ) 
                else 
                outputChatBox("client has no carmods, and has triggered the download!") 
                for _, downFile in pairs( downloadFiles ) do 
    downloadFile ( downFile[1] ) 
    addEventHandler("onClientRender",root,downloadState) 
end 
end 
end 
addEventHandler("onClientPlayerSpawn",root,checkIfPlayerAlreadyHasDownloaded) 

edit: im still getting huge freeze as explained in my previous post

Link to comment

Not sure why that would help, but also some mistake in that for loop.

  
for _, downFile in pairs( downloadFiles ) do 
    downloadFile ( downFile[1] ) 
end 
  
for _, downFile in pairs( downloadFiles ) do 
    downloadFile ( downFile ) 
end 
  
  
  
  

Also, don't add the handler inside of that loop.

Link to comment

i still have that freeze

local screenWidth,screenHeight = guiGetScreenSize() 
local downloadFiles = { 
    { "mods/alpha.txd" }, 
    { "mods/alpha.dff" }, 
} 
  
 function downloadState() 
 dxDrawText("Downloading... (0/2)",screenWidth,screenHeight,screenWidth,screenHeight,tocolor(255,255,255,255),1,"bankgothic") 
 end 
  
  
addEventHandler ( "onClientFileDownloadComplete", resourceRoot, 
    function ( file, success ) 
        if ( success ) then 
            -- alpha 
            if ( file == "mods/alpha.txd" and fileExists("mods/alpha.dff") ) or ( file == "mods/alpha.dff" and fileExists("mods/alpha.txd") ) then 
                local txdalp = engineLoadTXD ( "mods/alpha.txd" ) 
                local dffalp = engineLoadDFF ( "mods/alpha.dff", 602 ) 
                engineImportTXD ( txdalp, 602 ) 
                engineReplaceModel ( dffalp, 602 ) 
                removeEventHandler("onClientRender",root,downloadState) 
           end     
       end 
  end 
) 
  
function checkIfPlayerAlreadyHasDownloaded() 
if ( fileExists("mods/alpha.dff") and fileExists("mods/alpha.txd")) then 
                local txdalp = engineLoadTXD ( "mods/alpha.txd" ) 
                local dffalp = engineLoadDFF ( "mods/alpha.dff", 602 ) 
                engineImportTXD ( txdalp, 602 ) 
                engineReplaceModel ( dffalp, 602 ) 
                else 
                outputChatBox("client has no carmods, and has triggered the download!") 
for _, downFile in pairs( downloadFiles ) do 
    downloadFile ( downFile ) 
end 
end 
end 
addEventHandler("onClientPlayerSpawn",root,checkIfPlayerAlreadyHasDownloaded) 
addEventHandler("onClientRender",root,downloadState) 

i used your fix bonsai but i dont understand the handler part,onClientRender is attached to total another function

and im having freeze

Link to comment

I'm just thinking the handler to draw the download message isn't needed before it actually starts.

Also, its not needed anymore after the download is finished. But its fine, since you remove it there.

But if you add the handler inside of a loop, you add it multiple times.

Even though this outputs some error in the debuglog and won't work anyway, its just more "clean" this way.

EDIT:

Did you try /debugscript 3?

It should show some more information, about some infinite running stuff or something.

Link to comment

my whole pc is lagging i couldnt even see anything in debugscript 3 because it wasnt possible because of loading and on console it says nothing, only thing which is getting infinite or overflow is my spawn script in which idk why i get c stack overflow but its attached to onPlayerSpawn but it never caused that 1 minute til crash freeze

Link to comment

yeah, and i have replaced those both with setElementPosition onPlayerWasted with a timer, thanks

but theres a problem when i didn't download yet the carmod, it outputs that i have triggered the download in chat, but it isnt really downloading i believe, no debugscript 3 errors and my code is same as above posted

Link to comment

Are you also sure you have this in meta.xml:

<file src="filename.filetype" download="false" /> 

?

Also, even if the file has been downloaded, then 'onClientFileDownloadComplete' will still run. Check in your files or the file was downloaded. (In the folder where you have installed MTA. e.g.: E:\Program Files (x86)\Rockstar Games\MTA San Andreas 1.4\mods\deathmatch\resources )

I'm sure you already did, but just to be sure. :P

Link to comment

As far as I know you don't even have to check if its existing already, since it won't download but still trigger that finish function in that case.

Try this with one file only, maybe without importing, also put an "else" to that if (sucess) then thing.

Link to comment

fileExists is used because onClientFileDownloadComplete is triggered when ONE by ONE file is downloaded, so if i remove the checks there is possibility of launching carmod without a texture or its shape so it would be fucked

and i believe cars wouldn't even be modded if there wasn't import txd and dff function in my script

Link to comment

Yeah, I know that. I was talking about checkIfPlayerAlreadyHasDownloaded(), sorry.

This downloadFile function seems to cause a lot of problems.

I never used it so I don't know if its caused by people being unable to use it or general problems.

Link to comment

Try running this, not sure if it's going to make a difference though:

local screenWidth,screenHeight = guiGetScreenSize() 
local downloadFiles = { "mods/alpha.txd", "mods/alpha.dff" } 
      
function downloadState() 
    dxDrawText("Downloading... (0/2)",screenWidth,screenHeight,screenWidth,screenHeight,tocolor(255,255,255,255),1,"bankgothic") 
end 
  
--[[ This is handled in onClientPlayerSpawn 
for _, downFile in pairs( downloadFiles ) do 
    downloadFile ( downFile ) 
    downloadState() 
end ]] 
      
addEventHandler ( "onClientFileDownloadComplete", root, function ( file, success ) 
    if ( success and fileExists ( "mods/alpha.dff" ) and fileExists ( "mods/alpha.txd" ) ) then 
        local txdalp = engineLoadTXD ( "mods/alpha.txd" ) 
        local dffalp = engineLoadDFF ( "mods/alpha.dff", 602 ) 
        engineImportTXD ( txdalp, 602 ) 
        engineReplaceModel ( dffalp, 602 ) 
        removeEventHandler("onClientRender",root,downloadState) 
    end     
end ) 
      
function checkIfPlayerAlreadyHasDownloaded() 
    if ( fileExists("mods/alpha.dff") and fileExists("mods/alpha.txd")) then 
        outputChatBox("Loading carmods...") 
        local txdalp = engineLoadTXD ( "mods/alpha.txd" ) 
        local dffalp = engineLoadDFF ( "mods/alpha.dff", 602 ) 
        engineImportTXD ( txdalp, 602 ) 
        engineReplaceModel ( dffalp, 602 ) 
    else 
        outputChatBox("client has no carmods, and has triggered the download!") 
        for _, downFile in pairs( downloadFiles ) do 
            downloadFile ( downFile ) 
            addEventHandler ( "onClientRender", root, downloadState ) 
        end 
    end 
end 
addEventHandler("onClientPlayerSpawn", localPlayer, checkIfPlayerAlreadyHasDownloaded ) 

Link to comment

Here is your problem:

for _, downFile in pairs(downloadFiles) do 
            outputChatBox(tostring(downFile)) 
            lol = downloadFile(downFile) 
            outputChatBox(tostring(lol)) 
        end 

'downFile' is a table. Which means, you have to fill this in by 'downloadFile':

downloadFile(downFile[1]) 

EDIT: Damn xXMADEXx, you are earlier than me.

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