(→An Additional Example: Subscribing To/Manually Firing Events) |
|||
Line 44: | Line 44: | ||
[[File:Additionalexampleluascript.png]] | [[File:Additionalexampleluascript.png]] | ||
− | + | - The first six lines output various arbitrary properties, specifically the time, some statistics on the resources associated with the script, and objects associated with the scene. | |
− | + | ||
− | The first six lines output various arbitrary properties, specifically the time, some statistics on the resources associated with the script, and objects associated with the scene. | + | |
These lines don't perform any logically associated operations beyond illustrating how to retrieve (and log) these values in the context of the scripting runtime. | These lines don't perform any logically associated operations beyond illustrating how to retrieve (and log) these values in the context of the scripting runtime. | ||
− | The next line uses the SScript class' 'find' method directly to rename the directional light in the scene. | + | - The next line uses the SScript class' 'find' method directly to rename the directional light in the scene. |
+ | |||
+ | - The next three lines instantiate a local reference to a game object named "GameObject", then invoke methods on that instance to subscribe to it's events. An appropriate message is written to the console in the interest of instrumentation. | ||
− | The next | + | - The next four lines are quite interesting, and provide an excellent example of taking a reference to an anonymous function. The anonymous function is declared typographically starting with the text 'function()', and ending with the text 'end'. The function logic is in the body, and in this case produces text output indicating that the event fired and was serviced, and the time and mouse position are logged as well. |
− | The next | + | - The next three lines register the method, wiring it up to the 'OnMouseDown' event (mouseclick), write to the log confirming the event method has been registered, and then manually invoke the event. |
− | The | + | - The final line adjusts the range of the light in the scene. Not real sure where Adam was headed with that, but this an example and that's what it does ;) |
{{Scripting Navbox}} | {{Scripting Navbox}} |
Space supports two separate scripting runtimes - Lua and C#; for most uses, we strongly recommending using the Lua variant, as it is cross-platform compatible. (C#/.NET only work for Standalone clients). Our Lua runtime runs optimally in all platforms including WebGL; and utilises the same API as the C# variant.
Note: We had originally announced JavaScript as our cross-platform runtime, however after implementation we've found our Lua runtime performs considerably better and with greater stability.
Note: This documentation refers to the first fruits of Lua scripting in Space, and as such, it and everything it describes is subject to change at just about any time
Here's the view in the inspector pane of the GameObject to which the example script is attached, along with a particle system:
That's the entire script in the source code box, it's really quite simple.
- The first line writes a text message on the console.
- The second line creates a script-local reference to the host object (the GameObject to which the script is attached).
- The next five lines set up a local variable referencing the host object's position, set the positional properties via the local variable, and finally store that position back to the host object, effectively setting it's position in world.
- after that, the attached particle system is invoked with 50 particles.
Note that in each of these operations, the reference to the host object has been used as a property selector. The last line uses a reference to the scene as a property selector, but does so directly, without creating any persistent references in advance of of doing so.
- That last line sets the text value to be rendered on the canvas in the hierarchy, producing the 'Hello World" message as 3D text rendered in the scene.
Hopefully this will be a sufficient 'jumping off point' for those of us eager to get busy with scripting interactive objects for Space.
Following is an additional example, scraped from the Space Beta Tester's skype channel.
- The first six lines output various arbitrary properties, specifically the time, some statistics on the resources associated with the script, and objects associated with the scene. These lines don't perform any logically associated operations beyond illustrating how to retrieve (and log) these values in the context of the scripting runtime.
- The next line uses the SScript class' 'find' method directly to rename the directional light in the scene.
- The next three lines instantiate a local reference to a game object named "GameObject", then invoke methods on that instance to subscribe to it's events. An appropriate message is written to the console in the interest of instrumentation.
- The next four lines are quite interesting, and provide an excellent example of taking a reference to an anonymous function. The anonymous function is declared typographically starting with the text 'function()', and ending with the text 'end'. The function logic is in the body, and in this case produces text output indicating that the event fired and was serviced, and the time and mouse position are logged as well.
- The next three lines register the method, wiring it up to the 'OnMouseDown' event (mouseclick), write to the log confirming the event method has been registered, and then manually invoke the event.
- The final line adjusts the range of the light in the scene. Not real sure where Adam was headed with that, but this an example and that's what it does ;)
|