GetResourceState: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Added the new states "starting" and "stopping")
m (Improved example)
 
(7 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Server function}}
{{Server client function}}
__NOTOC__
__NOTOC__
<!-- Describe in plain english what this function does. Don't go into details, just give an overview -->
This function returns the state of a given resource
This function returns the state of a given resource
==Syntax==  
 
<!-- NOTE: don't use 'special' names for variable names, e.g. you shouldn't be writing things like 'player player, vehicle vehicle', instead write something like 'player thePlayer, vehicle vehicleToGetInto'. This is less confusing and prevents the syntax highlighting being odd -->
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string getResourceState ( resource theResource )  
string getResourceState ( resource theResource )  
Line 10: Line 9:


===Required Arguments===  
===Required Arguments===  
*'''theResource:''' The resource you wish to get the name of.
*'''theResource:''' The resource you wish to get the state of.
 
{{OOP||[[resource]]:getState|state}}
===Returns===
===Returns===
If successful returns a string with the resource state in it, ''false'' otherwise.
If successful returns a string with the resource state in it, ''false'' otherwise.
Line 17: Line 16:
*'''loaded'''
*'''loaded'''
*'''running'''
*'''running'''
{{New feature|3|DP3|
*'''starting'''
*'''starting'''
*'''stopping'''
*'''stopping'''
}}
*'''failed to load''' - Use [[getResourceLoadFailureReason]] to find out why it failed.


==Example==  
==Example==  
<section class="server" name="Server" show="true">
This example returns the state of a given resource. Syntax: ''/state <Resource Name>''
This example returns the state of a given resource. Syntax: ''/state <Resource Name>''
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function getState( player, command, sz )
function getState( player, command, resourceName )
if sz then
if not resourceName then
local bFound = false
outputChatBox( "Syntax: " .. command .. " [resource name]", player, 255, 0, 0 )
local resourceTable = getResources() -- get a table of resources
return
end
for resourceKey, resourceValue in ipairs( resourceTable ) do
local resource = getResourceFromName( resourceName )
-- iterate through the table and output each resource's name
if not resource then
local name = getResourceName( resourceValue )
outputChatBox( "Error: No resource with name " .. resourceName .. " exists.", player, 255, 0, 0 )
if string.lower( name ) == string.lower( sz ) then
return
outputChatBox( "Resource State: " .. name .. " is currently " .. getResourceState( resourceValue ), player, 0, 0, 255 )
bFound = true
break
end
end
if bFound ~= true then
outputChatBox( "Error: No resource found named: " .. sz, player, 255, 0, 0 )
end
else
outputChatBox( "Error: You did not specify a resource to check", player, 255, 0, 0 )
end
end
local state = getResourceState( resource )
outputChatBox( "Resource " .. resourceName .. " is " .. state, player, 0, 0, 255 )
end
end


addCommandHandler( "state", getState )
addCommandHandler( "state", getState )
</syntaxhighlight>
</syntaxhighlight>
</section>


==See Also==
==See Also==
{{Resource functions}}
{{Resource functions}}

Latest revision as of 18:10, 21 March 2024

This function returns the state of a given resource

Syntax

string getResourceState ( resource theResource ) 

Required Arguments

  • theResource: The resource you wish to get the state of.

OOP Syntax Help! I don't understand this!

Method: resource:getState(...)
Variable: .state


Returns

If successful returns a string with the resource state in it, false otherwise. The state can be one of:

Example

This example returns the state of a given resource. Syntax: /state <Resource Name>

function getState( player, command, resourceName )
	if not resourceName then
		outputChatBox( "Syntax: " .. command .. " [resource name]", player, 255, 0, 0 )
		return
	end
	local resource = getResourceFromName( resourceName )
	if not resource then
		outputChatBox( "Error: No resource with name " .. resourceName .. " exists.", player, 255, 0, 0 )
		return
	end
	local state = getResourceState( resource )
	outputChatBox( "Resource " .. resourceName .. " is " .. state, player, 0, 0, 255 )
end

addCommandHandler( "state", getState )

See Also