Jump to content

Returning dropped object back to place it was dropped.


Myth024
 Share

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

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

Recommended Posts

I have an object that has a certain type of physics that means that object can be kicked into random places. I would like to have it return to the orginal place it was dropped when touched. I'm guessing that I would have to generate a global variable that "records" it's start position, then recall it later in when touched.

I've tried

vector position = llGetPos();

 before the default state to set it as a global variable however, I get the following error.

 An object reference is required for the nonstatic field, method, or property 'llGetPos()'    
If someone has an idea of how to set a global variable that holds the position, I think I can figure out the rest. Originally I tried to have two linked objects so I could have the physics enabled object inside a box but both objects kept running the same script and defeating the purpose of containing a moving object inside a non moving object. Oh, yea, I'm really new to LSL scripting so yea, I'm a noob, but I've been trying to find the answer through documentation and this forum is my next shot. Thanks for your help.



Link to comment
Share on other sites

Like Darkie said, one can only do static assignments when declaring global variables, such as integer a = 1;. But even trying to do integer a = 1+2; won't compile.

 

Usually global variables are initialized in the state_entry() event of default. That way, every time the script is reset, the globals get re-initialized, possibly with new values than before.

 

vector gPosition; // declare it heredefault{    state_entry()    {        gPosition = llGetPos(); // initialize it here    }}

 

Link to comment
Share on other sites

To be sure I understand what your telling me. I can't run any functions outside of a state and therefore can only call them inside one. So here is another question. If I declare a variable inside the default state, can I still referr to the variable inside other states?  So if I say, for example,

default 
{
state_entry
{
gPosition = llGetPos();
}
state one
}

state one
{
state_entry
{
bunch of stuff that moves the object to some random place.
}
state reset_pos
}
state reset_pos
{
state_entry
{
llSetPos(gPostion);
}
state default
}

Now I know the syntax may not be perfect, I just popped that down off the top of my head. In theory, when the objected is rezzed it would get it's position, then when moving on to the reset_pos state, it would go back to that same position. then the script would start over. Thus the object would always return to the original spot it was "dropped".  Am I following that correctly or am I missing something?

Thanks for your responses they have already helped.

Link to comment
Share on other sites

Almost right.  To be a global variable, you have to declare it at the head of the script, outside the scope of individual states.  Then you can use it anywhere, changing the value assigned to it whenever you want and being confident that the value will be accessible globally --- that is, throughout your script.  So...

integer gGlobal_variable = 12;  // gGlobal_variable is defined here and given a default valuedefault{    state_entry()    {        llSay(0, (string) gGlobal_variable);  // The result will be 12 (defined above and reset again in state new .... see below)        gGlobal_variable += 10;        llSay(0,(string)gGlobal_variable);  // Now the result will be 22    }    touch_start(integer num)    {        llSay(0, (string)gGlobal_variable);  // The result will be 22 (set in state_entry)        state new;    }}state new{    state_entry()    {        llSay(0, (string)gGlobal_variable);  // The result will be 22 (set in state_entry in state default)     }    touch_start(integer num)    {        gGlobal_variable = 12;        llSay(0, (string)gGlobal_variable);  // The result will be 12        state default;    }}

 EDIT: I can't add at midnight, but I can fix it after breakfast.

 

 

Link to comment
Share on other sites

Study this code, with it's comments, and the chat it returns when started and then when touched.

 

integer i = 2; // declaration for the global "i"integer nada (){ // within a subroutine (user function)    integer i = 7; // this "i" is local to this function    return i; // but we can return the value of it, like so}default{    state_entry()    {         llOwnerSay((string)i); // Global "i" is available until...                // ... within an event        integer i = 4; // now "i" is declared as a local variable        llOwnerSay((string)i);        { // or within a code block (such as when using "if" or in a loop)            integer i = 3; // this "i" is only local within the code block             llOwnerSay((string)i);        }        llOwnerSay((string)i); // back to the state_entry local "i"    }    touch_start(integer total_number)    {         llOwnerSay((string)i); // Here is the global "i" still!         llOwnerSay((string)nada()); // Chat the "i" that exists in nada()         state next;    }}state next{    state_entry()    {        llOwnerSay((string)i); // Here is the global "i" still!    }}

 

Link to comment
Share on other sites

You are about to reply to a thread that has been inactive for 3967 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...