Call: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(add oop syntax)
mNo edit summary
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
{{Server client function}}
This function is used to call a function from another resource (which must be running).  
{{Note|Calls may incur a performance overhead - they are not equivalent in performance to calling functions in the same resource.}}
{{Important Note|The sourceResource and sourceResourceRoot "hidden" variables are available even if you use exports.*:*}}
 
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:
The function which you wish to call '''must''' first be exported within the resource's meta.  For example:
Line 31: Line 34:
   <syntaxhighlight lang="lua">exports["resource-name"]:exportedFunction ( 1, "2", "three" )</syntaxhighlight>
   <syntaxhighlight lang="lua">exports["resource-name"]:exportedFunction ( 1, "2", "three" )</syntaxhighlight>


<!-- These only work from r5939
Two extra "hidden" variables are passed to the exported function:
When using the [[call]] function, two extra "hidden" variables are passed to the exported function:
* '''sourceResource''' - The resource that called the exported function
* '''sourceResource''' - The resource that called the exported function
* '''sourceResourceRoot''' - The resource root element of the resource which called the exported function.
* '''sourceResourceRoot''' - The resource root element of the resource which called the exported function.
-->
 
==Syntax==  
==Syntax==  
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
Line 50: Line 52:


===Returns===
===Returns===
Returns anything that the designated function has returned appropriately. If the function does not exist, is not exported, or the call was not successful it will return ''nil''.
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==
<syntaxhighlight lang="lua">
exports["resource_name"]:exportedFunction([ arguments... ])
</syntaxhighlight>
<syntaxhighlight lang="lua">
exports.resource_name:exportedFunction([ arguments... ])
</syntaxhighlight>
 
===Required Arguments===
*'''resource_name:''' Resource name
*'''exportedFunction:''' The name of the function you want to call. Its '''not''' a string.
 
===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.


==Example==
==Example==
<section name="Server" class="server" show="true">  
<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, telling the rest of the script to start outputting kills.
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.
 
<syntaxhighlight lang="lua">
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
</syntaxhighlight>
</section>
 
<section name="Server" class="server" show="false">
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.


<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
function showKills ( option )
function showKills ( option )
if option == false then
if not option then
-- Remove the "kills" column
-- Remove the "kills" column
gameShowKills = false
call(getResourceFromName("scoreboard"), "removeScoreboardColumn", "kills")
call(getResourceFromName("scoreboard"), "removeScoreboardColumn", "kills")
else
else
-- Add the "kills" column
-- Add the "kills" column
gameShowKills = true
call(getResourceFromName("scoreboard"), "addScoreboardColumn", "kills")
call(getResourceFromName("scoreboard"), "addScoreboardColumn", "kills")
outputDebugString ( "Showing kills now..." )
outputDebugString ( "Showing kills now..." )
end
end
gameShowKills = option
end
end
</syntaxhighlight>
</syntaxhighlight>

Revision as of 23:25, 29 May 2019

[[{{{image}}}|link=|]] Note: Calls may incur a performance overhead - they are not equivalent in performance to calling functions in the same resource.
[[{{{image}}}|link=|]] Important Note: The sourceResource and sourceResourceRoot "hidden" variables are available even if you use exports.*:*

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.

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
Click to expand [+]
Server

See Also