Difference between revisions of "User talk:Toady Nakamura"

From Second Life Wiki
Jump to navigation Jump to search
(reply with a lengthy explanation of how to deal with lists in LSL)
m (for class tomorrow i will clean up later)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Leave me a message !!! [[User:Toady Nakamura|Toady Nakamura]] 23:44, 5 July 2012 (PDT)
Leave me a message !!! [[User:Toady Nakamura|Toady Nakamura]] 23:44, 5 July 2012 (PDT)\


<lsl>
<source lang="lsl2">
//Teleporters use the Changed Event
 
 
// —— Global variables
vector gDestination = <000,000,000>; // don’t change in class ! (see note one)
integer gAccessMode = 1; // Change who can use the teleporter: 1 - public, 2 - owner, 3 - group.
 
//—- all for hover text
string gText = "Sit here for fun teleport"; // hover text
vector gTextColor = < 0.42 , 1.0 , 0.42 > ; // color of hover text 
float  gAlpha = 1.0; // transparency of hover text
 
//====Script starts here with default ==========
 
default
{
    state_entry()
    {
        llSitTarget(<0.0, 0.0, 0.51>, ZERO_ROTATION);
        llSetSitText("Teleport");
        llSetTouchText("Teleport");
        llSetText(gText, gTextColor, 1.0);
        llSetClickAction(CLICK_ACTION_SIT);
 
vector offset = llGetPos() + < 0, 0, 10 >;
gDestination = gDestination + offset;
    }
 
    changed(integer change)
    {
        if (change & CHANGED_LINK)
        {
            key USER = llAvatarOnSitTarget();
 
            if (llGetAgentSize(USER) != ZERO_VECTOR) // it’s really an avatar!
            {
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
                if (gDestination == <0.0,0.0,0.0>)  // destination not set !!
                {
                    llUnSit(USER);  // unsit user
                    llOwnerSay("WARNING ! Teleporter destination is set to 0,0,0.  Aborting.");
                }
                integer access_granted = FALSE; // did not let this proceed
 
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
                if (gAccessMode == 1)  // 1 is public, anyone can use
                  {  access_granted = TRUE;} // you are good to go!
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
                else if (gAccessMode == 2)  // 2 is owner, so owner only
                {
                    if (USER == llGetOwner()) // if user is the same as owner 
                      {  access_granted = TRUE; } // you are good to go!
                    else
                    {
                        llUnSit(USER);
                        llSay(0,"Sorry, " + llKey2Name(USER) + "teleporter for owner use only.");
                    }
                }
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
                else if (gAccessMode == 3) // 3 is group, so only same group as rezzed
                {
                    if (llSameGroup(USER)) // if user is in same group
                        { access_granted = TRUE; } // they are good to go!
                    else
                    {
                        llUnSit(USER);
                        llSay(0,"Sorry," + llKey2Name(USER) + " teleporter for group use only.");
                    }
                }
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 
                if (access_granted) // Yippie, you have permission! 
                {
                    vector _InitPos = llGetPos(); // gets starting position
                    llSetRegionPos(gDestination); // sends to destination
                    llUnSit(USER);   // unsit !
                    llSetRegionPos(_InitPos);   // go back to start pos
                }
            }
        }
    }
}
 
</source>
 
 
<source lang="lsl2">
//Counts down from 5 to 1, then can do something else
//Counts down from 5 to 1, then can do something else
default
default
Line 21: Line 108:
     }
     }
}
}
</lsl>
</source>


== Debug error ==
== Debug error ==
Line 37: Line 124:
== Regarding your question about lists in LSL on [[User_talk:Strife Onizuka]] 's page: ==
== Regarding your question about lists in LSL on [[User_talk:Strife Onizuka]] 's page: ==


Usual list with 10 items:
Please read [[User:Kireji_Haiku/How_to_deal_with_lists_in_LSL|this intro of how to iterate over a list in LSL]]. Hope it helps, kind regards. -- [[User:Kireji Haiku|Kireji Haiku]] 12:40, 5 November 2012 (PST)
 
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
</lsl>


Then start with a for-loop:
== Your simple listen script ==


<lsl>
You do say at the top of your talk page "Leave me a message !!!" ... so I am :)
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];


