Difference between revisions of "Script"
(→Basics) |
(→Basics) |
||
Line 12: | Line 12: | ||
To have your script doing something it needs to be attached to a some Game Object (GameObj), like an NPC, a crate, a rock, anything that can exist in the SO world. | To have your script doing something it needs to be attached to a some Game Object (GameObj), like an NPC, a crate, a rock, anything that can exist in the SO world. | ||
− | 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. |
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 script define functions and reister these functions as handlers for certain events - the [[Event Handlers|Event Handlers]]. |
Revision as of 19:28, 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 a some Game Object (GameObj), like an NPC, a crate, a rock, anything that can exist in the SO world.
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.
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 script define functions and reister these functions as handlers for certain events - the Event Handlers.