Exorcyst Posted March 17, 2017 Share Posted March 17, 2017 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 More sharing options...
Xiija Posted March 17, 2017 Share Posted March 17, 2017 make sure you define the variable as a float somewhere... float cat; if(channel == 0) { cat = 1.0; } 1 Link to comment Share on other sites More sharing options...
Rolig Loon Posted March 17, 2017 Share Posted March 17, 2017 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 1 Link to comment Share on other sites More sharing options...
Exorcyst Posted March 17, 2017 Author Share Posted March 17, 2017 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! 1 Link to comment Share on other sites More sharing options...
Innula Zenovka Posted March 17, 2017 Share Posted March 17, 2017 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. 2 Link to comment Share on other sites More sharing options...
Dora Gustafson Posted March 18, 2017 Share Posted March 18, 2017 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 1 Link to comment Share on other sites More sharing options...
Innula Zenovka Posted March 18, 2017 Share Posted March 18, 2017 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. 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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