Jump to content

Replacing models onClientFileDownloadComplete


Gravestone

Recommended Posts

I'm trying to make a option for players to download mods. The files are downloaded but if I restart the resource or the player joins then it spams the debug with 'attempt to load '..file name..' before 'onClientFileDownloadComplete' event. Is there anyway I can prevent my debug from getting spammed?

weapons= { { file = "335" }, { file = "346" }, { file = "347" }, { file = "348" }, { file = "349" }, { file = "350" }, { file = "351" }, { file = "352" },
{ file = "353" }, { file = "355" }, { file = "356" }, { file = "357" }, { file = "358" }, { file = "359" },	{ file = "360" }, { file = "362" },	{ file = "363" },
{ file = "372" }}
function downloadComplete(file, success)
	if (source == getResourceRootElement(getThisResource())) then
	for index, weapon in pairs(weapons) do
		if file == "weapons/"..weapon.file..".txd" or file == "weapons/"..weapon.file..".dff" then
				setTimer(replaceModels, 1000, 1)
				
			end	
		end
	end
end	
addEventHandler("onClientFileDownloadComplete", root, downloadComplete)

function replaceModels()
		for index, weapon in pairs(weapons) do
			if fileExists("weapons/"..weapon.file..".txd") and fileExists("weapons/"..weapon.file..".dff") then
				txd = engineLoadTXD("weapons/"..weapon.file..".txd")
				engineImportTXD(txd, weapon.file)
				dff = engineLoadDFF("weapons/"..weapon.file..".dff")
				engineReplaceModel(dff, weapon.file)
				
		else
			outputDebugString("Weapon file do not not exist for client "..getPlayerName(localPlayer))
		end
	end
end	
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), replaceModels)

 

Link to comment

Use downloadFile everytime and remove addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), replaceModels).

I think MTA can handle it automatically like that.

By the way, outputDebugString with user's name is useless there, since it's client-side, so you'll always get only your own name.

Link to comment

Try this:

 

addEvent("onClientReplaceStuff", true )

weapons= { { file = "335" }, { file = "346" }, { file = "347" }, { file = "348" }, { file = "349" }, { file = "350" }, { file = "351" }, { file = "352" },
    { file = "353" }, { file = "355" }, { file = "356" }, { file = "357" }, { file = "358" }, { file = "359" },	{ file = "360" }, { file = "362" },	{ file = "363" },
    { file = "372" }}
    function downloadComplete(file, success)
    	if (source == getResourceRootElement(getThisResource())) then
    	for index, weapon in pairs(weapons) do
    		if file == "weapons/"..weapon.file..".txd" or file == "weapons/"..weapon.file..".dff" then
    				setTimer(replaceModels, 1000, 1)
    				if ( index == #weapons ) then
          				triggerEvent("onClientReplaceStuff", root, true )
          				break
          			end
    			end	
    		end
    	end
    end	
    addEventHandler("onClientFileDownloadComplete", root, downloadComplete)
    function replaceModels( b )
    	if ( b ) then
    		for index, weapon in pairs(weapons) do
    			if fileExists("weapons/"..weapon.file..".txd") and fileExists("weapons/"..weapon.file..".dff") then
    				txd = engineLoadTXD("weapons/"..weapon.file..".txd")
    				engineImportTXD(txd, weapon.file)
    				dff = engineLoadDFF("weapons/"..weapon.file..".dff")
    				engineReplaceModel(dff, weapon.file)
    				
    		else
    			outputDebugString("Weapon file do not not exist for client "..getPlayerName(localPlayer))
    		end
    	end
    	end
    end	
    addEventHandler("onClientReplaceStuff", root, replaceModels)

 

Link to comment

The solution is:

1) Always call downloadFile for files with download="false".

2) Only use the file after onClientFileDownloadComplete is triggered.

3) You are using fileExists to skip downloadFile, but fileExists does not check the file content. downloadFile automatcially checks the existing file first, and will immediately trigger onClientFileDownloadComplete if download is not required.

 

Just remove  fileExists().

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