LoneWolfiNTj Posted September 22, 2021 Share Posted September 22, 2021 I just got through doing an extreme editing job on a script I found in a full-perms security orb. It was buggy, and full of features I didn't need, and missing features I did need. So I fixed it. 🙂 Took several hours but I learned a lot, so it was worth it. In the process I came across several places involving code duplication in if/else statements. To give a simplified example: status_text() { string sta_str; if(status) {sta_str = "Status: On" ;} else {sta_str = "Status: Off";} llSetText(sta_str, <0.0,1.0,0.0>, 1.0); } Ok, that works, but I figured I'd get it down from 4 lines to 2 by using a ternary operator: text() { string sta_str = "Status: " + (status?"On":"Off"); llSetText(sta_str, <0.0,1.0,0.0>, 1.0); } But alas that gave "syntax error". No ternary ("a?b:c") operator in LSL, alas. Too bad, because many other languages (C, C++, Perl, etc) have it and it's very useful. So, is there any other similar feature? Or are with stuck with if / else? Link to comment Share on other sites More sharing options...
Fritigern Gothly Posted September 22, 2021 Share Posted September 22, 2021 You are indeed stuck with if/else. However, the original code can be made one line shorter: status_text() { string sta_str = "Status: Off"; if(status) sta_str = "Status: On" ; llSetText(sta_str, <0.0,1.0,0.0>, 1.0); } By assigning a default value to sta_str, the value will be retained when the the condition (status) is FALSE. You also usually don't need the accolades (curly brackets) when an if-statement contains only one function to be executed when the condition is TRUE, so I removed that too in the above example. 1 Link to comment Share on other sites More sharing options...
Rolig Loon Posted September 22, 2021 Share Posted September 22, 2021 16 minutes ago, Fritigern Gothly said: You also usually don't need the accolades (curly brackets) when an if-statement contains only one function to be executed when the condition is TRUE That's true, but it's a bad habit to get into. Always use the curly brackets to delineate the scope of a conditional block of code, even when they aren't technically necessary. That way, you'll be less likely to forget them when they are required (which is most of the time). 1 Link to comment Share on other sites More sharing options...
Mollymews Posted September 22, 2021 Share Posted September 22, 2021 1 hour ago, Fritigern Gothly said: status_text() { string sta_str = "Status: Off"; if(status) sta_str = "Status: On" ; llSetText(sta_str, <0.0,1.0,0.0>, 1.0); } By assigning a default value to sta_str, the value will be retained when the the condition (status) is FALSE.  this is the fastest way to do it. Is about 2 times faster than If Else and about 10 times faster than llList2String(["Status: Off", "Status: On"], status); 2 Link to comment Share on other sites More sharing options...
Quistess Alpha Posted September 22, 2021 Share Posted September 22, 2021 4 hours ago, Rolig Loon said: Always use the curly brackets to delineate the scope of a conditional block of code, even when they aren't technically necessary. I generally agree, but in my current mental formatting guidelines for myself(which I don't always follow. . .) , I'm allowing a single statement after an if without curly brackets if it fits on one line. // bad formatting: if(ID==llGetOwner()) return; // ok formatting: if(ID==llGetOwner()) return; Â 1 Link to comment Share on other sites More sharing options...
Coffee Pancake Posted September 23, 2021 Share Posted September 23, 2021 6 hours ago, Rolig Loon said: Always use the curly brackets to delineate the scope of a conditional block of code ^^ THIS ^^ Even for single line IF's 1 Link to comment Share on other sites More sharing options...
bobsknief Orsini Posted September 23, 2021 Share Posted September 23, 2021 string sta_str = "Status: Off"; if(status){sta_str = "Status: On";} Link to comment Share on other sites More sharing options...
Mollymews Posted September 23, 2021 Share Posted September 23, 2021 if there is an authority on LSL code formatting it would be the original Linden Scripting Language Guide 2003 as the originators wrote it up. from page 12  4.1. Conditional Statements The ’if’ statement operates and has the same syntax as the Java/C version. check_message(string message) { if(message == "open") { open(); } else if(message == "close") { close(); } else { llSay(0, "Unknown command: " + message); } } is not quite Java/C as there is no exact standard for Java or C. But LSL follows BSD Allman style fairly closely link to a pdf copy of LSL Guide 2003 is here: https://economicslab.tistory.com/attachment/cfile3.uf@1909190B49D79475010A44.pdf/   1 Link to comment Share on other sites More sharing options...
Wulfie Reanimator Posted September 23, 2021 Share Posted September 23, 2021 (edited) 16 hours ago, LoneWolfiNTj said: Ok, that works, but I figured I'd get it down from 4 lines to 2 by using a ternary operator: text() { string sta_str = "Status: " + (status?"On":"Off"); llSetText(sta_str, <0.0,1.0,0.0>, 1.0); } But alas that gave "syntax error". No ternary ("a?b:c") operator in LSL, alas. Too bad, because many other languages (C, C++, Perl, etc) have it and it's very useful. So, is there any other similar feature? Or are with stuck with if / else? There is no ternary operator in LSL, but if you want to avoid repetition, you could do some dumb list trickery. string sta_str = "Status: " + llList2String(["Off", "On"], status); Â Edited September 23, 2021 by Wulfie Reanimator 2 Link to comment Share on other sites More sharing options...
Quistess Alpha Posted September 23, 2021 Share Posted September 23, 2021 5 hours ago, Mollymews said: LSL Guide 2003 Wow, Interesting to see how much was already there in the beginning. I would have placed my bet that the vehicle functions were a latter addition. Link to comment Share on other sites More sharing options...
Mollymews Posted September 23, 2021 Share Posted September 23, 2021 3 hours ago, Quistess Alpha said: Wow, Interesting to see how much was already there in the beginning. I would have placed my bet that the vehicle functions were a latter addition. vehicles came toward the end of 2003. Cory, Ben and Andrew Linden made some example scripts at the time showing the vehicle functions. Like this one: Simple Airplane example dated 12-08-2003. https://www.outworldz.com/cgi/freescripts.plx?ID=757Â 1 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