Jump to content

Need help with a script please....


NawaliaTrea
 Share

You are about to reply to a thread that has been inactive for 2201 days.

Please take a moment to consider if this thread is worth bumping.

Recommended Posts

Hi ....

so just like the title of the topic i am totally new with LSL, so i experimenting with the stuff i have. i got this nice cheap switchblade which has touch script which i am trying to replace it with chat command . the original script in the item make the blade of the switchblade swing open on touch

default
{
touch_start(integer tnum)
{
rotation rot = llEuler2Rot(<0 * DEG_TO_RAD, 0 * DEG_TO_RAD, 180 * DEG_TO_RAD>);
llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_ROT_LOCAL, rot]);
llSleep(0.2);
llPlaySound("switchblade-in", 1.0);
state open;
}
}
state open
{
touch_start(integer tnum)
{
rotation rot = llEuler2Rot(<0 * DEG_TO_RAD, 0 * DEG_TO_RAD, 0 * DEG_TO_RAD>);
llSetLinkPrimitiveParamsFast(LINK_THIS, [PRIM_ROT_LOCAL, rot]);
llSleep(0.2);
llPlaySound("switchblade-out", 1.0);
state default;
}
}

Been experimenting with a attach/detach script listen command to replace the touch command but still no luck.

could someone please help me out...i am really blind here but really want to learn ;)

Thank you

Link to comment
Share on other sites

This is probably not the easiest script to convert from touch to listen because it swaps between states and listens are automatically closed on state change, so llListen() would need to be called whenever it enters either state, among other complications.

If you really want to learn about states specifically, this is all possible, but if not, you can just use a global variable to keep track of whether the blade is open or closed.

Either way, the first step might be to get an appropriate llListen() called and the listen() event triggered. To that end: what do you want to chat to the attachment? (one thing to open and another to close, or a single thing to toggle its state from one to the other?)

Link to comment
Share on other sites

Like Qie, I would recommend avoiding the complications of states for a script as simple as this. All you need is a single global integer variable that you can use as a toggle for opening or closing the blade. Then, open a channel (with llListen) in the state_entry event of your script. Change the touch_start event to a listen event and listen for a message that will flip the value of your toggle from TRUE to FALSE or vice versa. Use the value to either open or close the blade. Easy peasy.

Link to comment
Share on other sites

I'm not at all sure you need states for a simple attach/detach script.

Let's take it step by step.   Since I don't know what your level of scripting knowledge is, I'm breaking it down into simple components that you can study and then put together to try and make something that works.

First, before the script can hear anything, it needs to be told to listen (to "open a listener," as we sometimes say) on a particular channel.   To do this, you use llListen.   Take a minute to look at the arguments llListen takes -- llListen(channel, name, id, message) where channel is the channel the script listens on, name is the name of the object or avatar to which to listen, id is the uuid of the object or avatar, and message is the message to which the object should listen.   

You always need to specify a channel.   You can leave the other arguments open by using "", so llListen(5,"","",""); means "Listen for any message from anyone or anything on channel 5".   llListen(5,"",llGetOwner(),""); means "Listen for messages from the owner of this script on channel 5".

When the script hears a message that passes the filter, the listen event http://wiki.secondlife.com/wiki/Listen fires (why can I sometimes use the link tool to create hyperlinks and sometimes not?).   So, in your example you might have something like this

//It's generally good practice to declare and define channels and keys up her, as global variables, so you can use them anywhere in the script, and readily change their values if necessary
integer iChannel = 5;
key kOwner;

default{
    state_entry()
    {
        kOwner = llGetOwner();//make kOwner hold the owner's uuid
        llListen(iChannel, "", kOwner, "");//listen to any message from the object's owner on whatever iChannel has been defined as
    }

    listen(integer channel, string name, key id, string message)
    {
        //to get this far, the script must have heard a message from the owner on the appropriate channel   
    }
}

 

What do you need in the listen event?   Let's see. The script needs to test to see whether the message is one it expects -- one telling it to attach or to detach -- or something else completely that it should simply ignore.   Either way, whether it's supposed to attach or detach itself from the the owner, it will need to check if it has permission to attach/detach itself.  To do this , it needs to call llRequestPermissions to attach/detach.   If it's already attached (and needs to detach itself) the permissions will be granted silently if it doesn't already have then.  If it's not attached, it will need to ask for permission to attach itself if it doesn't have permission to attach.  It can check whether it has permission to do what it wants by using http://wiki.secondlife.com/wiki/LlGetPermissions.   

To attach an item to someone, you call llAttachToAvatar(key avatar, integer attachementPoint)  http://wiki.secondlife.com/wiki/LlAttachToAvatar.   To detach the item, you simply call llDetachFromAvatar().

So, something like 

    listen(integer channel, string name, key id, string message)
    {
        message = llToLower(message);//put the message into lowercase, so the user doesn't need to worry about capitalisation
        if(~llListFindList(["attach","detach"], [message])){
            //and test if it's either "attach" or "detach". If it is, then proceed.   Otherwise the message isn't intended for this script so ignore it.
            // if(~llListFindList(haystack, needle)) is shorthand for if(llListFindList(haystack,needle) != -1), which is what llListFindList returns if there's no needle there
            if(llGetPermissions() & PERMISSION_ATTACH){//If the object already has permission to attach/detach
                if("attach" == message && !llGetAttached()){//if the message is "attach" and the object is not already attached
                    //llGetAttached returns either the integer corresponding to the attachment point, if the object is attached, or 0, if it isn't
                    //if(!llGetAttached()) is shorthand for if(llGetAttached != 0)
                    //do something to attach to the avatar
                }
                else if ("detach" == message && llGetAttached()){//if the message is "detach" and the object is already attached
                    //do something to detach from the avatar

                }
            }
            else{//the script does not have permission to attach/detach, so it has to ask for it
                llRequestPermissions(kOwner, PERMISSION_ATTACH);
            }
        }  
    }

