Page 1 of 1
switch/case
Posted: Wed Aug 27, 2003 2:28 pm
by TheBluDragon
I am trying to use a switch/case statement.
ok, I know it exists, can someone point me to a working example so I can use it.
I s'pose the time of night I am trying to get this working dosent help.
As another idea, can anyone tell me how I can have an if statement with multiple conditions? ie, the PC has item #1, OR item #2 OR item #3 (or any combination), then it executes the code. I am planning to use the switch/case to do this, but there must be a simpler way using 'if'.
Posted: Wed Aug 27, 2003 2:40 pm
by Neve
Code: Select all
switch(d3()){
case 1 : <blablablabla>;
break;
case 2 : <blablablabla>;
break;
case 3 : <blablablabla>;
break;
}
If you use the "Random" function, you should start with "case 0"
About testing for either this or that item, you should use the pipes : ||
Code: Select all
if(GetIsDay() || GetIsDawn()) DoSomething();
else DoSomethingElse();
Posted: Wed Aug 27, 2003 2:59 pm
by JollyOrc
am I remembering it right when I say that switch - case only checks integers ? So no
Code: Select all
switch (name) {
case "John" : <blablablabla>;
break;
case "joe": <blablablabla>;
break;
}
?
Posted: Wed Aug 27, 2003 3:14 pm
by Neve
JollyOrc wrote:am I remembering it right when I say that switch - case only checks integers ? So no
Code: Select all
switch (name) {
case "John" : <blablablabla>;
break;
case "joe": <blablablabla>;
break;
}
?
2g.nss(5): ERROR: NON INTEGER EXPRESSION WHERE INTEGER REQUIRED

Posted: Wed Aug 27, 2003 3:24 pm
by JollyOrc
Neve wrote:
2g.nss(5): ERROR: NON INTEGER EXPRESSION WHERE INTEGER REQUIRED

hmm.. so I'll probably end up with stacked if's. Damn.
Posted: Wed Aug 27, 2003 3:39 pm
by Lafferty
Just convert your values to something integer in the case of a string make it:
"John" -> (J = 074;o = 111; h = 104; n = 110) -> 074111104110
LOL

*slaps head* what a chaotic concept and make the keyword a bit longer and thats it
ok this is not so good... maybe store your Strings in an array/hash and get the position of an entry and compare to that?
ok i should go home and eat someting instead of drinking coffee all day and posting crap

Posted: Wed Aug 27, 2003 4:47 pm
by Actually
Will NWNScript take an array, and pointers? I didn't think it had that option.
@Neve - "For random, start case w/ 0"... D'OH! Damn, now I gotta go back and clean that script up! Thanks for the head's up!
Bye Now,
Jerry Cornelius - Follow not the path of the null pointer, for therein lies damnation.
Posted: Wed Aug 27, 2003 4:59 pm
by Neve
Actually,
You could also change your Random function like this :
Instead of
Random(37), which generates values from 0 - 37
Random(36) + 1, which generates values from 1 - 37
As for arrays, you could try to make a linked list instead, with a '%%' or something to divide the strings/integers you'd like to save

