Capricorn 76
A Lua tutorial illustrating the console window & how to use the ILog interface.


---------------------------
---------------------------
-- Tutorial: Console Window
-- Open the console window, display some output, wait for user, then close.
--
-- Copyright (c) 2006-2009 Capricorn 76 Pty. Ltd.
---------------------------
---------------------------


---------------------------
-- Main program entry state.
-- The ScriptENGINE automatically looks for a startup state called OnStateRun_[scriptName] to start the state machine.
-- In this case, it looks for OnStateRun_test(param1, param2, param3).
-- If it cannot find the function, it looks for the fallback function OnStateRun(scriptName, param1, param2, param3).
---------------------------
function OnStateRun(scriptName, param1, param2, param3)
    -- Open the console window
    IApp:showConsoleWindow();

    -- Turn on output logging. This is an easy way to enable/disable console output via the ILog interface
    ILog:setOutputLevel(1);

    -- Example of how to create a subscriber to handle published logging messages
    local subLog = IEvents:subscribeLog('OnLog');

    -- Print something to the console
    print('This is a message to the console');

    -- Log something
    ILog:write('This is a log message');

    -- Log something, and publish a log event (that IEvents:subscribeLog() subscribers can handle)
    ILog:writePublish('This is a published log message');

    -- Wait for user input
    print('Press any key to exit...');
    IKey:getch();   -- Wait for user key press

    -- Unsubscribe to the events
    IEvents:unsubscribe(subLog);
end

---------------------------
-- Log Subscriber callback function.
-- This is called by the ScriptENGINE whenever Log events are published
---------------------------
function OnLog(logString, userData, handled)
    print('OnLog : Event published:' .. logString);
end

Copyright © 2006-23 Sep 2009 Capricorn 76 Pty. Ltd. (created on Wed Sep 23 16:49:11 2009)