Javascript SDK: Difference between revisions
No edit summary |
mNo edit summary |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
The JavaScript SDK for accessing the MTA Web Interface is currently part of the ajax resource. As such it can only be used in HTML pages hosted on the MTA server as part of a resource. To use it, place the following in the | The JavaScript SDK for accessing the MTA Web Interface is currently part of the ajax resource. As such it can only be used in HTML pages hosted on the MTA server as part of a resource. To use it, place the following in the ''<head>'' section of your page. | ||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
Line 7: | Line 7: | ||
Doing this automatically gives scripts in your page access to all the functions you have exported from your resource, under the same names. For example, if you have exported a "getPlayerCount" function, you can just call this from javascript: | Doing this automatically gives scripts in your page access to all the functions you have exported from your resource, under the same names. For example, if you have exported a "getPlayerCount" function, you can just call this from javascript: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="javascript"> | ||
getPlayerCount ( function ( playerCount ) { | getPlayerCount ( function ( playerCount ) { | ||
alert ( "There are " + playerCount + " players on the server!" ); | alert ( "There are " + playerCount + " players on the server!" ); | ||
Line 16: | Line 16: | ||
You can also manually call a script function using: | You can also manually call a script function using: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="javascript"> | ||
callFunction ( "resourceName", "functionName", returnFunction, errorFunction [, arguments... ] ); | callFunction ( "resourceName", "functionName", returnFunction, errorFunction [, arguments... ] ); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 24: | Line 24: | ||
Example: | Example: | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="javascript"> | ||
callFunction ( "testResource", "getPlayerCount", function ( playerCount ) { | callFunction ( "testResource", "getPlayerCount", function ( playerCount ) { | ||
alert( "There are " + playerCount + " players on the server!" ); | alert( "There are " + playerCount + " players on the server!" ); | ||
Line 32: | Line 32: | ||
}); | }); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:SDK]] | |||
[[Category:Tutorials]] |
Latest revision as of 10:21, 15 July 2024
The JavaScript SDK for accessing the MTA Web Interface is currently part of the ajax resource. As such it can only be used in HTML pages hosted on the MTA server as part of a resource. To use it, place the following in the <head> section of your page.
<* = call(getResourceFromName("ajax"),"start", getResourceName(getThisResource()) ) *>
Doing this automatically gives scripts in your page access to all the functions you have exported from your resource, under the same names. For example, if you have exported a "getPlayerCount" function, you can just call this from javascript:
getPlayerCount ( function ( playerCount ) { alert ( "There are " + playerCount + " players on the server!" ); } );
Notice that this uses an anonymous function as an argument. This function is called when the function returns data. This allows you to continue doing other things in your script while waiting for a response. The arguments for the anonymous function are the same as the returned values from the function you're calling.
You can also manually call a script function using:
callFunction ( "resourceName", "functionName", returnFunction, errorFunction [, arguments... ] );
- returnFunction: This is the function that is called when the result of the call is returned. The arguments should be the same as the returned values from the function.
- errorFunction: This function is called when an error occurs. There is a single string argument that specifies what the error is.
Example:
callFunction ( "testResource", "getPlayerCount", function ( playerCount ) { alert( "There are " + playerCount + " players on the server!" ); }, function ( error ) { alert ( "Error getting player count: " + error ); });