integer index = 0;
I couldn't resist commenting on your recently posted script. Easiest is if I post the script here with my comments inserted (enclosed within **  **)
for (index = 0;index < llGetListLength(listOfTenItems);index++)
{
    //do something
}
</lsl>


Resetting the value to 0 for index was redundant so we can skip that:
(content omitted)
[[User:Omei Qunhua|Omei Qunhua]] 01:38, 29 January 2014 (PST)
:Thank you for your comments, you corrected a script I expect my students to correct in class so I have removed your corrections here so they don't cheat and just use yours.  [[User:Toady Nakamura|Toady Nakamura]] 14:16, 11 March 2014 (PDT)


<lsl>
== [[User:Toady_Nakamura/Give_Anything]]: ==
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];


integer index = 0;
Try this instead?
for (;index < llGetListLength(listOfTenItems);index++)
{
    //do something
}
</lsl>


Setting the default value to 0 is redundant, too.
<source lang="lsl2">
vector floattext_color = < 1.0, 1.0, 1.0>;
string floattext_text  = "Put your floating message here.";
string foldername      = "Put the name of the folder they will get here.";


<lsl>
list contents;
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];


integer index;
default
for (;index < llGetListLength(listOfTenItems);index++)
{
{
     //do something
     changed(integer change)
}
    {
</lsl>
        if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
            llResetScript();
    }


    state_entry()
    {
        llSetText(floattext_text, floattext_color, 1.0);


Calculating the list length every step is redundant too because the length doesn't change.
        integer c = llGetInventoryNumber(INVENTORY_ALL);
        integer i = 0;


<lsl>
//      for-loops below have the same effect in this particular example
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
integer lengthOfList = llGetListLength(listOfTenItems);


integer index = 0;
//      for (; i < c; i += 1) ...;
for (;index < lengthOfList;index++)
//      for (; i < c; i++)   ...;
{
//     for (; i < c; ++i)    ...;
    //do something
}
</lsl>


Now the index for items in a list of ten is either:
        for (i = 0; i < c; i = i + 1)
        {
            string  n = llGetInventoryName(INVENTORY_ALL, i);
            integer t = llGetInventoryType(n);


<pre>
//          if you include INVENTORY_SCRIPT below, you need to
0,  1,  2,  3,  4,  5,  6,  7,  8,  9
//          check against if (n != llGetScriptName()) as well to not give away this script


//  or
            if (t & (INVENTORY_LANDMARK | INVENTORY_NOTECARD | INVENTORY_OBJECT))
                contents += n;
        }
    }


-10, -9, -8, -7, -6, -5, -4, -3, -2, -1
    touch_start(integer num_detected)
</pre>
    {
        key    avatarKey  = llDetectedKey(0);
        string avatarName = llDetectedName(0);


Because ++index is faster than index++ we could start with -11 and increment first before doing something, instead of the other way around before... as long as the index stays negative. We can write:
        if (llGetListLength(contents))
            llGiveInventoryList(avatarKey, foldername, contents);


<lsl>
        llOwnerSay(avatarName + " got supplies.");
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
     }
integer lengthOfList = llGetListLength(listOfTenItems);
 
integer index = -lengthOfList - 1;
for (;index < 0;++index)
{
     //do something
}
}
</lsl>
</source>


Now trying to simplify the math for calculating the index default value we can use '''bitwise-NOT of the length''' which is the same as the negative length minus one:
Cheers! '''[[User:Kireji_Haiku|Kireji Haiku]]''' <sup><small>([[User talk:Kireji_Haiku|talk]]|[[Special:Contributions/Kireji_Haiku|contribs]])</small></sup> 01:02, 2 September 2014 (PDT)


<lsl>
==next up==
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
<source lang="lsl2">
integer lengthOfList = llGetListLength(listOfTenItems);
default
 
//  it's bitwise-NOT (~) not minus (-)
integer index = ~lengthOfList;
for (;index < 0;++index)
{
    //do something
}
</lsl>
 
as you might have seen by now, we can merge two lines in the middle:
 
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
 
