EngineFreeModel: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "bool = engineFreeModel(id) https://github.com/multitheftauto/mtasa-blue/commit/34d4eed068f9c957d977044e8c7fd36e087ff09a")
 
(Remove obsolete Requirements section)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
bool = engineFreeModel(id)
__NOTOC__
{{Client function}}
{{New feature/item|3.0158|1.5.7|20147|This function is used to un-assign the specified model ID from the [[engineRequestModel]] assignment.}}


https://github.com/multitheftauto/mtasa-blue/commit/34d4eed068f9c957d977044e8c7fd36e087ff09a
==Syntax==
<syntaxhighlight lang="lua">
bool engineFreeModel ( int modelID )
</syntaxhighlight>
===Required Arguments===
*'''modelID''': the model ID you want to have un-assigned.
 
===Returns===
Returns ''true'' if the model was successfully freed, ''false'' otherwise.
 
==Remarks==
If there are elements created on the game world whose model ID does match the one that you want to release using the engineFreeModel function then their model ID is reset to a well-known default before releasing the internal model data. This is 0 for peds, 1337 for objects and 400 for vehicles. By doing this there are no model data leaks if using the engineFreeModel function while elements are still streamed-in. Creators of custom model systems have to be careful though as custom model IDs should only be released if all elements that use said ID are streamed-out or destroyed.
 
==Example==
This example creates a ped and then gives you the opportunity to change its model. If the resource stops, then the IDs allocated will be deallocated. Use ''/cap'' for creating the ped and ''/sap'' to skin the ped. You will need some skins added to a folder and to the meta.xml for ''/sap'' to work.
<syntaxhighlight lang="lua">
local peds = {}
function createAllocatedPed()
    local x, y, z = getElementPosition(localPlayer)
    local id = engineRequestModel("ped")
    peds[id] = createPed(id, x+0.5, y, z+0.5)
    outputChatBox("New ped with ID "..id.." created.")
end
addCommandHandler("cap", createAllocatedPed, false, false)
 
function skinAllocatedPeds()
    local txd, dff;
    for id,ped in pairs(peds) do
        if fileExists("skins/" .. id .. ".txd") and fileExists("skins/" .. id .. ".dff") then
            txd = engineLoadTXD("skins/" .. id .. ".txd")
            engineImportTXD(txd, id)
            dff = engineLoadDFF("skins/" .. id .. ".dff")
            engineReplaceModel(dff, id)
            outputChatBox("Model ID "..id.." changed correctly.")
        else
            outputChatBox("Model ID "..id.." couldn't change. REASON: skins/" .. id .. ".txd or skins/" .. id .. ".dff does not exist.")
        end
    end
end
addCommandHandler("sap", skinAllocatedPeds, false, false)
 
function onStop()
    for id,ped in pairs(peds) do
        engineFreeModel(id)
    end
end
addEventHandler("onClientResourceStop", resourceRoot, onStop)
</syntaxhighlight>
 
==See Also==
{{Engine functions}}

Latest revision as of 17:14, 7 November 2024

This function is used to un-assign the specified model ID from the engineRequestModel assignment.

Syntax

bool engineFreeModel ( int modelID )

Required Arguments

  • modelID: the model ID you want to have un-assigned.

Returns

Returns true if the model was successfully freed, false otherwise.

Remarks

If there are elements created on the game world whose model ID does match the one that you want to release using the engineFreeModel function then their model ID is reset to a well-known default before releasing the internal model data. This is 0 for peds, 1337 for objects and 400 for vehicles. By doing this there are no model data leaks if using the engineFreeModel function while elements are still streamed-in. Creators of custom model systems have to be careful though as custom model IDs should only be released if all elements that use said ID are streamed-out or destroyed.

Example

This example creates a ped and then gives you the opportunity to change its model. If the resource stops, then the IDs allocated will be deallocated. Use /cap for creating the ped and /sap to skin the ped. You will need some skins added to a folder and to the meta.xml for /sap to work.

local peds = {}
function createAllocatedPed()
    local x, y, z = getElementPosition(localPlayer)
    local id = engineRequestModel("ped")
    peds[id] = createPed(id, x+0.5, y, z+0.5)
    outputChatBox("New ped with ID "..id.." created.")
end
addCommandHandler("cap", createAllocatedPed, false, false)

function skinAllocatedPeds()
    local txd, dff;
    for id,ped in pairs(peds) do
        if fileExists("skins/" .. id .. ".txd") and fileExists("skins/" .. id .. ".dff") then
            txd = engineLoadTXD("skins/" .. id .. ".txd")
            engineImportTXD(txd, id)
            dff = engineLoadDFF("skins/" .. id .. ".dff")
            engineReplaceModel(dff, id)
            outputChatBox("Model ID "..id.." changed correctly.")
        else
            outputChatBox("Model ID "..id.." couldn't change. REASON: skins/" .. id .. ".txd or skins/" .. id .. ".dff does not exist.")
        end
    end
end
addCommandHandler("sap", skinAllocatedPeds, false, false)

function onStop()
    for id,ped in pairs(peds) do
        engineFreeModel(id)
    end
end
addEventHandler("onClientResourceStop", resourceRoot, onStop)

See Also