Jump to content

Short but important scripting question!


Exorcyst
 Share

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

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

Recommended Posts

Hi everyone! 

I just started learning how to script in LSL and I've got a little script now by using the web and the wiki.

However, there's a tiny thing that I can't find: I want to set a float variable (let's say its called "cat" from 0.0 to 1.0 by using an "if" command). so in my noobbrain it would look like: 

if(channel == 0)

{ cat = 1) }

However, this doesn't work. How would I make this work properly?

 

Thank you very much for your time!

Link to comment
Share on other sites

Take a look at http://wiki.secondlife.com/wiki/Category:LSL_Float

When you define a variable, you have to define its type explicitly, and the variable is only valid within the scope where it is defined.  So if you define it within that if, it won't even exist outside the {brackets}.  You probably ought to read this one too >>> http://wiki.secondlife.com/wiki/LSL_Variables

  • Like 1
Link to comment
Share on other sites

ot strictly relevant to the question, but since you say you're just starting with LSL, please allow me to suggest that you get into the habit of using {curly brackets} to surround anything following an if() clause, even if you don't -- as is the case here -- actually need them.

This is because, if the when the if clause isn't followed by any curly brackets, LSL interprets that as meaning the following line is governed by the if clause, but assumes anything following it should be executed anyway..  So, 

	listen(integer channel, string name, key id, string message)
	{
		if("red" == message)
			llSetColor(<1.0,0.0,0.0>,ALL_SIDES);

	}

 

will work as expected -- that is, the object will turn red if it receives the message "red";

However, if you try adding anything else to the code, you may rapidly observer unexpected results, since LSL interprets

if("red" == message)
	llOwnerSay("received "+message;
	llSetColor(<1.0,0.0,0.0>,ALL_SIDES);

as meaning the object should execute the llOwnerSay only if the message is "red" but should then go on to change the colour regardless of what the message is, which probably isn't what you want.

If you use, though 

if("red" == message){
	llOwnerSay("received "+message;
	llSetColor(<1.0,0.0,0.0>,ALL_SIDES);
}

there's no possibility of any confusion.

It's up to you, of course, but I certainly found that, before I formed the habit of always using brackets and braces even when, strictly speaking, I didn't need to, I frequently got horribly confused, particularly when things weren't working properly and I started putting in lots of llOwnerSay commands to help me debug the script.

 

 

  • Like 2
Link to comment
Share on other sites

I agree it's a matter of taste.   All I'm saying is that I certainly got into all sorts of messes when I was a beginner because I didn't bracket things after"if clauses",    This was particularly the case because if you don't always use brackets, then simply putting in "llOwnerSay" to debug something that's not working right will, far from helping, often simply add to the confusion.

Certainly when I started to script I found that, along with mistyping if (a = b) instead of if (a == b) (so easy to do, and so hard to spot when you're tired), not using brackets was my number one problem with conditional statements going wrong.

  • Like 2
Link to comment
Share on other sites

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