//  it's bitwise-NOT (~) not minus (-)
integer index = ~llGetListLength(listOfTenItems);
for (;index < 0;++index)
{
    //do something
}
</lsl>
 
and because index should have a negative value while looping and the loop stops when index reaches zero, we can change the code to:
 
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
 
//  it's bitwise-NOT (~) not minus (-)
integer index = ~llGetListLength(listOfTenItems);
for (;index;++index)
{
    //do something
}
</lsl>
 
and improve readability by using a while-loop:
 
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
 
//  it's bitwise-NOT (~) not minus (-)
integer index = ~llGetListLength(listOfTenItems);
while (++index)
{
    //do something
}
</lsl>
 
and try to get rid of the bitwise-NOT because some people find it hard to read. The positive side effect of the next step is that you need to increment one step less in total and the loop runs the first step directly without checking first for a conditional statement:
 
<lsl>
list listOfTenItems = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"];
 
// negative length now!
integer index = -llGetListLength(listOfTenItems);
 
do
{
{
     //do something
  state_entry()
  {
     integer p;
    for (p = 2; p <= llGetNumberOfPrims(); p++)
      llSetLinkPrimitiveParamsFast(p, [PRIM_NAME, llGetObjectName(), PRIM_DESC, llGetObjectDesc()]);
  }
}
}
while (++index);
</source>
</lsl>


Hope it helps, kind regards. -- [[User:Kireji Haiku|Kireji Haiku]] 12:40, 5 November 2012 (PST)
that way I don't frogget to do it.

Latest revision as of 00:27, 2 December 2020

Leave me a message !!! Toady Nakamura 23:44, 5 July 2012 (PDT)\

//Teleporters use the Changed Event 


// —— Global variables 
vector gDestination = <000,000,000>; // don’t change in class ! (see note one)
 
integer gAccessMode = 1; // Change who can use the teleporter: 1 - public, 2 - owner, 3 - group.

//—- all for hover text
string gText = "Sit here for fun teleport"; // hover text
vector gTextColor = < 0.42 , 1.0 , 0.42 > ; // color of hover text  
float  gAlpha = 1.0; // transparency of hover text

//====Script starts here with default ==========

default
{
    state_entry()
    {
        llSitTarget(<0.0, 0.0, 0.51>, ZERO_ROTATION);
        llSetSitText("Teleport");
        llSetTouchText("Teleport");
        llSetText(gText, gTextColor, 1.0);
        llSetClickAction(CLICK_ACTION_SIT);

	vector offset = llGetPos() + < 0, 0, 10 >;
	gDestination = gDestination + offset;
    }

    changed(integer change) 
    {
        if (change & CHANGED_LINK)
        {
            key USER = llAvatarOnSitTarget();

            if (llGetAgentSize(USER) != ZERO_VECTOR) // it’s really an avatar!
            {
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  
                if (gDestination == <0.0,0.0,0.0>)  // destination not set !! 
                {
                    llUnSit(USER);  // unsit user
                    llOwnerSay("WARNING ! Teleporter destination is set to 0,0,0.  Aborting.");
                }
                integer access_granted = FALSE; // did not let this proceed

// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  
                if (gAccessMode == 1)  // 1 is public, anyone can use
                   {   access_granted = TRUE;} // you are good to go!
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
                else if (gAccessMode == 2)  // 2 is owner, so owner only
                {
                    if (USER == llGetOwner()) // if user is the same as owner  
                      {  access_granted = TRUE; } // you are good to go!
                    else
                    {
                        llUnSit(USER);
                        llSay(0,"Sorry, " + llKey2Name(USER) + "teleporter for owner use only.");
                    }
                }
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  
                else if (gAccessMode == 3) // 3 is group, so only same group as rezzed
                {
                    if (llSameGroup(USER)) // if user is in same group
                        { access_granted = TRUE; } // they are good to go!
                    else
                    {
                        llUnSit(USER); 
                        llSay(0,"Sorry," + llKey2Name(USER) + " teleporter for group use only.");
                    }
                }
// .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  
                if (access_granted) // Yippie, you have permission!  
                {
                    vector _InitPos = llGetPos(); // gets starting position
                    llSetRegionPos(gDestination); // sends to destination
                    llUnSit(USER);		  // unsit ! 
                    llSetRegionPos(_InitPos); 	  // go back to start pos
                }
            }
        }
    }
}


