In part 1 we examined ScriptENGINE initialisation, loading 3D worlds, running the processing loop, and shutting down the ScriptENGINE. In this article, we'll take a closer look at what you can do after calling the IWorld:load() function.
View the Demo Code
We are going to examine the runDemo.e76script from the ScriptENGINE SDK/Scripts folder. This script holds the basic framework used to run each demo. The ScriptENGINE Demos all follow the same basic structure. So for this article, we're going to look at the cryptographic demo.
Navigate to the ScriptENGINE SDK/[Cryptography] Make your data unreadable to prying eyes folder.
Open the world.e76script in the SciTE code editor. We will be referring to the world.e76script for the rest of this article.
The ScriptENGINE SDK
Now's a great time to get the ScriptENGINE SDK (including the SciTE code editor, Programmer's Reference and ScriptENGINE Demos).
The ScriptENGINE Demo Framework
The ScriptENGINE Demos are run using the runDemo.e76script framework code in the ScriptENGINE SDK/Demos folder.
Loading a 3D World and Script
The runDemo.e76script calls IWorld:load() to load the 3D world and the world.e76script. The ScriptENGINE executes code defined outside any functions when the world.e76script is loaded into the engine.
The world.e76script calls IApp:loadScript('worldIds') in it's base code. The worldIds.e76script maps the 3D world entity names to their unique identifiers. The worldIds.e76script is auto-generated by the ScriptENGINE SDK World Editor when the world is saved.
Demo Initialisation
The runDemo.e76script calls the OnWorldLoad() function after loading the world.e76script into the engine. This gives us a chance to perform any specific initialisation for this example.
The world.e76script first loads a GUI definition file using IGraphics:loadGui(). GUIs can be created directly from script code, or visually by using the ScriptENGINE SDK GUI Editor.
The world.e76script then registers a GUI subscriber using the IEvents:subscribeGui() function. The registered callback is fired whenever GUI events occur (eg. button clicks). The subscribeGui() returns a unique subscriberID that is used to later unregister the subscriber via the IEvents:unsubscribe() function.
At this point, the world.e76script could perform more initialisation, like registering other subscribers, creating more GUI controls, adding other 3D entities and so on.
Running the Demo
After loading the 3D world, the runDemo.e76script calls the IApp:run() function to run the ScriptENGINE's automatic processing loop. The processing loop runs in blocking mode, until the IApp:stop() is called (usually from an event subscriber callback).
(Alternatively, you can create a manual processing loop by calling IApp:tick() in a loop).
Handling GUI Events
The GUI subscriber's callback function is called whenever a GUI event occurs. GUI events include things like button clicks, mouse clicks, focus changes, listbox changes, combobox changes etc.
The GUI subscriber callback function has the following signature:
function Callback(callerId, eventType, userData, handled)
- callerId is the a unique GUI identifier that triggered the event. This ID is defined when you create the GUI control in the ScriptENGINE SDK GUI editor, or manually from script code using the IGraphics:create...() functions.
- eventType defines the triggered event type. The list of event types can be found under the ScriptENGINE Constants section of the Programmer's Reference.
- userData is the user-defined data (if any) specified you call the IEvents:subscribe() function or IEvents:setUserData() function.
- handled is a boolean indicating whether another subscriber has already handled this event.
Subscriber callback signatures can be inserted directly into your code from the SciTE code editor. Use the [Edit->Insert Abbreviation] SciTE menu for a list of ScriptENGINE function signatures.
Subscriber callback signatures can also be found in the Programmer's Reference.
Demo Cleanup
The runDemo.e76script calls the OnWorldUnload() function before unloading the 3D world from the engine. This gives us a chance to perform any specific cleanup for this example.
In particular, we use it to unregister the GUI subscriber via the IEvents:unsubscribe() function. This stops our callback functions being called.
| < Prev | Next > |
|---|