Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

%&$ING sword fight script, HELP!

Jediborg Wishbringer
Second Life Resident
Join date: 15 Nov 2004
Posts: 20
12-01-2004 20:03
I have been working on this swordfight script ALL DAY. it seems the more i work on it, the more convoluted my program gets. This current model makes you swing your sword when you click the left mouse button. only problem is, you can only swing it once before the keys are automatically released! all other versions have me swinging the sword non-stop untill i disarm it! HELP! i'm basically trying to replicate the pirate fighting sword V3, but without the damage script.
Here's my program so far, any suggestions?


key id="Jediborg Wishbringer";
string name="";
integer normal;
float sec;

default
{
state_entry()
{
llSay(0, "type /sword to arm/disarm";);
llListen(0,name,id,"/sword";);
}

touch_start(integer total_number)
{
llSay(0, "I'm the Legendary Mastersword! only the \n true hero of time can weild me!!";);

}
listen(integer channel, string name, key id, string message)
{
llSay(0, "The Blade of Evil's Bane is Ready";);
state armed;
}


}
state armed
{
state_entry()
{
key avitar = llGetOwner();
llRequestPermissions(avitar,PERMISSION_TRIGGER_ANIMATION);
if(llGetPermissions()==PERMISSION_TRIGGER_ANIMATION)
{
//llStartAnimation("looped fencing";);
llStartAnimation("bow";);
}

llRequestPermissions(avitar,PERMISSION_TAKE_CONTROLS);
if(llGetPermissions()==PERMISSION_TAKE_CONTROLS);
{
llTakeControls(CONTROL_LBUTTON, TRUE, FALSE);
}

llListen(0,name,id,"/sword";);
llTakeControls(CONTROL_LBUTTON, TRUE, FALSE);

}
control(key name, integer levels, integer edges)
{
key avitar = llGetOwner();
llRequestPermissions(avitar,PERMISSION_TRIGGER_ANIMATION);
if(llGetPermissions()==PERMISSION_TRIGGER_ANIMATION)
{
if(levels == CONTROL_LBUTTON)
{
llStartAnimation("sword_strike_R";);
sec =3;
llSetTimerEvent(sec);
//llStopAnimation("sword_strike_R";);
}
}
}
timer()
{
key avitar = llGetOwner();
llRequestPermissions(avitar,PERMISSION_TRIGGER_ANIMATION);
if(llGetPermissions()==PERMISSION_TRIGGER_ANIMATION)
{
llStopAnimation("sword_strike_R";);
sec=0.0;
llSetTimerEvent(sec);
state armed;
}
}
listen(integer channel, string name, key id, string message)
{
if(message=="/sword";)
{
llSay(0,"your Sword is disarmed";);
//llStopAnimation("looped fencing";);
//llStopAnimation("sword_strike_R";);
state default;
}
}
state_exit()
{

}

}
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
12-01-2004 20:45
CODE

control(key name, integer levels, integer edges)
{
if( (levels & CONTROL_LBUTTON) && (edges & CONTROL_LBUTTON)) //Button just now pushed
{
llStartAnimation("sword_strike_R");
}

if( !(levels & CONTROL_LBUTTON) && (edges & CONTROL_LBUTTON)) //Button just now released
{
llStopAnimation("sword_strike_R");
}
}


Anding (&;) the integers levels and edges with the constant for the key you're looking for is how you detect what key(s) are involved in starting that callto the control() event

levels: it sets bits true if the corresponding key is pressed.
edges: sets bits true if that key has changed from one state to the other when the event is called.

So, to detect when a key has just been pressed, check the edges and levels are both (&&;) true. And to see if it's been let up, the edges must be true, but the levels must not (!) be true. For most applications this gives you a flexible base to start with.

You can test for just levels if you want the script to repeat something just so long as the button is pushed.

Of course, the good people building the Wiki explain this better than I can: The control() event.
Jediborg Wishbringer
Second Life Resident
Join date: 15 Nov 2004
Posts: 20
12-02-2004 14:44
thanks, that makes a lot of sense,
only problem is, after i press the left mouse button, the sword strikes, then releases the contolls, so when i click the mouse again, it doesn't strike again. is there some kind of loop, i need to do to get it to keep the controlls?
Jillian Callahan
Rotary-winged Neko Girl
Join date: 24 Jun 2004
Posts: 3,766
12-03-2004 05:39
A script holds permissions until the agent tells it to take a hike. So you really only need ask for perms once...

For an attachment, the best place to do that is in the attach() event:
CODE

attach(key on)
{
if (on != NULL_KEY) // Nice and general - if we get a key, it's an av wearing it.
{
integer perm = llGetPermissions(); // Now we check to see if we've been granted perms already...

if (perm != (PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION))
{
llRequestPermissions(on, PERMISSION_TAKE_CONTROLS | PERMISSION_TRIGGER_ANIMATION); // and if not, ask for them
}
else
{
llTakeControls(CONTROL_LBUTTON, TRUE, TRUE); // if so, grab the controls
}

}
else
{
llSay(0, "releasing controls"); // otherwise, no key means we've been detached.
llTakeControls(FALSE, TRUE, TRUE);
}
}



So, when the agent grants perms, it triggers the run_time_permissions() event:
CODE

run_time_permissions(integer perm)
{
if (perm) // This assumes MUCH, but usually works.
{
llTakeControls(CONTROL_LBUTTON, TRUE, TRUE); // since we've been granted perms, grab controls
}
}



Have it check perms again when re-activated in the listen event. Don't bother anywhere else.