GetThisResource: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
m (Added comments to example usage)
 
(25 intermediate revisions by 13 users not shown)
Line 1: Line 1:
__NOTOC__
{{Server client function}}
{{Needs_Checking|Does it return a pointer to a resource or a resource object?}}
__NOTOC__
<!-- Describe in plain english what this function does. Don't go into details, just give an overview -->
This function retrieves the resource from which the function call was made.
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>
{{OOP|This function is a static function underneath the Resource class.|[[Resource]].getThis}}


===Returns===
===Returns===
<!-- Make this descriptive. Explain what cases will return false. If you're unsure, add a tag to it so we can check -->
Returns the resource in which the current script is.
Returns a resource object (or pointer?).


==Example==  
==Example==
<!-- Explain what the example is in a single sentance -->
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.
This example performs a check to see if the resource started is the right one:
<!-- 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">
<syntaxhighlight lang="lua">
addEventHandler("onResourceStart", getRootElement(), "onStartInScoresResource")
local thisResource = getThisResource()
function onStartInScoresResource(resource)
local resRoot = getResourceRootElement(thisResource)
    local thisResource = getThisResource() -- get the resource that this script is a part of
 
    if (resource == thisResource) then -- make sure the resource that started is the one this script is a part of
addEventHandler("onResourceStart", resRoot, function()
          -- Note: without this check, the operations below would execute whenever ANY resource is loaded
  local resourceName = getResourceName(thisResource)
          -- perform some operations this script will be used for, in this case keeping track of player scores and money:
  iprint("You are in the " .. resourceName .. " resource!")
          local players = getElementsByType("player")
  iprint(thisResource == resource) -- true
          for playerKey, playerValue in players do
  iprint(resRoot == resourceRoot) -- true
              setPlayerMoney(playerValue, 500)
end)
              setElementData(playerValue, "score", 0)
          end
    end
end
</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}}
{{Resource_functions}}
[[Category:Needs_Checking]]

Latest revision as of 19:40, 14 March 2023

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 ( )

OOP Syntax Help! I don't understand this!

Note: This function is a static function underneath the Resource class.
Method: Resource.getThis(...)


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.

local thisResource = getThisResource()
local resRoot = getResourceRootElement(thisResource)

addEventHandler("onResourceStart", resRoot, function()
   local resourceName = getResourceName(thisResource)
   iprint("You are in the " .. resourceName .. " resource!")
   iprint(thisResource == resource) -- true
   iprint(resRoot == resourceRoot) -- true
end)

See Also