flow control...
|
Aaron Levy
Medicated Lately?
Join date: 3 Jun 2004
Posts: 2,147
|
07-12-2004 04:29
I'm starting to get into some complex flow controls, and need help with a concept.
How do I combine a bunch of flows into one statement?
For example, the following numbers are red on a roulette wheel: 21 18 12 25 27 1 ... (not going to list them all)
What would the if statement look like whose logic is "If SpotBallStoppedIn is 21 or 18 or 12 or 25 or 27 or 1, then SpotColor = red"
I have done this with the standard if/else statements, but I think there's a shorter way of doing this.
|
Aaron Levy
Medicated Lately?
Join date: 3 Jun 2004
Posts: 2,147
|
07-12-2004 04:42
I think it has something to do with the boolean || (OR) but I can't get it to work.
|
Moopf Murray
Moopfmerising
Join date: 7 Jan 2004
Posts: 2,448
|
07-12-2004 05:04
There are a few ways to do this. The long-handed way would be: if (SpotBallStoppedIn == 21 || SpotBallStoppedIn == 18 || SpotBallStoppedIn == 12 || SpotBallStoppedIn == 25) { }
You could also do the following: list checkValues = [21,18,12,25,27,1]; if (llListFindList(checkValues, [SpotBallStoppedIn]) != -1) { }
(In this version all you have to do is change the checkValues list to change what to check for - the llListFindList function returns -1 if it's not found in the list btw) And I'm sure others will have other ways of doing this.
|
Cray Levy
Member
Join date: 7 Jul 2004
Posts: 33
|
07-12-2004 05:04
Educated guess: use if(a == b || a == c) instead of if(a == b || c)
I'd imagine that searching a list or a string might be faster with many alternatives though, since expressions aren't atomic.
|
CoCoNoNo Anubis
Skylark Mechanic
Join date: 28 Jun 2004
Posts: 40
|
07-12-2004 13:45
((a == b) || (a == c)) would be a more semantically correct way to phrase it. (a == b || a == c) might confuse the compiler, not to mention another programmer.
_____________________
-CoCoNoNo Current Major Projects: CoCoHoBo - Personal conveyance not for the faint of heart. Dreamer Class Skyships - Personalized skycraft for small parties or gatherings.
|
Alondria LeFay
Registered User
Join date: 2 May 2003
Posts: 725
|
07-13-2004 00:00
Actually, I would recommend, as strange as it may sound, comparing it in string form, similar to the list example but instead like: string checkValues = "21,18,12,25,27,1"; if (llSubStringIndex(checkValues, (string)SpotBallStoppedIn) != -1) { }
Not only does that save 106 bytes of script space (or more, with the full list), I tested the above versus the similar list version by looping them 200 times a piece and consistently the string method was about 3 seconds quicker per 200 cycles. While I did not test it, I am sure they both are quicker than the long if...
|
Moopf Murray
Moopfmerising
Join date: 7 Jan 2004
Posts: 2,448
|
07-13-2004 00:10
From: someone Originally posted by Alondria LeFay Actually, I would recommend, as strange as it may sound, comparing it in string form, similar to the list example but instead like:
string checkValues = "21,18,12,25,27,1"; if (llSubStringIndex(checkValues, (string)SpotBallStoppedIn) != -1) { }
Not only does that save 106 bytes of script space (or more, with the full list), I tested the above versus the similar list version by looping them 200 times a piece and consistently the string method was about 3 seconds quicker per 200 cycles.
While I did not test it, I am sure they both are quicker than the long if... Alondria, your example would not work in all circumstances. For instance, checking for a value of 1 would also match 21, 11 as it's simply checking for any instance of the value being searched for. Including a seperator before and after each value would avoid this: string checkValues = ",21,18,12,25,27,1,"; if (llSubStringIndex(checkValues, ","+(string)SpotBallStoppedIn+",") != -1) { }
In this example, searching for 1 actually searches for ,1, so ,21, or ,11, cannot also give a false match.
|
Alondria LeFay
Registered User
Join date: 2 May 2003
Posts: 725
|
07-13-2004 00:19
Doh.. Note to self: Don't try to post stuff after a 16 hour day at world. You are so correct on my oversight.  I'll have to retest the speed differences with the addition of seperators.. maybe after I sleep though..
|
Moopf Murray
Moopfmerising
Join date: 7 Jan 2004
Posts: 2,448
|
07-13-2004 00:25
From: someone Originally posted by Alondria LeFay Doh.. Note to self: Don't try to post stuff after a 16 hour day at world.
You are so correct on my oversight.  I'll have to retest the speed differences with the addition of seperators.. maybe after I sleep though.. Alondria, I know that feeling all too well! I'd be suprised if it still wasn't faster than using a list, though, as lists do seem to be slow in LSL. I just went for the example of least resistance in my original reply 
|
Cray Levy
Member
Join date: 7 Jul 2004
Posts: 33
|
07-13-2004 03:08
There probably isn't going to be a measurable speed difference. That's why I suggested a string. Searching a string is very trivial and *probably* atomic because of that, given their length being limited and all. Can't say much on lists without further details, but in general a list should at least roughly double the effort (and memory) involved, and that's for very basic lists, not lists consisting of dynamically typed values.
|