Difference between revisions of "Script"
(→Basics) |
(→Basics) |
||
(8 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
Scripts in Shards Online are files written in [[Lua]] ending with <tt>.lua</tt> and containing the Lua code which does what your script should do. | Scripts in Shards Online are files written in [[Lua]] ending with <tt>.lua</tt> and containing the Lua code which does what your script should do. | ||
− | A super primitive script | + | A super primitive script: |
<br><tt>helloworld.lua</tt> | <br><tt>helloworld.lua</tt> | ||
<syntaxhighlight lang = lua> | <syntaxhighlight lang = lua> | ||
Line 10: | Line 10: | ||
This script being put into your mods scripts folder by itself does nothing. | This script being put into your mods scripts folder by itself does nothing. | ||
− | To have your script doing something it needs to be attached to | + | To have your script doing something it needs to be attached to some [[Game Object]] (GameObj), like an NPC, a crate, a rock, anything that can exist in the SO world. A special case is [[globals|globals.lua]], which is automagically run on server start. Because [[globals|globals.lua]] is not attached to any object, the [[this]] construct as a reference to the object a script is attached to does not work here. Using [[this]] in [[globals]] will throw errors / behave as [[nil]]. |
− | So lets create an object and attach the script to it. You can do that with the " | + | So lets create an object and attach the script to it. You can do that with the the [[behavior]] tab on the "God Info" window (right click an object or use /info). |
+ | An attached behavior is nothing but a synonym for an attached script. | ||
− | Scripts can contain other scripts using the [[require]] function. | + | RULE: Scripts can contain other scripts using the [[require]] function. |
When doing so, you will see the text being printed to the server console which teaches us one thing: | When doing so, you will see the text being printed to the server console which teaches us one thing: | ||
− | Once a script gets attached to an object it runs immediately. | + | RULE: Once a script gets attached to an object it runs immediately. |
Attach the script to a second object and see the message again - as often as you attach it to an object. | Attach the script to a second object and see the message again - as often as you attach it to an object. | ||
− | You can attach a script to many different objects, but only once to any given object. | + | RULE: You can attach a script to many different objects, but only once to any given object. |
If then, you decide to backup and restart your server (/backup and /restart) and thus the object with the attached script gets saved, you will see the text again in your console after restart, exactly one time per object which teaches us: | If then, you decide to backup and restart your server (/backup and /restart) and thus the object with the attached script gets saved, you will see the text again in your console after restart, exactly one time per object which teaches us: | ||
− | When an object gets loaded from backup all scripts on it run once. | + | RULE: When an object gets loaded from backup all scripts on it run once. |
+ | == Events == | ||
So how can scripts add any persistent functionality to an object - do we have to keep them running or what? Hell, No ! | So how can scripts add any persistent functionality to an object - do we have to keep them running or what? Hell, No ! | ||
A running script would block the server permanently, what we want is the script to wait for things to happen it should respond to. | A running script would block the server permanently, what we want is the script to wait for things to happen it should respond to. | ||
− | Scripts are mostly event controlled, the whole script architecture is based on [[Event|events]] | + | Scripts are mostly event controlled, the whole script architecture is based on [[Event|events]]. |
+ | |||
+ | Most scripts define functions and register these functions as handlers for certain events - the [[Event Handlers|Event Handlers]]. This is done with a special function called [[RegisterEventHandler]]. | ||
+ | |||
+ | Once such a function has been created and registered it is ready to answer (handle) triggered events, and this is where the fun begins. Once we can handle events we can react to stuff that happens in the world. To see a list of events check the [http://shardsonline.com/lN2rwhz4jSVambqesEBZ/lua-reference/events.html official documentation]. | ||
+ | [[Category:Modding]][[Category:Drafts]] |
Latest revision as of 19:53, 5 November 2016
Basics
Scripts in Shards Online are files written in Lua ending with .lua and containing the Lua code which does what your script should do.
A super primitive script:
helloworld.lua
print ("Always look at the bright side of life, and yes, 'Hello World!'")
This script being put into your mods scripts folder by itself does nothing.
To have your script doing something it needs to be attached to some Game Object (GameObj), like an NPC, a crate, a rock, anything that can exist in the SO world. A special case is globals.lua, which is automagically run on server start. Because globals.lua is not attached to any object, the this construct as a reference to the object a script is attached to does not work here. Using this in globals will throw errors / behave as nil.
So lets create an object and attach the script to it. You can do that with the the behavior tab on the "God Info" window (right click an object or use /info). An attached behavior is nothing but a synonym for an attached script.
RULE: Scripts can contain other scripts using the require function.
When doing so, you will see the text being printed to the server console which teaches us one thing:
RULE: Once a script gets attached to an object it runs immediately.
Attach the script to a second object and see the message again - as often as you attach it to an object.
RULE: You can attach a script to many different objects, but only once to any given object.
If then, you decide to backup and restart your server (/backup and /restart) and thus the object with the attached script gets saved, you will see the text again in your console after restart, exactly one time per object which teaches us:
RULE: When an object gets loaded from backup all scripts on it run once.
Events
So how can scripts add any persistent functionality to an object - do we have to keep them running or what? Hell, No ! A running script would block the server permanently, what we want is the script to wait for things to happen it should respond to. Scripts are mostly event controlled, the whole script architecture is based on events.
Most scripts define functions and register these functions as handlers for certain events - the Event Handlers. This is done with a special function called RegisterEventHandler.
Once such a function has been created and registered it is ready to answer (handle) triggered events, and this is where the fun begins. Once we can handle events we can react to stuff that happens in the world. To see a list of events check the official documentation.