Events and States

Events (future)

A new more powerful way to handle events is expected before the beta version.

There will be a LL library with functions to add and remove event handlers with callback functions.

There is a proposal about the format in these links:

Events (currently)

Events are writen as global functions (without the local keyword).

They have the same names and parameters.

// events (LSL)

touch_start(integer num_detected)
{
    // do something
}

listen(integer channel, string name, key id, string message)
{
    // do something
}
-- events (SLua)

function touch_start(num_detected)
    -- do something
end


function listen(channel, name, id, message)
    -- do something
end
--

The “New Script”

// the "New Script" (LSL)

default
{
    state_entry()
    {
        llSay(0, "Hello, Avatar!");
    }

    touch_start(integer total_number)
    {
        llSay(0, "Touched.");
    }
}

//
-- the "New Script" (SLua - LSL style)



function state_entry()
    ll.Say(0, "Hello, Avatar!")
end


function touch_start(total_number)
   ll.Say(0, "Touched.")
end



state_entry()
-- the "New Script" (SLua - Lua style)








function touch_start(total_number)
   ll.Say(0, "Touched.")
end



ll.Say(0, "Hello, Avatar!")

SLua doesn’t have states and there are no events state_entry() or state_exit().

To keep the LSL structure, we can write a function with name state_entry() (it’s not an event, just a function) and call it from the top-level code (LSL-style script, last line).

Or move the code in the LSL state_entry() to the top-level code (Lua-style script, last line).

States

There aren’t states in Slua.

The events state_entry() and state_exit() doesn’t exist.

The script executes the code that is at the top level, out of the functions.

We can simulate states with functions with different names, one for each state, for each event. To change to another “state” we assign the function for this “state” to the name of the event.

// states (LSL)

default
{
    touch_end(integer num_detected)
    {
        llOwnerSay("Changing to state ready");
        state ready;
    }
}

state ready
{
    touch_end(integer num_detected)
    {
        llOwnerSay("Changing to state default");
        state default;
    }
}


//
-- states (SLua)


-- function for the event in the "state" default
function touch_end_default(num_detected)
    ll.OwnerSay("Changing to state ready")
    touch_end = touch_end_ready  -- we change the function that handles the event
end




-- function for the event in the "state" ready
function touch_end_ready(num_detected)
    ll.OwnerSay("Changing to state default")
    touch_end = touch_end_default  -- we change the function that handles the event
end


-- in Lua we can assign a function to a variable
-- touch_end becomes a function, the same than touch_end_default
touch_end = touch_end_default