Tuesday, March 11, 2008

Listen event fails after object changes ownership

A problem I often see on the the Scripting chat is about users that are having troubles with listen() events after an object's owner changes. (i.e. someone buys the object).

This happens because users often call llListen() in the state_entry() handler and specify llGetOwner() as a filter. Using llGetOwner() as a filter is a good idea because it is the lowest lag option, but then special attention needs to be given on handling ownership changes.

Fortunately, the changed() handler gives us an easy fix - simply add to your code a "changed handler" that resets the script [llResetScript()] on change of ownership.

The code looks something like this:

changed(integer change)
{
if(change & CHANGED_OWNER)
{
llResetScript();
}
}



Note that the change integer in the arguments is bitwise compared to the constant CHANGED_OWNER. if(change==CHANGED_OWNER) would work most of the time, but it is possible to get a single call to changed() with multiple options bitwise ANDed together.

I'll write future posts on how to avoid resetting the script and more about bitwise operations.

No comments:


Official Second Life Blog