ScriptENGINE has exposed this event mechanism in its IEvent interface,
allowing you to create your own event types.
An event can be published at any time, by anybody.
But only active subscribers will be notified of the event.
An event will be published to multiple subscribers automatically,
if more than one subscriber is active.
The publisher doesn't need to know about the subscribers,
how many there are, or who they are.
---------------------------
---------------------------
-- User events example
-- Copyright (c) 2007-09 Capricorn 76 Pty. Ltd.
--
-- ScriptENGINE has an internal event management system utilised by all the
-- internal engines (eg; graphics, physics etc.) to notify the user
-- when events occur.
--
-- ScriptENGINE has exposed this event mechanism in its IEvent interface,
-- allowing you to create your own event types.
--
-- An event can be published at any time, by anybody.
-- But only active subscribers will be notified of the event.
-- An event will be published to multiple subscribers automatically,
-- if more than one subscriber is active.
-- The publisher doesn't need to know about the subscribers,
-- how many there are, or who they are.
--
-- NOTE:
-- The ./Demos/runDemo.e76script is a utility script used to run all the examples.
-- It loads the world, calls the OnWorldLoad(), OnWorldUnload() functions, and runs the graphics engine loop, waiting for the user to press escape.
---------------------------
---------------------------
local subTick;
local subUser;
---------------------------
-- runDemo.e76script calls this function when the world is loaded
---------------------------
function OnWorldLoad(worldName)
--print('OnWorldLoad');
---------------------------
-- The SDK world editor creates a worldIds.e76script when the world is saved. It contains all the entity ids.
-- We load the IDs so that we can refer to the world entities using friendly names.
---------------------------
IApp:loadScript(IWorld:getNameLong() .. '/worldIds');
---------------------------
-- Load example's GUI
---------------------------
OnWorldLoadGui();
---------------------------
-- Subscribe to user events.
-- We will publish events from the OnRotateCube.
-- 'myEvent' can be any name we like with one proviso: subscribers must use the same name as the publisher.
-- ScriptENGINE can support an unlimited number of user event types.
---------------------------
subUser = IEvents:subscribeUserEvent('myEvent', 'OnUserEvent');
---------------------------
-- Subscribe to the graphics engine 'tick' event.
-- Our callback will be periodically fired by the graphics engine,
-- so we can do some extra processing.
--
-- Could create a thread to manage this,
-- but we're showing how this can be done from the graphics engine thread.
--
-- Note that you should do minimal processing this way,
-- because the graphics can become jerky as the processing load increases.
--
-- The returned identifier is later used to unsubscribe.
---------------------------
subTick = IEvents:subscribeTickPeriodic('OnRotateCube', 1000);
end
---------------------------
-- Load example's GUI
---------------------------
function OnWorldLoadGui()
---------------------------
-- Load this exampl's GUI
---------------------------
IGraphics:loadGui('help');
---------------------------
-- Create a listbox via script.
-- This could have bee stored in an .e76gui file and loaded with IGraphics:loadGui()
---------------------------
IGraphics:createListbox('listEvents', '0.2 0.8 0.8 0.9', true, 'Tiny.png');
---------------------------
-- Create a tooltip for the listbox. The Gui example show's this in more detail.
-- The tooltip size is automatically managed for single line tooltips. If wordwrap was used, then the size would have to be specified
---------------------------
IGraphics:createTooltip('listEvents', 'Published user messages');
end
---------------------------
-- runDemo.e76script calls this function when the world is unloaded
---------------------------
function OnWorldUnload(worldName)
--print('OnWorldUnload');
---------------------------
-- Unsubscribe to events.
-- We will no longer be notified of the events anymore.
---------------------------
IEvents:unsubscribe(subUser);
IEvents:unsubscribe(subTick);
end
------------------------
-- Handle Logging events
------------------------
function OnUserEvent(publishedParam, userData)
---------------------------
-- In this example, we're using our user event like a logging event.
-- We know that the publishedParam is a string. It's up to us to define it. But it could be any Lua type (eg. strings, numbers or even tables)
---------------------------
---------------------------
-- Write text out to the listbox
---------------------------
idx = IGraphics:addListboxItem('listEvents', publishedParam);
IGraphics:makeListboxItemVisible('listEvents', idx);
end
------------------------
-- Handle graphics tick event
------------------------
function OnRotateCube(currentTime, lastTime, isWindowActive, userData)
---------------------------
-- Rotate the cube
---------------------------
IWorld:manipulateEntity(idUnit_box, 'rotate', 0, 1, 1);
------------------------
-- Rotate Agent
------------------------
IWorld:manipulateEntity(idAgent_Mech, 'rotate', 0, 5, 0);
---------------------------
-- Publish user events.
-- In this example, we're using our user event like a logging event.
-- The published parameter can by any Lua type (eg. strings, numbers or even tables)
---------------------------
IEvents:publishUserEvent('myEvent', tostring(currentTime) .. '. USER EVENT:Cube rotation: ' .. IWorld:getEntityRotation(idUnit_box));
IEvents:publishUserEvent('myEvent', tostring(currentTime) .. '. USER EVENT:Agent rotation: ' .. IWorld:getEntityRotation(idAgent_Mech));
end
Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:12 2009)