GetThisResource: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
mNo edit summary
(25 intermediate revisions by 12 users not shown)
Line 1: Line 1:
__NOTOC__  
{{Server client function}}
<!-- Describe in plain english what this function does. Don't go into details, just give an overview -->
__NOTOC__
This fake function is for use with blah & blah and does blahblahblabhalbhl
This function retrieves the resource from which the function call was made.
{{Note| Every resource has a predefined global variable called ''resource'' that contains the resource pointer for that resource, in other words, the value that this function returns.}}


==Syntax==  
==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 -->
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
resource getThisResource ( )
resource getThisResource ( )
</syntaxhighlight>  
</syntaxhighlight>


===Required Arguments===  
===Returns===
<!-- List each argument one per line. This should be the argument's name as in the argument list above, NOT the argument's data type -->
Returns the resource in which the current script is.
*'''argumentName:''' description


<!-- Only include this section below if there are optional arguments -->
==Example==
===Optional Arguments===
This example retrieves the current resource's root element and attaches it to an onResourceStart event handler. This causes the event handler to get called only when the ''current'' resource is started rather than when ''any'' resource is started, thereby reducing unnecessary overhead.
{{OptionalArg}}
<syntaxhighlight lang="lua">
*'''argumentName2:''' description
-- get the root element of this resource (the resource that the script is a part of)
*'''argumentName3:''' description
resourceRoot = getResourceRootElement(getThisResource())
--getThisResource() is the same thing as MTA's secret global variable
--"resource" as explained in the note above.


===Returns===
-- create a function to handle the command
<!-- Make this descriptive. Explain what cases will return false. If you're unsure, add a tag to it so we can check -->
function onResourceCommand()
Returns ''true'' if blah, ''false'' otherwise. <br \>
  local thisResource = getThisResource()
false on fail
  local resourceName = getResourceName(thisResource)
  outputChatBox("You are in the " .. resourceName .. " resource!")
end


==Example==
-- add the command handler
<!-- Explain what the example is in a single sentance -->
addCommandHandler("what", onResourceCommand)
This example does...
<!-- Add the code below, an emphasis should be on making it clear, not optimized. You could provide two versions if you wish, one clear and well commented, the other optimized -->
<syntaxhighlight lang="lua">
--This line does...
blabhalbalhb --abababa
--This line does this...
mooo
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
<!-- Change FunctionArea to the area that this function is in on the main function list page, e.g. Server, Player, Vehicle etc -->
{{Resource_functions}}
{{FunctionArea_functions}}
[[Category:Incomplete]] -- leave this unless you complete the function

Revision as of 04:41, 11 August 2019

This function retrieves the resource from which the function call was made.

[[{{{image}}}|link=|]] Note: Every resource has a predefined global variable called resource that contains the resource pointer for that resource, in other words, the value that this function returns.

Syntax

resource getThisResource ( )

Returns

Returns the resource in which the current script is.

Example

This example retrieves the current resource's root element and attaches it to an onResourceStart event handler. This causes the event handler to get called only when the current resource is started rather than when any resource is started, thereby reducing unnecessary overhead.

-- get the root element of this resource (the resource that the script is a part of)
resourceRoot = getResourceRootElement(getThisResource()) 
--getThisResource() is the same thing as MTA's secret global variable 
--"resource" as explained in the note above. 

-- create a function to handle the command
function onResourceCommand()
   local thisResource = getThisResource()
   local resourceName = getResourceName(thisResource)
   outputChatBox("You are in the " .. resourceName .. " resource!")
end

-- add the command handler
addCommandHandler("what", onResourceCommand)

See Also