Capricorn 76
This Lua tutorial demonstrates the ScriptENGINE subscriber/publisher mechanism used to publish event throughout the system. In this tutorial, we look at User Events, then publish the event in a loop to see the subscriber response.


---------------------------
---------------------------
-- Tutorial: Events & Subscribers
-- Subscribe to a User Event, Then publish the event in a loop to see the subscriber response.
-- Note: The IEvents interface includes functions to subscribe to many different ScriptENGINE events.
--
-- Copyright (c) 2006-2008 Capricorn 76 Pty. Ltd.
---------------------------
---------------------------


-- Display the ScriptENGINE console window
IApp:showConsoleWindow();

---------------------------
-- The ScriptENGINE starts its program by looking for a function called OnStateRun_[startupScriptName]().
-- In this case, it looks for OnStateRun_test().
-- If the function is not found, then the ScriptENGINE looks for the fallback function OnStateRun().
-- If it finds a relevant OnStateRun function, it calls it.
-- The State entry function can do whatever it likes, including queuing other state using IState:queue().
---------------------------
function OnStateRun()

    ---------------------------
    -- Create a subscriber.
    -- This tells the ScriptENGINE to notify us whenever the relevant event occurs by firing our callback function.
    -- If the subscription is successful, then the ScriptENGINE returns the new subscriber id.
    -- Use the returned id to unsubscribe when you've finished handling the event.
    --
    -- Note that multiple subscribers can subscribe to the same event.
    ---------------------------
    local subUser = IEvents:subscribeUserEvent('myEvent', 'OnMyEvent');

    ---------------------------
    -- Run a loop, to publish the user event a number of times.
    --  You can create as many user events as you like in the ScriptENGINE by giving each a unique name.
    ---------------------------
    local i;
    for i = 1, 10 do
        ---------------------------
        -- Publish the event.
        -- All relevant subscribers will be notified and passed the input parameters
        -- Use Lua tables to specify multiple parameters.
        ---------------------------
        IEvents:publishUserEvent('myEvent', {i, 'param' .. i, 'param' .. (i*2)});
    end

    ---------------------------
    -- Stop subscribing to the event.
    -- The callback will no longer be fired when the event is published
    ---------------------------
    IEvents:unsubscribe(subUser);

    print('Test complete. Press any key to exit...');
    IKey:getch();
end

---------------------------
-- The Subscriber callback function.
-- The ScriptENGINE calls this function whenever the relevant event is published.
-- NB: From the SciTE code editor (supplied with the ScriptENGINE SDK),
--        select the "Edit->Insert Abbreviation" menu to insert the framework code for a callback function.
---------------------------
function OnMyEvent(publishedParam, userData, handled)
    -- We know the user event is published with a Lua table as a parameter, containing 3 indices.
    -- Wo we print them here...
    print('OnMyEvent', publishedParam[1], publishedParam[2], publishedParam[3]);
end

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