//Counts down from 5 to 1, then can do something else
default
{
    state_entry()
    {
        integer count = 5;
        do
        {

            llSay(0, (string)count);

        //  wait a sec
            llSleep(1.0);
        }
        while (--index);
 
        llWhisper(0, "I am done counting now.");
    }
}

Debug error

Um I am sorry if I am going about this the wrong way but from what I can tell this was the only way to talk on the wiki and I had a question about the LSL 101 Logic page, the example script in particular, I copied it(sort of, I actually typed it out because of the note at the bottom) in the LSLEditor community edition and ran the debug... thing, and got the error "Field 'n' is never assigned to, and will always have its default value"

I was wondering if this was an actual issue, and I wonder this because I have no actual coding knowledge. I apologize profusely if I have the wrong person or posted this where I shouldn't have. If you would like to see how it came out exactly I will of course provide what was written.

--Rohise Resident 22:56, 9 October 2012 (PDT)

Variable names

Greetings, I rewrote your example script on User:Toady_Nakamura/Simple_Recording_Tipjar. For sake of readability, please use variable names that actually mean something amd not i, m, n and what not. Your doing yourself a favor by doing so! -- Kireji Haiku 11:18, 19 October 2012 (PDT)

I replied on your talk page, as is customary. You did not reply and you wiped out my reply to you. Toady Nakamura 10:07, 4 November 2012 (PST)

Regarding your question about lists in LSL on User_talk:Strife Onizuka 's page:

Please read this intro of how to iterate over a list in LSL. Hope it helps, kind regards. -- Kireji Haiku 12:40, 5 November 2012 (PST)

Your simple listen script

You do say at the top of your talk page "Leave me a message !!!" ... so I am :)

I couldn't resist commenting on your recently posted script. Easiest is if I post the script here with my comments inserted (enclosed within ** **)

(content omitted) Omei Qunhua 01:38, 29 January 2014 (PST)

Thank you for your comments, you corrected a script I expect my students to correct in class so I have removed your corrections here so they don't cheat and just use yours. Toady Nakamura 14:16, 11 March 2014 (PDT)

User:Toady_Nakamura/Give_Anything:

Try this instead?

vector floattext_color = < 1.0, 1.0, 1.0>;
string floattext_text  = "Put your floating message here.";
string foldername      = "Put the name of the folder they will get here.";

list contents;

default
{
    changed(integer change)
    {
        if (change & (CHANGED_OWNER | CHANGED_INVENTORY))
            llResetScript();
    }

    state_entry()
    {
        llSetText(floattext_text, floattext_color, 1.0);

        integer c = llGetInventoryNumber(INVENTORY_ALL);
        integer i = 0;

//      for-loops below have the same effect in this particular example

//      for (; i < c; i += 1) ...;
//      for (; i < c; i++)    ...;
//      for (; i < c; ++i)    ...;

        for (i = 0; i < c; i = i + 1)
        {
            string  n = llGetInventoryName(INVENTORY_ALL, i);
            integer t = llGetInventoryType(n);

//          if you include INVENTORY_SCRIPT below, you need to
//          check against if (n != llGetScriptName()) as well to not give away this script

            if (t & (INVENTORY_LANDMARK | INVENTORY_NOTECARD | INVENTORY_OBJECT))
                contents += n;
        }
    }

    touch_start(integer num_detected)
    {
        key    avatarKey   = llDetectedKey(0);
        string avatarName = llDetectedName(0);

        if (llGetListLength(contents))
            llGiveInventoryList(avatarKey, foldername, contents);

        llOwnerSay(avatarName + " got supplies.");
    }
}

Cheers! Kireji Haiku (talk|contribs) 01:02, 2 September 2014 (PDT)

next up

default
{
  state_entry()
  {
    integer p;
    for (p = 2; p <= llGetNumberOfPrims(); p++)
      llSetLinkPrimitiveParamsFast(p, [PRIM_NAME, llGetObjectName(), PRIM_DESC, llGetObjectDesc()]);
  }
}

that way I don't frogget to do it.