DeleteResource: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
m (Wrong revision number, updated)
(Completely wrong example replaced by a useful and working example)
Line 18: Line 18:


==Example==
==Example==
This example checks to see if there's 2 resources.
This example adds a command to delete a certain resource (admins only, no spaces in resource name allowed).
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
addEventHandler("onResourceStart",getResourceRootElement(getThisResource()),
addCommandHandler("removeresource",
function()
function(player, cmd, name)
  resources = getResources() --Get all resources
    --Check if it is an admin using this command
  if (getResourceState(resources) == "loaded" and "running" or "loaded" and "starting") then --Checks to see if there's to resources that's loaded and running or loaded and starting up
    if not (isObjectInACLGroup("user."..getAccountName(getPlayerAccount(player)),aclGetGroup("Admin"))) then
    stopResource(resources)
        outputChatBox("You are not allowed to use this command", player)
    deleteResource(resources)
        return
  end
    end
    --Did the user pass a valid resource name?
    if not (name) or (name == "") and (name == " ") then
        outputChatBox("An invalid resource name has been passed (/removeresource <name>)", player)
        return
    end
    --Let us check if the resource name exists
    --Get all resources
    local resourceTable = getResources()
    for resourceKey, resourceValue in ipairs(resourceTable) do
        local resourceName = getResourceName(resourceValue)
        --Does the resource exist?
        if (name == resourceName) then
            --Stop the resource (maybe it is running)
            stopResource(resourceValue)
            --Delete it
            local deleted = deleteResource(name)
            if (deleted) then
                outputChatBox("Resource "..name.." has been successfully removed", player)
            else
                outputChatBox("There is an unknown problem with the resource", player)
            end
            --The function is finished and was successful, return to stop it
            return
        end
    end
    --If a resource with the specified name does not exist show an error message
    outputChatBox("The specified resource does not exist", player)
end)
end)
</syntaxhighlight>
</syntaxhighlight>

Revision as of 10:25, 17 March 2012

This function deletes a resource from the MTA memory and moves it to the "/resources-cache/trash/" directory.

Note: This function only works properly from 1.3.0-3912 (r3912). In case you are below r3912 you need to create a directory called "trash" inside of the "resources-cache" folder.

Syntax

bool deleteResource ( string resourceName )

Required Arguments

  • resourceName: The name of resource to delete.

Returns

Returns true if the resource has been deleted successfully, false otherwise.

Example

This example adds a command to delete a certain resource (admins only, no spaces in resource name allowed).

addCommandHandler("removeresource",
function(player, cmd, name)
    --Check if it is an admin using this command
    if not (isObjectInACLGroup("user."..getAccountName(getPlayerAccount(player)),aclGetGroup("Admin"))) then
        outputChatBox("You are not allowed to use this command", player)
        return
    end 
    --Did the user pass a valid resource name?
    if not (name) or (name == "") and (name == " ") then
        outputChatBox("An invalid resource name has been passed (/removeresource <name>)", player)
        return
    end
    --Let us check if the resource name exists
    --Get all resources
    local resourceTable = getResources() 
    for resourceKey, resourceValue in ipairs(resourceTable) do
        local resourceName = getResourceName(resourceValue)
        --Does the resource exist?
        if (name == resourceName) then
            --Stop the resource (maybe it is running)
            stopResource(resourceValue)
            --Delete it
            local deleted = deleteResource(name)
            if (deleted) then
                outputChatBox("Resource "..name.." has been successfully removed", player)
            else
                outputChatBox("There is an unknown problem with the resource", player)			
            end
            --The function is finished and was successful, return to stop it
            return
        end
    end
    --If a resource with the specified name does not exist show an error message
    outputChatBox("The specified resource does not exist", player)
end)

Requirements

Minimum server version 1.1.1-9.03316
Minimum client version n/a

Note: Using this feature requires the resource to have the above minimum version declared in the meta.xml <min_mta_version> section. e.g. <min_mta_version server="1.1.1-9.03316" />

See Also