Javascript SDK: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
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 ''<head>'' section of your page.
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" lang="lua">
<syntaxhighlight lang="lua">
<* = call(getResourceFromName("ajax"),"start", getResourceName(getThisResource()) ) *>
<* = call(getResourceFromName("ajax"),"start", getResourceName(getThisResource()) ) *>
</syntaxhighlight>
</syntaxhighlight>
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="lua" lang="javascript">
<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="lua" lang="javascript">
<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="lua" lang="javascript">
<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!" );

Latest revision as of 20:25, 23 September 2016

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 );
});