EngineReplaceModel: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (→‎Example: Indentation)
(Replace examples into one solution using tables.)
Line 27: Line 27:


==Example==  
==Example==  
'''Example 1:'''
<section name="Client" class="client" show="true">
<section name="Client" class="client" show="true">
Client-side example for replacing vehicle model and texture with custom ones.
This example allows you to replace single or many models (with ability to toggle TXD filtering/DFF alpha transparency).
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function ReplaceVehicle()
function onClientResourceStartReplaceModels()
    outputChatBox ("> replacing the euros vehicle")
local modelsToReplace = {
{ -- replace object
colFile = "object.col",
txdFile = "object.txd",
dffFile = "object.dff",
modelID = 1337,
alphaTransparency = false,
filteringEnabled = true,
},
{ -- replace vehicle
colFile = false, -- if .col is not present set to false/nil
txdFile = "vehicle.txd",
dffFile = "vehicle.dff",
modelID = 434,
alphaTransparency = false,
filteringEnabled = true,
},
}


    txd = engineLoadTXD ("data/euros.txd")
for assetID = 1, #modelsToReplace do
    engineImportTXD (txd, 587)
local modelData = modelsToReplace[assetID]
    dff = engineLoadDFF ("data/euros.dff")
local modelCol = modelData.colFile
    engineReplaceModel (dff, 587)
local modelTxd = modelData.txdFile
end
local modelDff = modelData.dffFile
local modelID = modelData.modelID
 
if modelCol then
local colData = engineLoadCOL(modelCol)
 
if colData then
engineReplaceCOL(colData, modelID)
end
end


addEvent ("replaceVeh", true)
if modelTxd then
addEventHandler ("replaceVeh", root, ReplaceVehicle)
local filteringEnabled = modelData.filteringEnabled
</syntaxhighlight>
local txdData = engineLoadTXD(modelTxd, filteringEnabled)
</section>
<section name="Server" class="server" show="true">
Server-side example function for triggering the replace.
<syntaxhighlight lang="lua">
function ReplaceCommand()
    triggerClientEvent ("replaceVeh", root, replaceVeh)
end
addCommandHandler ("replace", ReplaceCommand)
</syntaxhighlight>
</section>


'''Example 2:'''
if txdData then
<section name="Client" class="client" show="true">
engineImportTXD(txdData, modelID)
Client-side example for replacing weapons with custom mods.
end
<syntaxhighlight lang="lua">
end
function ReplaceWeapon()
    txd = engineLoadTXD ("m4.txd")
    engineImportTXD (txd, 356)
    dff = engineLoadDFF ("m4.dff", 356) -- use weapon model ID, not weapon ID (model ID from https://wiki.multitheftauto.com/wiki/Weapons)
    engineReplaceModel (dff, 356) -- Likewise, model ID, for M4 as example it's 356
end
addEventHandler ("onClientResourceStart", getResourceRootElement(getThisResource()), replaceWeapon)
</syntaxhighlight>
</section>


'''Example 3:'''
if modelDff then
<section name="Client" class="client" show="true">
local dffData = engineLoadDFF(modelDff)
Client-Side example for replacing object collision, texture and model with custom ones.
<syntaxhighlight lang="lua">
function ReplaceObject()
    col = engineLoadCOL ("MyModel.col")
    txd = engineLoadTXD ("MyModel.txd")
    dff = engineLoadDFF ("MyModel.dff")


    engineReplaceCOL (col, 1234)
if dffData then
    engineImportTXD (txd, 1234)
local alphaTransparency = modelData.alphaTransparency
    engineReplaceModel (dff, 1234) -- replace the model at least
end
engineReplaceModel(dffData, modelID, alphaTransparency)
addEvent ("replaceObj", true)
end
addEventHandler ("replaceObj", root, ReplaceObject)
end
</syntaxhighlight>
end
</section>
<section name="Server" class="server" show="true">
Server-side example function for triggering the replace.
<syntaxhighlight lang="lua">
function ReplaceCommand()
    triggerClientEvent ("replaceObj", root, replaceObj)
end
end
addCommandHandler ("replace", ReplaceCommand)
addEventHandler("onClientResourceStart", resourceRoot, onClientResourceStartReplaceModels)
</syntaxhighlight>
</syntaxhighlight>
</section>
</section>

Revision as of 10:51, 17 October 2022

[[{{{image}}}|link=|]] Note: Please note the loading order that is used in the examples as other orders can cause collisions, textures or the DFF not to load due to technical limitations

This function replaces the given model ID with the model contained in a DFF file loaded by engineLoadDFF. This function supports vehicles, objects, peds and players but not CJ clothing and body parts.

To replace weapon models you must use their object IDs, not weapon IDs. There is a weapon model list available at weapons.

[[{{{image}}}|link=|]] Note:
  • Default GTA map objects behave inconsistently when using this function on them. If you want to replace models in the original GTA map, use one of the methods shown here.

Syntax

bool engineReplaceModel ( dff theModel, int modelID [, bool alphaTransparency = false ] )

OOP Syntax Help! I don't understand this!

Method: dff:replace(...)


Required Arguments

  • theModel: The model to replace the given model ID with
  • modelID: The model it to replace the model of

Optional Arguments

  • alphaTransparency: Set to true if model uses semi-transparent textures, e.g. windows. This will ensure other objects behind the semi-transparent textures are rendered correctly. (Can slightly impact performance, so only set when required)

Returns

Returns true if the model was successfully replaced, false if it failed for some reason, ie. the DFF or the model ID is not valid.

Example

Click to collapse [-]
Client

This example allows you to replace single or many models (with ability to toggle TXD filtering/DFF alpha transparency).

function onClientResourceStartReplaceModels()
	local modelsToReplace = {
		{ -- replace object
			colFile = "object.col",
			txdFile = "object.txd",
			dffFile = "object.dff",
			modelID = 1337,
			alphaTransparency = false,
			filteringEnabled = true,
		},
		{ -- replace vehicle
			colFile = false, -- if .col is not present set to false/nil
			txdFile = "vehicle.txd",
			dffFile = "vehicle.dff",
			modelID = 434,
			alphaTransparency = false,
			filteringEnabled = true,
		},
	}

	for assetID = 1, #modelsToReplace do
		local modelData = modelsToReplace[assetID]
		local modelCol = modelData.colFile
		local modelTxd = modelData.txdFile
		local modelDff = modelData.dffFile
		local modelID = modelData.modelID

		if modelCol then
			local colData = engineLoadCOL(modelCol)

			if colData then
				engineReplaceCOL(colData, modelID)
			end
		end

		if modelTxd then
			local filteringEnabled = modelData.filteringEnabled
			local txdData = engineLoadTXD(modelTxd, filteringEnabled)

			if txdData then
				engineImportTXD(txdData, modelID)
			end
		end

		if modelDff then
			local dffData = engineLoadDFF(modelDff)

			if dffData then
				local alphaTransparency = modelData.alphaTransparency
				
				engineReplaceModel(dffData, modelID, alphaTransparency)
			end
		end
	end
end
addEventHandler("onClientResourceStart", resourceRoot, onClientResourceStartReplaceModels)

See Also