Call: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Add information about not using exports in render events)
 
(36 intermediate revisions by 19 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
<!-- Describe in plain english what this function does. Don't go into details, just give an overview -->
{{Server client function}}
This fake function is for use with blah & blah and does blahblahblabhalbhl
{{Important Note|Calls (exports) may incur a performance overhead - they are not equivalent in performance to calling functions in the same resource. Do not use exports in render events (like [[onClientRender]]), or in fast processing logic, unless you want to kill performance.}}
{{Important Note|The sourceResource and sourceResourceRoot "hidden" variables are available even if you use exports.*:*}}
{{Important Note|Using this function straight away on resource start might cause elements to not be passed properly, use [[setTimer]] in order to delay function execution and to avoid this issue.}}
 
This function is used to call a function from another resource (which must be running).
 
The function which you wish to call '''must''' first be exported within the resource's meta.  For example:
<syntaxhighlight lang="xml"><meta>
<info author="jbeta" type="script" description="Scoreboard resource" />
<script src="scoreboard_client.lua" type="client"/>
<script src="scoreboard_exports.lua" type="server"/>
<script src="scoreboard_http.lua" type="server"/>
<export function="getScoreboardColumns" http="true" />
<export function="getScoreboardRows" http="true" />
<export function="addScoreboardColumn" type="server"/>
<export function="removeScoreboardColumn" type="server"/>
<export function="setPlayerScoreboardForced" type="server"/>
<export function="setScoreboardForced" type="client"/>
</meta></syntaxhighlight>
This enables other resources to call a function from this resource.
 
You cannot call a server function from the client or vice versa. See [[triggerServerEvent]] and [[triggerClientEvent]] for possibilities to do that.
 
There is an easier syntax replacing this function. For example, you can instead of:<br>
  <syntaxhighlight lang="lua">call ( getResourceFromName ( "resource" ), "exportedFunction", 1, "2", "three" )</syntaxhighlight>
do much like a normal call:<br>
  <syntaxhighlight lang="lua">exports.resource:exportedFunction ( 1, "2", "three" )</syntaxhighlight>
If the resource name contains illegal characters (such as hyphens), you can also do:<br>
  <syntaxhighlight lang="lua">exports["resource-name"]:exportedFunction ( 1, "2", "three" )</syntaxhighlight>
 
Two extra "hidden" variables are passed to the exported function:
* '''sourceResource''' - The resource that called the exported function
* '''sourceResourceRoot''' - The resource root element of the resource which called the exported function.
 
==Syntax==
<syntaxhighlight lang="lua">
var... call ( resource theResource, string theFunction, [ arguments... ] )
</syntaxhighlight>
{{OOP||[[resource]]:call}}
===Required Arguments===
*'''theResource:''' This is a resource pointer which refers to the resource you are calling a function from.
*'''theFunction:''' This is a string with the name of the function which you want to call.
 
===Optional Arguments===
{{OptionalArg}}
*'''arguments:''' Any arguments you may want to pass to the function when it is called. Any number of arguments of can be specified, each being passed to the designated function.
 
===Returns===
Returns anything that the designated function has returned, if the function has no return, nil is returned. If the function does not exist, is not exported, or the call was not successful it will return false.
 


==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">
bool call ( resource, string (function), arguments ... )
exports["resource_name"]:exportedFunction([ arguments... ])
</syntaxhighlight>
<syntaxhighlight lang="lua">
exports.resource_name:exportedFunction([ arguments... ])
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
<!-- 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 -->
*'''resource_name:''' Resource name
*'''argumentName:''' description
*'''exportedFunction:''' The name of the function you want to call. Its '''not''' a string.


<!-- Only include this section below if there are optional arguments -->
===Optional Arguments===  
===Optional Arguments===  
{{OptionalArg}}  
{{OptionalArg}}  
*'''argumentName2:''' description
*'''arguments:''' Any arguments you may want to pass to the function when it is called. Any number of arguments of can be specified, each being passed to the designated function.
*'''argumentName3:''' description


===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 anything that the designated function has returned, if the function has no return, nil is returned. If the function does not exist, is not exported, or the call was not successful it will return false.
Returns ''true'' if blah, ''false'' otherwise.
 
==Example==
<section name="Server" class="server" show="true">
This extract shows adding of a "kills" column to the scoreboard resource. This then sets the ''gameShowKills'' variable to true(or false), telling the rest of the script to start outputting kills.


==Example==
'''Main Resource:'''
<!-- Explain what the example is in a single sentance -->
<syntaxhighlight lang="lua">
This example does...
function showKills ( option )
<!-- 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 -->
if not option then
-- Remove the "kills" column
exports.scoreboard:removeScoreboardColumn("kills")
else
-- Add the "kills" column
exports["scoreboard"]:addScoreboardColumn("kills")
outputDebugString ( "Showing kills now..." )
end
gameShowKills = option
end
</syntaxhighlight>
'''scoreboard Resource:'''
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--This line does...
function removeScoreboardColumn(columnName)
blabhalbalhb --abababa
    -- What ever scripted ...
--This line does this...
end
mooo
 
function addScoreboardColumn(columnName)
    -- What ever scripted ...
end
</syntaxhighlight>
</section>
 
 
<section name="Server" class="server" show="true">
This extract shows adding of a "kills" column to the scoreboard resource. This then sets the ''gameShowKills'' variable to true(or false), telling the rest of the script to start outputting kills.
 
'''Main Resource:'''
<syntaxhighlight lang="lua">
function showKills ( option )
if not option then
-- Remove the "kills" column
call(getResourceFromName("scoreboard"), "removeScoreboardColumn", "kills")
else
-- Add the "kills" column
call(getResourceFromName("scoreboard"), "addScoreboardColumn", "kills")
outputDebugString ( "Showing kills now..." )
end
gameShowKills = option
end
</syntaxhighlight>
'''scoreboard Resource:'''
<syntaxhighlight lang="lua">
function removeScoreboardColumn(columnName)
    -- What ever scripted ...
end
 
function addScoreboardColumn(columnName)
    -- What ever scripted ...
end
</syntaxhighlight>
</syntaxhighlight>
</section>


==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

Latest revision as of 17:10, 22 November 2024

[[{{{image}}}|link=|]] Important Note: Calls (exports) may incur a performance overhead - they are not equivalent in performance to calling functions in the same resource. Do not use exports in render events (like onClientRender), or in fast processing logic, unless you want to kill performance.
[[{{{image}}}|link=|]] Important Note: The sourceResource and sourceResourceRoot "hidden" variables are available even if you use exports.*:*
[[{{{image}}}|link=|]] Important Note: Using this function straight away on resource start might cause elements to not be passed properly, use setTimer in order to delay function execution and to avoid this issue.

This function is used to call a function from another resource (which must be running).

The function which you wish to call must first be exported within the resource's meta. For example:

<meta>
	<info author="jbeta" type="script" description="Scoreboard resource" />
	<script src="scoreboard_client.lua" type="client"/>
	<script src="scoreboard_exports.lua" type="server"/>
	
	<script src="scoreboard_http.lua" type="server"/>
	
	<export function="getScoreboardColumns" http="true" />
	<export function="getScoreboardRows" http="true" />
	
	<export function="addScoreboardColumn" type="server"/>
	<export function="removeScoreboardColumn" type="server"/>
	
	<export function="setPlayerScoreboardForced" type="server"/>
	<export function="setScoreboardForced" type="client"/>
</meta>

This enables other resources to call a function from this resource.

You cannot call a server function from the client or vice versa. See triggerServerEvent and triggerClientEvent for possibilities to do that.

There is an easier syntax replacing this function. For example, you can instead of:

call ( getResourceFromName ( "resource" ), "exportedFunction", 1, "2", "three" )

do much like a normal call:

exports.resource:exportedFunction ( 1, "2", "three" )

If the resource name contains illegal characters (such as hyphens), you can also do:

exports["resource-name"]:exportedFunction ( 1, "2", "three" )

Two extra "hidden" variables are passed to the exported function:

  • sourceResource - The resource that called the exported function
  • sourceResourceRoot - The resource root element of the resource which called the exported function.

Syntax

var... call ( resource theResource, string theFunction, [ arguments... ] )

OOP Syntax Help! I don't understand this!

Method: resource:call(...)


Required Arguments

  • theResource: This is a resource pointer which refers to the resource you are calling a function from.
  • theFunction: This is a string with the name of the function which you want to call.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • arguments: Any arguments you may want to pass to the function when it is called. Any number of arguments of can be specified, each being passed to the designated function.

Returns

Returns anything that the designated function has returned, if the function has no return, nil is returned. If the function does not exist, is not exported, or the call was not successful it will return false.


Syntax

exports["resource_name"]:exportedFunction([ arguments... ])
exports.resource_name:exportedFunction([ arguments... ])

Required Arguments

  • resource_name: Resource name
  • exportedFunction: The name of the function you want to call. Its not a string.

Optional Arguments

NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.

  • arguments: Any arguments you may want to pass to the function when it is called. Any number of arguments of can be specified, each being passed to the designated function.

Returns

Returns anything that the designated function has returned, if the function has no return, nil is returned. If the function does not exist, is not exported, or the call was not successful it will return false.

Example

Click to collapse [-]
Server

This extract shows adding of a "kills" column to the scoreboard resource. This then sets the gameShowKills variable to true(or false), telling the rest of the script to start outputting kills.

Main Resource:

function showKills ( option )
	if not option then
		-- Remove the "kills" column
		exports.scoreboard:removeScoreboardColumn("kills")
	else
		-- Add the "kills" column
		exports["scoreboard"]:addScoreboardColumn("kills")
		outputDebugString ( "Showing kills now..." )
	end
	gameShowKills = option 
end

scoreboard Resource:

function removeScoreboardColumn(columnName)
    -- What ever scripted ...
end

function addScoreboardColumn(columnName)
    -- What ever scripted ...
end


Click to collapse [-]
Server

This extract shows adding of a "kills" column to the scoreboard resource. This then sets the gameShowKills variable to true(or false), telling the rest of the script to start outputting kills.

Main Resource:

function showKills ( option )
	if not option then
		-- Remove the "kills" column
		call(getResourceFromName("scoreboard"), "removeScoreboardColumn", "kills")
	else
		-- Add the "kills" column
		call(getResourceFromName("scoreboard"), "addScoreboardColumn", "kills")
		outputDebugString ( "Showing kills now..." )
	end
	gameShowKills = option 
end

scoreboard Resource:

function removeScoreboardColumn(columnName)
    -- What ever scripted ...
end

function addScoreboardColumn(columnName)
    -- What ever scripted ...
end

See Also