Functions: Difference between revisions
From Iron Realms Nexus Client Documentation
No edit summary |
No edit summary |
||
Line 13: | Line 13: | ||
* '''dec_variable(name, by)''' - Decrement a variable from the client's simplified scripting system. | * '''dec_variable(name, by)''' - Decrement a variable from the client's simplified scripting system. | ||
* '''mul_variable(name, by)''' - Multiply a variable from the client's simplified scripting system. | * '''mul_variable(name, by)''' - Multiply a variable from the client's simplified scripting system. | ||
* '''div_variable(name, by)''' - Divide a variable from the client's simplified scripting | * '''div_variable(name, by)''' - Divide a variable from the client's simplified scripting system. | ||
=== Reflexes === | |||
* '''reflex_find_by_name(type, name, case_sensitive, enabled_only, package))''' - Search for a specific reflex. | |||
* '''reflex_enable(id)''' - Enable a reflex (id returned by reflex_find_by_name above). | |||
* '''reflex_disable(id)''' - Enable a reflex (id returned by reflex_find_by_name above). | |||
=== Misc. === | === Misc. === | ||
* '''to_number(val)''' - Convert a string number to a value. | * '''to_number(val)''' - Convert a string number to a value. | ||
Revision as of 22:56, 15 February 2016
Functions and scripts are the most powerful option that the client has to offer, but also one that is most difficult to use. Functions and scripts are programmed in the Javascript language.
While we won't be covering how to code in JavaScript in this section, we'll cover some of the built-in functions and how to build your own custom code to further expand your reflexes.
Built-in modules
Commands
- send_command(input, no_expansion) - Send a command to the game. Set no_expansion to 1 to send the exact string to the game without expansion.
Variables
- get_variable(name) - Retrieve the value of a variable from the client's simplified scripting system.
- set_variable(name, val) - Set a variable for the client's simplified scripting system.
- delete_variable(name) - Delete a variable from the client's simplified scripting system.
- inc_variable(name, by) - Increment a variable from the client's simplified scripting system.
- dec_variable(name, by) - Decrement a variable from the client's simplified scripting system.
- mul_variable(name, by) - Multiply a variable from the client's simplified scripting system.
- div_variable(name, by) - Divide a variable from the client's simplified scripting system.
Reflexes
- reflex_find_by_name(type, name, case_sensitive, enabled_only, package)) - Search for a specific reflex.
- reflex_enable(id) - Enable a reflex (id returned by reflex_find_by_name above).
- reflex_disable(id) - Enable a reflex (id returned by reflex_find_by_name above).
Misc.
- to_number(val) - Convert a string number to a value.
Functions are created as any other reflex type. Each function must have a name, otherwise it will not work. Once created, you can invoke functions simply by entering its name as a command - for this reason, you should be careful not to give your functions names that would conflict with the actual game commands.
Scripts are similar to functions, but are attached to a particular trigger, and are only ever invoked when the trigger matches.
Scripts and functions consist of commands, separated by a semicolon (;). One important command is client.send_direct, which lets you send commands from the script. For example: client.send_direct("look");
Scripts can use their own private variables, defined using var name = value;. For example, var number = 5;
It is important to keep in mind that these are NOT the client variables, but separate ones entirely. It is also possible to perform various calculations on the variables. For example: number = 4 * 5;
would assign the result of 4 * 5 to the local variable number. number = number * 5
would multiply the value stored in variable number by 5, then assign the result back to variable number, overwriting the previous value.
Scripts can access and set the client variables as well - use client.get_variable to retrieve the value of a variable, and client.set_variable to set it. For example, this script increases the client variable called amount by one:
var a = client.get_variable('amount') + 1; client.set_variable('amount', a);
Functions and scripts receive some additional variables depending on how they are called.
Functions called by a command receive an args variable, which contains the individual words used after the function name, accessed using args[0], args[1],. ....
Functions called in a trigger receive information about the match - args.text, args.match, args.prefix, args.suffix, args.backrefs[1], ...
Scripts receive the backrefs (args[0]., args[1],. args[2], ...) and the current_package variable indicating the active package (see below).
Another example of what scripts can do are conditional triggers, using the if statement. For example: if (client.get_variable('count') >= 5) client.send_direct('say count is ' + client.get_variable('count'));
In this example, the + operator is used to join two strings.
You can call functions in scripts using run_function(name, args, package).
Manipulating reflexes in functions or scripts
The client allows you to fetch information about reflexes, and to modify it in various ways.
First, you need to fetch the desired refles, using client.reflex_find_by_name(type, name, case_sensitive, enabled_only, package). The last three parameters are optional. For example: var el = client.reflex_find_by_name('alias', 'drop'); The name is entered into the Alias name / Trigger name / ... field that you haven't been using so far.
The most useful option that this function provides is the ability to enable and disable various types of reflexes (including groups) in a script. To do this, you can use the client.reflex_enable and client.reflex_disable functions. For example: var el = client.reflex_find_by_name('alias', 'drop'); client.reflex_disable(el);
. You can use el.enabled in conditions to check if a particular reflex is enabled.
This functionality allows you to easily implement functionality such as one-time triggers. Simply use reflex_enable when you need to enable the trigger, and reflex_disable in the trigger itself, using the trigger's name so that the trigger disables itself.
Output text manipulation in trigger scripts and functions
In trigger scripts and functions called by triggers, you have access to the client.current_line, which can be used to modify the line that will be shown on the screen, similarly to how the built-in functionality operates. This functionality is suitable for more complicated cases, such as only highlighting if a certain condition is met, or displaying a result of some calculation.
To hide a line of text, simply set its gag property to true - client.current_line.gag = true;.
To colorize or change a portion of the text, you need to access the parsed line, using client.current_line.parsed_line or client.current_line.parsed_prompt (if the line is actually a prompt; note that prompts are not displayed by default). You can then use the colorize(start, end, color, background) and replace(start, end, newtext, color, background) functions to modify the line. "end" is the first index that is not colorized or replaced. For example, client.current_line.parsed_line.colorize(10, 12, 'red', null);
would colorize the 10th and 11st character red.
client.current_line.parsed_line.text() allows you to retrieve the line text without any formatting - useful to determine the appropriate positions for the replacements.
Default functions
There are three functions which are called automatically by the client. These are onLoad, called when the settings areloaded, onGMCP, called upon receiving a GMCP message, and onBlock, called upon receiving each text block, allowing you to peform manipulations on it, or kickstart functionality that you want to execute on every prompt. The function does not receive any data.
The onGMCP function receives two arguments - args.gmcp_method is the GMCP message name, args.gmcp_args are the parameters (if any).
The onBlock function can make use of the current_block variable, which holds the individual lines (current_block[0] is the first one, current_block[1] is the second one, and so on). The manipulation methods described with triggers are all applicable on these as well.
Examples
GMCPData: This example reads some data coming over via GMCP and assigns it to variables.