DeleteResource: Difference between revisions
Jump to navigation
Jump to search
-ffs-Sniper (talk | contribs) (Completely wrong example replaced by a useful and working example) |
Fernando187 (talk | contribs) (Remove obsolete Requirements section) |
||
(8 intermediate revisions by 6 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
{{Server function}} | {{Server function}} | ||
This function deletes a resource from the MTA memory and moves it to the | This function deletes a resource from the MTA memory and moves it to the '''/resources-cache/trash/''' directory. | ||
==Syntax== | ==Syntax== | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
bool deleteResource ( string resourceName ) | bool deleteResource ( string resourceName ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
{{OOP|This function is a static function underneath the Resource class.|[[Resource]].delete}} | |||
===Required Arguments=== | ===Required Arguments=== | ||
*'''resourceName:''' The name of resource to delete. | *'''resourceName:''' The name of resource to delete. | ||
Line 20: | Line 16: | ||
This example adds a command to delete a certain resource (admins only, no spaces in resource name allowed). | This example adds a command to delete a certain resource (admins only, no spaces in resource name allowed). | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
addCommandHandler("removeresource", | addCommandHandler ( "removeresource", | ||
function( | function ( playerSource, commandName, name ) | ||
--Check if it is an admin using this command | --Check if it is an admin using this command | ||
if not | if not isObjectInACLGroup ( "user." .. getAccountName ( getPlayerAccount ( playerSource ) ), aclGetGroup ( "Admin" ) ) then | ||
outputChatBox("You are not allowed to use this command", | outputChatBox ( "You are not allowed to use this command", playerSource ) | ||
return | return | ||
end | end | ||
--Did the user pass a valid resource name? | --Did the user pass a valid resource name? | ||
if not | if not name or name == "" or name == " " then | ||
outputChatBox("An invalid resource name has been passed (/removeresource <name>)", | outputChatBox ( "An invalid resource name has been passed (/removeresource <name>)", playerSource ) | ||
return | return | ||
end | end | ||
--Let us check if the resource name exists | --Let us check if the resource name exists | ||
--Get all resources | --Get all resources | ||
local resourceTable = getResources() | local resourceTable = getResources ( ) | ||
for resourceKey, resourceValue in ipairs(resourceTable) do | for resourceKey, resourceValue in ipairs ( resourceTable ) do | ||
local resourceName = getResourceName(resourceValue) | local resourceName = getResourceName ( resourceValue ) | ||
--Does the resource exist? | --Does the resource exist? | ||
if | if name == resourceName then | ||
--Stop the resource (maybe it is running) | --Stop the resource (maybe it is running) | ||
stopResource(resourceValue) | stopResource ( resourceValue ) | ||
--Delete it | --Delete it | ||
local deleted = deleteResource(name) | local deleted = deleteResource ( name ) | ||
if | if deleted then | ||
outputChatBox("Resource "..name.." has been successfully removed", | outputChatBox ( "Resource " .. name .. " has been successfully removed", playerSource ) | ||
else | else | ||
outputChatBox("There is an unknown problem with the resource", | outputChatBox ( "There is an unknown problem with the resource", playerSource ) | ||
end | end | ||
--The function is finished and was successful, return to stop it | --The function is finished and was successful, return to stop it | ||
Line 53: | Line 49: | ||
end | end | ||
--If a resource with the specified name does not exist show an error message | --If a resource with the specified name does not exist show an error message | ||
outputChatBox("The specified resource does not exist", | outputChatBox ( "The specified resource does not exist", playerSource ) | ||
end) | end ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==See Also== | ==See Also== | ||
{{Resource_functions}} | {{Resource_functions|server}} |
Latest revision as of 15:33, 7 November 2024
This function deletes a resource from the MTA memory and moves it to the /resources-cache/trash/ directory.
Syntax
bool deleteResource ( string resourceName )
OOP Syntax Help! I don't understand this!
- Note: This function is a static function underneath the Resource class.
- Method: Resource.delete(...)
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 ( playerSource, commandName, name ) --Check if it is an admin using this command if not isObjectInACLGroup ( "user." .. getAccountName ( getPlayerAccount ( playerSource ) ), aclGetGroup ( "Admin" ) ) then outputChatBox ( "You are not allowed to use this command", playerSource ) return end --Did the user pass a valid resource name? if not name or name == "" or name == " " then outputChatBox ( "An invalid resource name has been passed (/removeresource <name>)", playerSource ) 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", playerSource ) else outputChatBox ( "There is an unknown problem with the resource", playerSource ) 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", playerSource ) end )
See Also
- addResourceConfig
- addResourceMap
- callRemote
- copyResource
- createResource
- deleteResource
- getResourceACLRequests
- getResourceInfo
- getResourceLastStartTime
- getResourceLoadFailureReason
- getResourceLoadTime
- getResourceMapRootElement
- getResourceOrganizationalPath
- getResources
- isResourceArchived
- isResourceProtected
- refreshResources
- removeResourceFile
- renameResource
- restartResource
- setResourceInfo
- startResource
- stopResource
- updateResourceACLRequest
- Shared
- abortRemoteRequest
- call
- fetchRemote
- getResourceConfig
- getResourceDynamicElementRoot
- getResourceExportedFunctions
- getResourceFromName
- getResourceName
- getResourceRootElement
- getResourceState
- getThisResource
- getRemoteRequests
- getRemoteRequestInfo