Monday, July 14, 2008

Second Life Physics Scripting - Pinball #12 - Keyboard Control

I was finally able to trap the keyboard and I wanted to post a code snipet because it should show it in a fairly simple way.
default
{
state_entry()
{
}

// this is called by llRequestPermission
run_time_permissions(integer perm)
{
// permissions dialog answered
if (perm & PERMISSION_TAKE_CONTROLS)
{
// we got a yes
// take up and down controls
llTakeControls(CONTROL_ROT_LEFT
CONTROL_ROT_RIGHT
CONTROL_UP
CONTROL_DOWN,
TRUE, FALSE);
}
}

touch_start(integer total_number)
{
llSay(0, "Would you like to play? I'll need to take over your keyboard.");
llSay(0, "This doesn't work yet, it's still under construction.");

integer perm= llGetPermissions();

if (!perm&PERMISSION_TAKE_CONTROLS)
{
// get permission to take controls
// this was changed after the blog entry
// it needs to be called with detectedKey. During debugging
// you might still want to use llGetOwner to avoid others interupting.
llRequestPermissions(llDetectedKey(0), PERMISSION_TAKE_CONTROLS);
//llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
}
else
{
llSay(0,"We have permission to take control...trying");
// this was changed after the blog entry
// it needs to be called with detectedKey. During debugging
// you might still want to use llGetOwner to avoid others interupting.
llRequestPermissions(llDetectedKey(0), PERMISSION_TAKE_CONTROLS);
//llRequestPermissions(llGetOwner(), PERMISSION_TAKE_CONTROLS);
// the sl wiki says that you should always request permission
//llTakeControls(CONTROL_LEFT
// CONTROL_RIGHT
// CONTROL_UP CONTROL_DOWN,
// TRUE, TRUE);
}
}
control(key id, integer held, integer change)
{
llSay(0, "control id="+(string)id);
llSay(0, " held="+(string)held+" change="+(string)change);
}
}
These are the two wiki entries I used to get this done.
http://wiki.secondlife.com/wiki/LlTakeControls
http://www.lslwiki.net/lslwiki/wakka.php?wakka=llTakeControls

One thing I don't like is that it only seams like you can get information on keys that are already mapped to movement. In this case a,w,s,d,e,c,arrow keys and page up/down. This seems very limiting, but I think I can make it work. Seems like an oversite that would have been easy to allow for early on in the development process and I have trouble understanding any reason for this other than a lack of vision in regards to in world controls.

1 comment:

wooD said...

There was a bug in this which I fixed a few weeks after the post.

In
llRequestPermissions(llDetectedKey(0), PERMISSION_TAKE_CONTROLS);

I was using llOwnerKey which only allowed me to run the game. This had to be changed to llDetectedKey to allow others to get the keyboard control message.