Multiple IF statements?
|
Piggie Paule
Registered User
Join date: 22 Jul 2008
Posts: 675
|
08-25-2009 12:51
What's wrong here? adding 1 to the value of "col" if this goes over 15 it's set back to 1 and according to what value it is, it sets the values of r g and b. Obviously I'm making some fundamental mistake  if( str=="colour" ) { col += 1; if(col > 15) { col = 1; } if(col = 1) r = 0; g = 0; b = 0; if(col = 2) r = 0; g = 0; b = 1; if(col = 3) r = 1; g = 0; b = 0; if(col = 4) r = 1; g = 0; b = 1;
|
EF Klaar
Registered User
Join date: 11 Jun 2007
Posts: 330
|
08-25-2009 12:55
From: Piggie Paule if(col = 1) r = 0; g = 0; b = 0; You are using the assignment operator "=" in the test; you should be using the logical comparison operator "==", and possibly you want to enclose the other assignments inside braces so that they too execute conditonally: if (col == 1) {r = 0; g = 0; b = 0;}
|
EF Klaar
Registered User
Join date: 11 Jun 2007
Posts: 330
|
08-25-2009 13:01
Is this a good place to introduce you to colours as vectors?
vector red = <1.0, 0.0, 0.0>; llSetColor (red, ALL_SIDES);
And it also looks like using a list of colours and cycling through the index would help in what you're doing. But that is another can of worms, which you may prefer to avoid for the time being.
|
Rolig Loon
Not as dumb as I look
Join date: 22 Mar 2007
Posts: 2,482
|
08-25-2009 13:58
Curly brackets enclose bodies of code that are meant to be executed as a block. The block may define an event, for example, or a subset of an event, like a condition test. So when you write an if test, the statements to be executed if the test condition is TRUE must be in curly brackets.... if (col ==1) //If this test evaluates as TRUE { //then do everything from here ... r = 0; g = 0; b = 0; } // ...to here.
Omitting the brackets means ... if (col ==1) //If this test evaluates as TRUE r = 0;//... do this... g = 0; // .. and then do this and anything after it, even if (col ==1) is FALSE b = 0;
To be sure that you don't fall into this trap, always use the brackets even if you only have one statement to execute.
_____________________
It's hard to tell gender from names around here but if you care, Rolig = she. And I exist only in SL, so don't ask....  Look for my work in XStreetSL at 
|
Piggie Paule
Registered User
Join date: 22 Jul 2008
Posts: 675
|
08-25-2009 14:45
Thanks to you all for your help, and putting up with my very basic questions. I'm at the base of the cliff, looking up and I'm about 1 or 2 feet of the ground at the moment, making progress, but strugging to find a good foot hold quite often  It was the == rather then the = that was the biggest issue. I had originally got them rgb on seperate lines within the curley brackets before Wiki showed me I could have it on 1 line. I've also enclosed them in brackets (on the 1 line) as per your example. There are still many terms people use here, which I'm not understanding, but I like to work backwards in a way, get something up and running, then often re work it, and kinda work backwards, later on learning things I should have done from the start. Not a good way to start, but I like to get some "Results" 1st and something working, as it gives me satisfactions and encouragement, even if it's not done the right way. I can always re-do things better later when I have more understanding 
|
Void Singer
Int vSelf = Sing(void);
Join date: 24 Sep 2005
Posts: 6,973
|
08-25-2009 16:31
vector color; //--aka col = 1 if (col == 2) color.z = 1.0; else if (col == 3) color.x = 1.0; else if (col == 4) color = < 1.0, 0.0, 1.0 >; else lets you exit early, if you have 4 or more tests a binary tree is probably a better structure for lest amount of test for for ALL values (assuming an equal chance of any color) example: if (col & 4){ if (col & 2){ if (col & 1){ //-- col = 7 }else{ //-- clo = 6 } }else{ if (col & 1){ //-- col = 5 }else{ //-- col = 4 } }else{ if (col & 2){ if (col & 1){ //-- col = 3 }else{ //-- col = 2 } }else{ if (col & 1){ //-- col = 1 }else{ //-- col = 0 } } }
(technically you can collapse any "if" into a preceding "}else{" but it shouldn't matter if you do the full range and the compiler should be doing it for you in most cases... I think this is more readable in most case)
_____________________
| | . "Cat-Like Typing Detected" | . This post may contain errors in logic, spelling, and | . grammar known to the SL populace to cause confusion | | - Please Use PHP tags when posting scripts/code, Thanks. | - Can't See PHP or URL Tags Correctly? Check Out This Link... | - 
|
Strife Onizuka
Moonchild
Join date: 3 Mar 2004
Posts: 5,887
|
08-27-2009 05:01
list colors = [<1,0,0>]; //plus a bunch more...
integer len = llGetListLength(colors); if((col > len) || (col < 0)) col = 0;
vector rgb = llList2Vector(colors, col);
_____________________
Truth is a river that is always splitting up into arms that reunite. Islanded between the arms, the inhabitants argue for a lifetime as to which is the main river. - Cyril Connolly
Without the political will to find common ground, the continual friction of tactic and counter tactic, only creates suspicion and hatred and vengeance, and perpetuates the cycle of violence. - James Nachtwey
|