Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

If case with string varables = doesn't work

Angus Balboa
Registered User
Join date: 19 Aug 2006
Posts: 14
11-05-2007 14:23
Not sure if this has been asked before, but since I couldn't find an answer after searching, I decided to ask...

So anyway, I have my object listen for the owner to say something, and then what is said is disected and part of it is stored into a variable (using llGetSubString). We'll say string hello.

The problem is, when I use an if case in the listen event (message == hello), it doesn't work. It'll save and compile, but any time I say what should be in that string (and I've used llOwnerSay(hello) in many parts of the script to make sure it was stored properly), nothing happens. The if case will work with (message == "saywhatever";), but not with variables. Why?
Meade Paravane
Hedgehog
Join date: 21 Nov 2006
Posts: 4,845
11-05-2007 14:29
Can you post the script?
_____________________
Tired of shouting clubs and lucky chairs? Vote for llParcelSay!!!
- Go here: http://jira.secondlife.com/browse/SVC-1224
- If you see "if you were logged in.." on the left, click it and log in
- Click the "Vote for it" link on the left
Angus Balboa
Registered User
Join date: 19 Aug 2006
Posts: 14
11-05-2007 14:38
Eh... might as well. It's not anything special anyway. This is an attempt to give the object a "name" (which works), and then have it say hello to the owner when the owner says the name. But that part doesn't work.


string fname;

default
{

state_entry()
{
llListen(0,"",llGetOwner(),"";);
}

listen(integer channel, string name, key id, string message)
{

//checks for the rename command
if (llGetSubString(message, 0, 7) == "/frename";)
{
//renames object with whatever is typed after the command
fname = llGetSubString(message, 8, -1);
llOwnerSay(fname);
}

if (message == fname)
{
llSay(0, "Hello " + (string) llKey2Name(llGetOwner()) + "!";);
}
}
}
Kidd Krasner
Registered User
Join date: 1 Jan 2007
Posts: 1,938
11-05-2007 14:41
Anytime you have something of the form:

if (x == y) ...

and it doesn't seem to be working, the simplest way to diagnose it is to add the llOwnerSay messages immediately before the if statement:

llOwnerSay("About to compare x:" + x + "; against y:" + y + ";";);
if (x == y)

Putting the llOwnerSay(hello) in other parts of the script isn't the right first step; that's an inefficient shotgun approach It could be the right second step, after you've done this. Until you've done this, you don't know if the problem is with x or y (or, in your case, message or hello).
Angus Balboa
Registered User
Join date: 19 Aug 2006
Posts: 14
11-05-2007 14:47
Ok I think I found the problem... I couldn't tell though until I had it say both the message and fname consecutively. Seems the space was being added into the variable also...

Thanks Kidd.
Talarus Luan
Ancient Archaean Dragon
Join date: 18 Mar 2006
Posts: 4,831
11-05-2007 14:53
From: Angus Balboa
Eh... might as well. It's not anything special anyway. This is an attempt to give the object a "name" (which works), and then have it say hello to the owner when the owner says the name. But that part doesn't work.


From: someone

string fname;

default
{

state_entry()
{
llListen(0,"",llGetOwner(),"";);
}


Well, this doesn't have anything to do with your problem, but channel 0 listens are a bad thing to use. Use a non-public channel (something > 0), and things are lovely. :)

From: someone

listen(integer channel, string name, key id, string message)
{

//checks for the rename command
if (llGetSubString(message, 0, 7) == "/frename";)
{
//renames object with whatever is typed after the command
fname = llGetSubString(message, 8, -1);
llOwnerSay(fname);
}


OK, so if I type "/frenameblahblahblah" it will set it to "blahblahblah". If I type "/frename blahblahblah" it will set it to " blahblahblah" (note the beginning space in the second one). You probably expect people to use the latter, which will result in a space at the beginning of the name. Use fname = llGetSubString(message, 9, -1); to fix that.

From: someone

if (message == fname)
{
llSay(0, "Hello " + (string) llKey2Name(llGetOwner()) + "!";);
}
}
}


OK, so, message is "/frename blahblahblah" and fname is " blahblahblah" at the beginning of this statement section. fname is the requested subset of message. Why would you ever expect them to be equal?

Is it possible you meant to do this? ->

CODE

if (llGetSubString(message, 0, 7) == "/frename")
{
//renames object with whatever is typed after the command
fname = llGetSubString(message, 8, -1);
llOwnerSay(fname);
} else // OK, it wasn't a rename command, check to see if the owner said my name
if (message == fname)
{
llSay(0, "Hello " + (string) llKey2Name(llGetOwner()) + "!");
}
}


Also, there's no need to typecast llKey2Name to a string; it already returns a string, and the redundant (string) cast generates useless code.
Ravanne Sullivan
Pole Dancer Extraordinair
Join date: 10 Dec 2005
Posts: 674
11-05-2007 14:59
The most likely problem from what you have posted is that fname has a space character in front of it due to the way you parse the /frename command.

fname = llGetSubString(message, 8, -1);

/frename New Name will give you " New Name" not "New Name"

try
fname = llStringTrim((llGetSubString(message, 8, -1),STRING_TRIM);
llSetObjectName(fname);

When you say the name in chat you can not preceed it with a space character. You also never rename the object.
_____________________
Ravanne's Dance Poles and Animations

Available at my Superstore and Showroom on Insula de Somni
http://slurl.com/secondlife/Insula de Somni/94/194/27/
Angus Balboa
Registered User
Join date: 19 Aug 2006
Posts: 14
11-05-2007 15:13
From: Talarus Luan
...but channel 0 listens are a bad thing to use. Use a non-public channel (something > 0), and things are lovely. :)


But since it's the avatar saying it, how would I use a non-public channel?
Tyken Hightower
Automagical
Join date: 15 Feb 2006
Posts: 472
11-05-2007 15:16
From: Angus Balboa
But since it's the avatar saying it, how would I use a non-public channel?

Type into your chat bar something like this: /1 blah blah blah

That will output "blah blah blah" on channel 1. You can't speak on negative channels without a script, though.
_____________________