Tutorial:Script interfaces: Difference between revisions
No edit summary |
m (Changing to new (0.12.20) naming pattern) |
||
Line 6: | Line 6: | ||
-- assuming name and table_of_functions are defined elsewhere | -- assuming name and table_of_functions are defined elsewhere | ||
remote. | remote.add_interface(interface_name, table_of_functions) | ||
-- It is possible to define the name and table inside the call | -- It is possible to define the name and table inside the call | ||
remote. | remote.add_interface("my_interface", { | ||
my_getter = function() | |||
-- you can return 1 or more variables from the script | -- you can return 1 or more variables from the script | ||
return "foo" | return "foo" | ||
Line 16: | Line 16: | ||
-- the values can be only primitive type or (nested) tables | -- the values can be only primitive type or (nested) tables | ||
my_setter = function(foo, bar) | |||
glob.foo = foo | glob.foo = foo | ||
glob.bar = bar | glob.bar = bar | ||
Line 34: | Line 34: | ||
Example (in the different script than the one above): | Example (in the different script than the one above): | ||
-- calls the | -- calls the my_interface.my_getter from the script above and prints the returning value | ||
print(remote.call(" | print(remote.call("my_interface", "my_getter")) | ||
This remote.call can be used in the global scope as well. However then the script in which it is called has to be invoked after the script which defined the interface. The ordering of the scripts is: first the map script and then the mod scripts in order defined by the mod ordering. | This remote.call can be used in the global scope as well. However then the script in which it is called has to be invoked after the script which defined the interface. The ordering of the scripts is: first the map script and then the mod scripts in order defined by the mod ordering. | ||
Line 41: | Line 41: | ||
-- set values in the other script | -- set values in the other script | ||
-- remote call takes the name of the interface, name of the function and then variable amount of parameters | -- remote call takes the name of the interface, name of the function and then variable amount of parameters | ||
remote.call(" | remote.call("my_interface', "my_setter", 5, {bar=baz}) | ||
=== Discovering interfaces === | === Discovering interfaces === | ||
Line 49: | Line 49: | ||
Example: | Example: | ||
-- check whether there is the " | -- check whether there is the "my_interface" interface and whether it contains the "my_getter" function | ||
if remote.interfaces. | if remote.interfaces.my_interface and remote.interfaces.my_interface.my_getter then | ||
-- the remote call for the function is safe to use | -- the remote call for the function is safe to use | ||
end | end |
Revision as of 14:47, 28 December 2015
Script interfaces allow direct communication between simultaneously running scripts. This is done in form of defining a public interface with given functions. All the code regarding the interfaces communication is in the remote
namespace.
Defining interfaces
The interface is defined as follows:
-- assuming name and table_of_functions are defined elsewhere remote.add_interface(interface_name, table_of_functions)
-- It is possible to define the name and table inside the call remote.add_interface("my_interface", { my_getter = function() -- you can return 1 or more variables from the script return "foo" end, -- the values can be only primitive type or (nested) tables my_setter = function(foo, bar) glob.foo = foo glob.bar = bar end })
The interface functions cannot take function pointers or function closures. Primitive types, LuaObjects and tables work just fine.
The interfaces are not serialised. Therefore it is a good idea to put them into the global scope of the script. In that way they are run everytime the script file is loaded (no matter whether it is the first run or game being loaded).
At the moment the interface is identified solely by its name. There is no vesion mechanism yet. It is a good idea to prefix the name of the interface with the name of the mod / map where it is defined.
Calling interface functions
The interface can be used for calling functions from a different script.
Example (in the different script than the one above):
-- calls the my_interface.my_getter from the script above and prints the returning value print(remote.call("my_interface", "my_getter"))
This remote.call can be used in the global scope as well. However then the script in which it is called has to be invoked after the script which defined the interface. The ordering of the scripts is: first the map script and then the mod scripts in order defined by the mod ordering.
-- set values in the other script -- remote call takes the name of the interface, name of the function and then variable amount of parameters remote.call("my_interface', "my_setter", 5, {bar=baz})
Discovering interfaces
The script can check for expected interfaces and its functions via the remote.interfaces
table. This is a table indexed by interface names where the values are set of functions for particular interfaces.
Example:
-- check whether there is the "my_interface" interface and whether it contains the "my_getter" function if remote.interfaces.my_interface and remote.interfaces.my_interface.my_getter then -- the remote call for the function is safe to use end