When you call llRequestPermissions and the permission is granted, the http://wiki.secondlife.com/wiki/Run_time_permissions run_time_permissions event fires.   So use smetihng like this 


    run_time_permissions(integer perm)
    {
        if(perm & PERMISSION_ATTACH){//always check you have permission to do what you need to do
            if(!llGetAttached()){//the object is not attached, so
                llAttachToAvatar(ATTACH_RHAND);//attach to the avatar's right hand, or wherever else you specify
            }
            else if (llGetAttached()){//the object is already attached,so
                llDetachFromAvatar();
            }
        }
    }

You should be able to see from that what to do in the listen event, if you already have PERMISSION_ATTACH, which you will, after you've requested permissions the first time -- the permission lasts until the script is reset, so the next time you rez the item from your inventory, it will retain the permissions.     Because of this, you need to add a further event, the changed event, in case the object changes ownership, so the script knows to forget the old permissions and find the uuid of its new owner.   Something like 

    changed(integer change)
    {
        if(change & CHANGED_OWNER){ //if the item's owner changes
            llResetScript();//reset the script, in order to kill the old listener, forget the old set of permissions, read the new owner's uuid and start listening to the new owner.
        }   
    }

So, put it all together (remembering you need to do a bit in the listen event to make it attach/detach if it already has PERMISSION_ATTACH) and see how it goes.   Best of luck!

Link to comment
Share on other sites

wow...and i thought this would be a good simple script where i can learn the basic..i sure going to try the tips from Innula  Thank you soo much for all the replies...at the moment i just delete the whole script and made one set link alpha script like the one usually used on guns draw/sling ended up attaching 2 knives.. ;) looked like working the way i wanted but just not the same. so imma try till i get it...wish me luck ;) 

Link to comment
Share on other sites

@NawaliaTrea  In practice, I'd recommend simply switching the knife's alpha (and starting and stopping a knife-holding animation) when you want to threaten someone with your knife. 

You specifically asked, though, about attaching and detaching things, so I explained how to do that, but if you want to draw/sheath knives, then I think switching the knife's visibility is the way to do it.

Link to comment
Share on other sites

10 hours ago, Innula Zenovka said:

@NawaliaTrea  In practice, I'd recommend simply switching the knife's alpha (and starting and stopping a knife-holding animation) when you want to threaten someone with your knife. 

You specifically asked, though, about attaching and detaching things, so I explained how to do that, but if you want to draw/sheath knives, then I think switching the knife's visibility is the way to do it.

yes i think so. i dcurious if i can replace the touch command with a chat command. cos the knife will swing open on touch which is very nice in my opinion. instead just appear and disappear. But your explaination is really help me with something else i have in mind, sometimes i just detach something in sim and reattach it when needed it would be great if i can do that with a chat command. like you explained thanks soo much ;) 

Link to comment
Share on other sites

15 hours ago, NawaliaTrea said:

yes i think so. i dcurious if i can replace the touch command with a chat command. cos the knife will swing open on touch which is very nice in my opinion. instead just appear and disappear. But your explaination is really help me with something else i have in mind, sometimes i just detach something in sim and reattach it when needed it would be great if i can do that with a chat command. like you explained thanks soo much ;) 

That's easy to do.   As Rolig and Qie have suggested, using two states is overkill for this (and overcomplicates things no end).  The way I would do it is to create a global integer, iOpen by declaring it right up at the top of the script, before default.

If you don't give it a value, then by default it is FALSE (which is what we want here).

Then, in the listen event, you do something like this 

    listen(integer channel, string name, key id, string message)
    {
        message = llToLower(message);//put the message into lowercase, so the user doesn't need to worry about capitalisation
        if(~llListFindList(["open","close"],[message])){//if the message is either "open" or "close"
            iOpen=!iOpen;//flip the value of iOpen between FALSE and TRUE
            if(iOpen){//shorthand for if (iOpen == TRUE)
                //do stuff to open the knife 
            }
            else{
                //do stuff to close the knife
            }
        }
    }

You can take the code for opening and closing the knife direct from your existing script.

Link to comment
Share on other sites

I offered help for this in another thread, but seeing the same thing reposted scattershot kinda makes me not want to help now.

I tackled all of this last year, and have nice front-openers and even a classic swinguard stilleto, all using chat commands to draw/hide/open/close. I find the clunky rotation of a textured disc on a touch event to be a rather silly way to open a knife, and feel Innula is right about simply toggling visibility.

A lot of what is made in SL attempts to mimic real-world objects and their behavior, but we have the advantage of being able to short-circuit appearances and cast illusions that are as good as, or better than the real deal, at least as far as scripting and physics are concerned. Whenever possible I try to remember that "virtual" means you don't actually have to have a swinging blade and rotate it when someone uses the mouse to click a button.

Anyway, using chat commands to do this is as easy as pie, but I've found a small HUD button made out of a tortured cube so that 3 faces show, and using the chat commands on a hidden channel when the faces are clicked, works far better.

There's nothing quite as embarrassing as typing a chat command to do something by surprise, and the viewer does something stupid, like leave off the starting "/". <-<;

Edited by Berksey
Link to comment
Share on other sites

1 hour ago, Berksey said:

I offered help for this in another thread, but seeing the same thing reposted scattershot kinda makes me not want to help now.

Blame me for that.  I suggested to the OP -- in a private message -- that it might be better to start a new thread to ask the question rather than pose it in the middle of a thread to which it wasn't, in my view, relevant.  

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 2201 days.

Please take a moment to consider if this thread is worth bumping.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...