Jump to content
Exorcyst

Short but important scripting question!

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!

Share this post


Link to post
Share on other sites

make sure you define the variable as a float somewhere...

float cat;

if(channel == 0)

{ cat = 1.0; }

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Hey guys, thanks for the reply.

I defined the float at the start of the script (even before 'default')

It was a mistake somewhere else in the script. Thanks!

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Just a matter of taste. Think of the definition: The statement after the "if clause" is executed if the clause is true AND this statement can be a simple statement or a compound statement. Where a compound statement is a compound of statements inside curly brackets

:o

  • Like 1

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...