Posted: Wed Aug 27, 2003 5:04 pm
by Actually
Ooh, yeah. That's easier than re-numbering, even for the little "1 of d6 random effects for throwing a coin in the wishing well" script I'm writing. Thanks!
Bye Now,
Jerry Cornelius - include BEER.H
Posted: Thu Aug 28, 2003 4:15 am
by TheBluDragon
Neve wrote:Code: Select all
switch(d3()){
case 1 : <blablablabla>;
break;
case 2 : <blablablabla>;
break;
case 3 : <blablablabla>;
break;
}
If you use the "Random" function, you should start with "case 0"
About testing for either this or that item, you should use the pipes : ||
Code: Select all
if(GetIsDay() || GetIsDawn()) DoSomething();
else DoSomethingElse();
Ok, cool. Thanks for that.
Also, to extend this a little, would the double ampersand(sp?) && instead of the pipes || make it AND instead of OR ?? Compiles fine, but havent had a chance to test it out yet. If not what should it be?
Code: Select all
if(GetIsDay() && GetIsCloudy()) DoSomething();
else DoSomethingElse();
Posted: Thu Aug 28, 2003 4:35 am
by Myk D'vor
Correct, && is the symbol for logical AND. ! is the symbol for logical NOT, and || is the symbol for logical OR. The correct way to form up a switch/case type structure for non-integer options (like strings) is like this:
Code: Select all
if(string=="blah") {
SpeakString("it was blah");
} else if (string=="foo") {
SpeakString("it was foo");
} else if (string=="bar") {
SpeakString("it was bar");
} else if (string=="foobar") {
SpeakString("it was foobar");
}
You'll notice it's VERY similar to the switch organization and it works very well. Just make sure you're testing the same string against multiple values, or the flow of the ifs can end up missing an if you were counting on hitting
Myk D'Vor
Posted: Wed Dec 17, 2003 11:34 am
by TheBluDragon
I still cant seem to get this to work correctly, and I need help badly.
I am trying to get it to return true if the PC is carrying any 1 of 5 different items.
Code: Select all
if(!HasItem(GetPCSpeaker(), "Item1")) {
return TRUE;
} else if(!HasItem(GetPCSpeaker(), "Item2")){
return TRUE;
} else if(!HasItem(GetPCSpeaker(), "Item3")){
return TRUE;
} else if(!HasItem(GetPCSpeaker(), "Item4")){
return TRUE;
} else if(!HasItem(GetPCSpeaker(), "Item5")){
return TRUE;
}
return FALSE;
What it currently does is tests that PC has ALL the items, which makes no sense to me, surely "else" means "if this then ...
else this" ie. if not the first one, then the secone one ...
The effect it will have, is that if they are carrying one of these items, it will start 1 conversation tree, if they have none of them, it will be something different.
Works fine if I do it with only 1 item, but I need the 5 different items for other parts of the script. I have tried it without the "else" and without the "{}" so I am pretty much stuck.
Posted: Wed Dec 17, 2003 12:31 pm
by JollyOrc
Code: Select all
if (!HasItem(GetPCSpeaker(), "Item1"))
return TRUE;
if (!HasItem(GetPCSpeaker(), "Item2"))
return TRUE;
if (!HasItem(GetPCSpeaker(), "Item3"))
return TRUE;
if (!HasItem(GetPCSpeaker(), "Item4"))
return TRUE;
if (!HasItem(GetPCSpeaker(), "Item5"))
return TRUE;
return FALSE;
should work.
If the items are named as that a loop might be more easily readable, and can handle way more than 5 items.
Posted: Wed Dec 17, 2003 1:16 pm
by TheBluDragon
These are not the actual Item names I am using, but suffice to say, the actual names are unique, and readable.
The effect this has is that the PC must have all the items for it to read true, which is not what I want. I want it to read true if the PC has any of the items.
Posted: Wed Dec 17, 2003 4:44 pm
by j5hale3
On the lines that return true, you will need a structure to break out of the loop. sort of like a gosub, then return just after the loop.
Posted: Wed Dec 17, 2003 5:17 pm
by Actually
It'd be ugly as hell, but couldn't you just do:
Code: Select all
if ((!HasItem(GetPCSpeaker(), "Item1"))) || ((!HasItem(GetPCSpeaker(), "Item2"))) || ((!HasItem(GetPCSpeaker(), "Item3"))) || ((!HasItem(GetPCSpeaker(), "Item4"))) || ((!HasItem(GetPCSpeaker(), "Item5")))
return TRUE;
Can NWScript handle a multiple OR statement like that?
Bye Now,
Jerry Cornelius - include BEER.H
Posted: Wed Dec 17, 2003 5:21 pm
by JollyOrc
it can.
Posted: Thu Dec 18, 2003 9:39 am
by TheBluDragon
Hmm, I tried that too, and had the same result.
Gonna give it another go tonight using a slightly different tack.
Posted: Thu Dec 18, 2003 9:53 am
by JollyOrc
can you pm me the whole script as you use it ? I'll have a look then
Posted: Thu Dec 18, 2003 10:10 am
by Praetor
Instead of calling the function GetPCSpeaker() a billion times, use 1 function call and assign the result to a variable.