Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

List Management

Yonke Ming
Fat Laughing Hyena
Join date: 18 Jun 2005
Posts: 16
08-18-2005 22:50
When you touch the prim, you are added to the list.

But when you touch the prim again, you get removed from the list. But that part doesn't seem to be working.

CODE

list names;
key avatar;
string name;
integer length = 0;
integer namefound;
integer location;
default
{

touch_start(integer total_number)
{
namefound = 0; location = 0;
avatar = llDetectedKey(0);
length = llGetListLength(names);
name = llKey2Name(avatar);
integer i = 0;
//Searches for your name.
for (i = 0; i == length; i++)
{
if (name = llList2String(names, i))
{
namefound = 1;
location = i;
}

}
//If not found, adds you.
if (namefound != 1)
{
if (length != 3)
{
llSay(0, "The number of people on this list is now "+(string)length+".");
names += name;
}
else
{
llSay (0, "Max members of a team reached. Try again when someone leaves a team.");
}
}else
{
//If found, removes you.
names = llDeleteSubList(names, location, location);
}

//List display (For now, the first two names)
llSetText("Green Team: \n"+llList2String(names, 0)+"\n"+llList2String(names, 1)+"",<1,1,1>,1);
}
}
_____________________
I like Hyenas...

...Especially the soft, fat, squishy, and silly ones!
Judah Jimador
Registered User
Join date: 13 Mar 2005
Posts: 230
08-18-2005 23:10
Hi Yonke,

I think this is what's biting you...


CODE

for (i = 0; i == length; i++)
{
if (name = llList2String(names, i))
{
namefound = 1;
location = i;
}


You're trying to test for equality, but you're doing assignment instead.

I only do this about twice a week myself :p

-- jj
Alondria LeFay
Registered User
Join date: 2 May 2003
Posts: 725
08-18-2005 23:14
Don't cycle through the list, use llListFindList() instead. Here is a bit more optimized (and working) version. :)

CODE

list names;
default
{
touch_start(integer total_number)
{
string name = llDetectedName(0);
integer location = llListFindList(names, [ name ]);
if (location == -1)
{
if (llGetListLength(names) < 3) // <--- Or whatever the maximum players should be
{
names = names + [ name ];
llSay(0, "The number of people on this list is now "+(string)llGetListLength(names)+".");
}
else
{
llSay (0, "Max members of a team reached. Try again when someone leaves a team.");
}
}
else
{
names = llDeleteSubList(names, location, location);
}
llSetText("Green Team: \n"+llList2String(names, 0)+"\n"+llList2String(names, 1)+"",<1,1,1>,1);
// Or if you want to list all the names, do a llDumpList2String(names,"\n")
}
}
a lost user
Join date: ?
Posts: ?
08-19-2005 04:39
Definitely do what Alondria has suggested.. but to clarify something that seems to have been omitted by the second poster:

CODE

length = llGetListLength(names);
name = llKey2Name(avatar);
integer i = 0;
//Searches for your name.
for (i = 0; i == length; i++)
{
if (name = llList2String(names, i))
{
namefound = 1;
location = i;
}
}


Ok.. if you have two names of the list then "length" is going to equal 2. When you go into the for loop, you need to look only at two names, not 3.. that's list element 0 and element 1.. 2 list items.. so it should be:
for(i = 0; i < length; i++)

Then in your IF statement, as the second poster pointed out you are not using the correct conditional operator to "compare" two values.

To assign a value to a variable, use a single = sign:
integer a;
a = 1;
llSay(0,(string)a);

To compare two values, use two = signs:
integer a;
integer b;
a = 1;
b = 2;
if(a == b) llSay(0,(string)a);

if you went:
if((a = b + 1) == 3) llSay(0,"a = " + (string)a);

You will notice that you have changed the value of a to: b + 1
<,>,<=,>=,==,!= are all comparisons.
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
08-19-2005 05:13
just to be really picky in a for() statement ++i is faster than i++.

So
CODE

for(i=0; i<length; ++i)


For short lists it's a tiny variation, for longer lists it makes a difference.
Judah Jimador
Registered User
Join date: 13 Mar 2005
Posts: 230
08-19-2005 06:24
What they said, Yonke.

Gah...after the day I had yesterday, I should've known better than to think I'd done a decent job thinking about something. Everything I touched yesterday outsmarted me.

Maybe today, I can actually function...

Thanks for the "++i" versus "i++" tip :D

*double-checks shoelaces*
*sighs and heads off to work*

-- jj