Jump to content
ainst Composer

Object moves to original position : Script reset

Recommended Posts

Hello! I have such a funny script, it works fine, but for some reason, every time I change the position of the object or just rez it from inventory, I have to reset the scripts. otherwise, the object either flies up to the sky or moves to its original position. why it happens?

 

string animation; // the first animation in inventory will automatically be used
// the animation name must be stored globally to be able to stop the animation when standing up

integer k;
integer flip = 1;
list params;
vector pos;
integer link = 0;


default {
    state_entry() {
        // set sit target, otherwise this will not work 
        llSitTarget( < 0.0, 0.3, -1.0 > , <0.0, 0.0, -1.0, 1.0> );//***
        ///ZERO_ROTATION
        params = llGetLinkPrimitiveParams(LINK_ROOT, [PRIM_POSITION]);
        pos = llList2Vector(params, 0);
    }

    changed(integer change) {
        if (change & CHANGED_LINK) {
            key av = llAvatarOnSitTarget();
if (av) //evaluated as true if not NULL_KEY or invalid
{
    llRequestPermissions(av, PERMISSION_TRIGGER_ANIMATION);
    llSetTimerEvent(0.02 + llFrand(0.1));
}

            else // avatar is standing up
            {
                if (animation)
                    llStopAnimation(animation); // stop the started animation
                llResetScript(); // release the avatar animation permissions
                llSetTimerEvent(0.0);
            }
        }
    }

    run_time_permissions(integer perm) {
        if (perm & PERMISSION_TRIGGER_ANIMATION) {
            animation = llGetInventoryName(INVENTORY_ANIMATION, 0); // get the first animation from inventory
            if (animation) {
                llStopAnimation("sit"); // stop the default sit animation
                llStartAnimation(animation);
            }
        }
    }

    timer() {
        llSetLinkPrimitiveParamsFast(LINK_ROOT, [PRIM_POSITION, pos + ((flip = -flip) * < 0.02, 0.01, 0.01 > )]);
    }



}

 

ezgif.com-video-to-gif.gif

Edited by ainst Composer

Share this post


Link to post
Share on other sites
9 minutes ago, steph Arnott said:

Reset it using the on_rez(integer start_param)llResetScript();

Hi! Thanks very much!

I need to replace a State_Entry  event with this one or add this event? And it will help even if i only reposition object too?

Edited by ainst Composer

Share this post


Link to post
Share on other sites
Just now, ainst Composer said:

Hi! Thanks very much!

I need to replace a State_Entry  event with this one or add this event?

Add after the state_entry. state_entry is allway the first in the queue.

  • Thanks 1

Share this post


Link to post
Share on other sites
11 minutes ago, steph Arnott said:

Add after the state_entry. state_entry is allway the first in the queue.

And if i only reposition object it will work too? 

Share this post


Link to post
Share on other sites

The reason it's doing this in the first place is because you only get the object's position once - at the very beginning of the script in its state_entry event. You're storing the position in the "pos" variable, so the script will remember it for as long as the script is running. As originally written, your script will only update the pos variable if the script gets rest, which in turn forces the state_entry event to be run anew.

As Steph suggests, you can work around this by forcing the script to reset itself every time the object is rezzed. Adding a new event,  on_rez, and putting in it the command to reset the script should do what you want. The on_rez event is automatically called when the script detects that its host object has just been rezzed in-world.

However, in regards to your second question, this alone will not update the position if you manually reposition the object without resetting the script afterward. You would need to write additional code to keep track of the last known position and check if it is equal or not to the current position and update accordingly.

  • Thanks 1

Share this post


Link to post
Share on other sites

What is the purpose of "flip" in the code?

In the timer event, which starts on sit, the script just changes the object's position very slightly. (Based on the position stored on script start.) This doesn't seem like it belongs to the script as it's an outhouse and those don't tend to.. hop around. If you remove the timer event, you don't need to restart the script either. 

  • Thanks 1

Share this post


Link to post
Share on other sites
8 minutes ago, Wulfie Reanimator said:

What is the purpose of "flip" in the code?

In the timer event, which starts on sit, the script just changes the object's position very slightly. (Based on the position stored on script start.) This doesn't seem like it belongs to the script as it's an outhouse and those don't tend to.. hop around. If you remove the timer event, you don't need to restart the script either. 

Yes the flip shakes the object a bit

Share this post


Link to post
Share on other sites
13 minutes ago, Fenix Eldritch said:

However, in regards to your second question, this alone will not update the position if you manually reposition the object without resetting the script afterward. You would need to write additional code to keep track of the last known position and check if it is equal or not to the current position and update accordingly.

Would have to get into

if( (llGetRot() != LastRot) || (llGetPos() != LastPos) )

{

blah, blah

}

and i really do not want to.

  • Like 1
  • Thanks 1

Share this post


Link to post
Share on other sites
5 minutes ago, steph Arnott said:

Would have to get into

if( (llGetRot() != LastRot) || (llGetPos() != LastPos) )

{

blah, blah

}

and i really do not want to.

Wow! Thanks a lot! 

And in which part of the script it goes?

Share this post


Link to post
Share on other sites

Well... rotation doesn't even come into play here, so it would be even simpler. We already have the previous position saved in the "pos" variable, so just check that against the current position returned by querying llGetPos().

if (llGetPos() != pos)
{
	pos = llGetPos();
}

Putting that test right before you start the timer (when the avatar first sits on the object) should ensure the start position is refreshed.

Edit: Heck, we could even go simpler... You could skip the test part entirely and just issue pos = llGetPos(); by itself when the avatar first sits down. That forces the script to update the start position before it vibrates the outhouse wherever it may be. Technically wouldn't even need to reset the script then...

Edited by Fenix Eldritch
  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites
1 minute ago, Fenix Eldritch said:

Well... rotation doesn't even come into play here, so it would be even simpler. We already have the previous position saved in the "pos" variable, so just check that against the current position returned by querying llGetPos().


 

 

True, but doing half a job for the sake of a few bytes is not worth the effort.

  • Thanks 1

Share this post


Link to post
Share on other sites
4 hours ago, Fenix Eldritch said:

Well... rotation doesn't even come into play here, so it would be even simpler. We already have the previous position saved in the "pos" variable, so just check that against the current position returned by querying llGetPos().


if (llGetPos() != pos)
{
	pos = llGetPos();
}

Putting that test right before you start the timer (when the avatar first sits on the object) should ensure the start position is refreshed.

Edit: Heck, we could even go simpler... You could skip the test part entirely and just issue pos = llGetPos(); by itself when the avatar first sits down. That forces the script to update the start position before it vibrates the outhouse wherever it may be. Technically wouldn't even need to reset the script then...

Thanks very much! Both versions work!

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...