Capricorn 76
ScriptENGINE logger and handling log messages.

---------------------------
---------------------------
-- Logging and debugging example
-- Copyright (c) 2007-09 Capricorn 76 Pty. Ltd.
--
-- ScriptENGINE logger and handling log messages.
--
-- 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 subLog;

---------------------------
-- 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 a gui specific to this example
    ------------------------
    OnWorldLoadGui();

    ------------------------
    -- Subscribe to all logging events.
    -- When a 'log' event is fired via IApp:writePublish(),
    -- the subscriber will be notified, with the log message.
    -- In this example, log events are generated from the OnRotateCube().
    ------------------------
    subLog = IEvents:subscribeLog('OnLog');

    ---------------------------
    -- 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 an alternative method here.
    --
    -- 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);

    ---------------------------
    -- Set an animation for agent
    ---------------------------
    IGraphics:setEntityAnimation(idAgent_Mech, nil, true, 10);

    ---------------------------
    -- Turn on console logging with the ILog:write functions.
    -- nb: the print() function will always write to the console.
    ---------------------------
    ILog:setOutputLevel(1);
end

---------------------------
-- Load the example's specific gui
---------------------------
function OnWorldLoadGui()
    ------------------------
    -- Let's show some help text
    ------------------------
    IGraphics:loadGui('help');

    ------------------------
    -- Create a listbox via script.
    --
    -- This could have been stored in an .e76gui file
    -- and loaded with IGraphics:loadGui()
    ------------------------
    IGraphics:createListbox('listOutput', '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('listOutput', 'Published log messages', '', false);
end

---------------------------
-- runDemo.e76script calls this function before the world is unloaded
---------------------------
function OnWorldUnload(worldName)
    --print('OnWorldUnload');

    ------------------------
    -- Unsubscribe to events.
    -- We will no longer be notified of events.
    ------------------------
    IEvents:unsubscribe(subLog);
    IEvents:unsubscribe(subTick);

    ---------------------------
    -- Turn off console logging with the ILog:write functions.
    -- nb: the print() function will always write to the console.
    ---------------------------
    ILog:setOutputLevel();
end

------------------------
-- Handle Logging events
------------------------
function OnLog(logString, userData)

    ------------------------
    -- Write log message out to the listbox
    ------------------------
    idx = IGraphics:addListboxItem('listOutput', logString);
    IGraphics:makeListboxItemVisible('listOutput', idx);

    -- You can do anything with this logging info. For example, you could open a file and write the logging info there
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);

    ------------------------
    -- Log some info.
    -- The different functions log information in different ways.
    -- Only the ILog:writePublish() will publish a 'log' event.
    ------------------------
    ILog:write(tostring(currentTime) ..         ': This is some text written to the console');

    -- These logging functions also publish logging events
    ILog:writePublish(tostring(currentTime) ..      ': (Published log message) Cube rotation:' .. IWorld:getEntityRotation(idUnit_box));
    ILog:writePublish(tostring(currentTime) ..      ': (Published log message) 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)