switch/case

Moderator: Event DM

Post Reply
User avatar
TheBluDragon
Prince of Bloated Discourse
Posts: 279
Joined: Tue Jan 21, 2003 3:26 pm
Location: Bunbury, Western Australia (AWST: UTC+8 / GMT+8)

switch/case

Post by TheBluDragon » Wed Aug 27, 2003 2:28 pm

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'.
"Evil is a point of view. God kills indiscriminately and so shall we. For no creatures under God are as we are, none so like him as ourselves" - Lestat
User avatar
Neve
Prince of Bloated Discourse
Posts: 192
Joined: Mon Apr 14, 2003 4:09 pm
Location: The Netherlands
Contact:

Post by Neve » Wed Aug 27, 2003 2:40 pm

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();
- As you gaze unknowingly into the seemingly infinite depths of the optics of this altogether passionate embodiment of insatiability, you experience a gradual realisation that the heavily-embellished vocabulary scattered lavishly throughout the sentence you are currently reading is indisputably nothing greater than a generous ration of masculine bovine faeces.
User avatar
JollyOrc
Elder Sage
Posts: 3984
Joined: Fri Jan 17, 2003 11:41 am
Timezone: Europe, CE(S)T
Location: Germany
Contact:

Post by JollyOrc » Wed Aug 27, 2003 2:59 pm

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; 
}
?
User avatar
Neve
Prince of Bloated Discourse
Posts: 192
Joined: Mon Apr 14, 2003 4:09 pm
Location: The Netherlands
Contact:

Post by Neve » Wed Aug 27, 2003 3:14 pm

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 :P
- As you gaze unknowingly into the seemingly infinite depths of the optics of this altogether passionate embodiment of insatiability, you experience a gradual realisation that the heavily-embellished vocabulary scattered lavishly throughout the sentence you are currently reading is indisputably nothing greater than a generous ration of masculine bovine faeces.
User avatar
JollyOrc
Elder Sage
Posts: 3984
Joined: Fri Jan 17, 2003 11:41 am
Timezone: Europe, CE(S)T
Location: Germany
Contact:

Post by JollyOrc » Wed Aug 27, 2003 3:24 pm

Neve wrote: 2g.nss(5): ERROR: NON INTEGER EXPRESSION WHERE INTEGER REQUIRED :P
hmm.. so I'll probably end up with stacked if's. Damn.
User avatar
Lafferty
Scholar
Posts: 1129
Joined: Tue Feb 11, 2003 5:08 pm
Location: look at my hands... they are HUGE. And they cant touch themselves...
Contact:

Post by Lafferty » Wed Aug 27, 2003 3:39 pm

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 :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 :roll:
Actually
Sage
Posts: 1823
Joined: Sat Dec 14, 2002 10:11 pm
Location: Red Zone: Cuba

Post by Actually » Wed Aug 27, 2003 4:47 pm

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! :D

Bye Now,
Jerry Cornelius - Follow not the path of the null pointer, for therein lies damnation.
User avatar
Neve
Prince of Bloated Discourse
Posts: 192
Joined: Mon Apr 14, 2003 4:09 pm
Location: The Netherlands
Contact:

Post by Neve » Wed Aug 27, 2003 4:59 pm

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 :?
- As you gaze unknowingly into the seemingly infinite depths of the optics of this altogether passionate embodiment of insatiability, you experience a gradual realisation that the heavily-embellished vocabulary scattered lavishly throughout the sentence you are currently reading is indisputably nothing greater than a generous ration of masculine bovine faeces.
Actually
Sage
Posts: 1823
Joined: Sat Dec 14, 2002 10:11 pm
Location: Red Zone: Cuba

Post by Actually » Wed Aug 27, 2003 5:04 pm

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
User avatar
TheBluDragon
Prince of Bloated Discourse
Posts: 279
Joined: Tue Jan 21, 2003 3:26 pm
Location: Bunbury, Western Australia (AWST: UTC+8 / GMT+8)

Post by TheBluDragon » Thu Aug 28, 2003 4:15 am

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();
"Evil is a point of view. God kills indiscriminately and so shall we. For no creatures under God are as we are, none so like him as ourselves" - Lestat
Myk D'vor
Lord of Blithering Idiots
Posts: 111
Joined: Mon Feb 24, 2003 9:05 am

Post by Myk D'vor » Thu Aug 28, 2003 4:35 am

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
User avatar
TheBluDragon
Prince of Bloated Discourse
Posts: 279
Joined: Tue Jan 21, 2003 3:26 pm
Location: Bunbury, Western Australia (AWST: UTC+8 / GMT+8)

Post by TheBluDragon » Wed Dec 17, 2003 11:34 am

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.
"Evil is a point of view. God kills indiscriminately and so shall we. For no creatures under God are as we are, none so like him as ourselves" - Lestat
User avatar
JollyOrc
Elder Sage
Posts: 3984
Joined: Fri Jan 17, 2003 11:41 am
Timezone: Europe, CE(S)T
Location: Germany
Contact:

Post by JollyOrc » Wed Dec 17, 2003 12:31 pm

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.
User avatar
TheBluDragon
Prince of Bloated Discourse
Posts: 279
Joined: Tue Jan 21, 2003 3:26 pm
Location: Bunbury, Western Australia (AWST: UTC+8 / GMT+8)

Post by TheBluDragon » Wed Dec 17, 2003 1:16 pm

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.
"Evil is a point of view. God kills indiscriminately and so shall we. For no creatures under God are as we are, none so like him as ourselves" - Lestat
User avatar
j5hale3
Apprentice Scholar
Posts: 964
Joined: Tue Jan 14, 2003 3:23 am
Location: Central NJ {EST (GMT -5) and EDT (GMT -4)}

Post by j5hale3 » Wed Dec 17, 2003 4:44 pm

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.
Actually
Sage
Posts: 1823
Joined: Sat Dec 14, 2002 10:11 pm
Location: Red Zone: Cuba

Post by Actually » Wed Dec 17, 2003 5:17 pm

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
Everything I need to know in life, I learned from being an alcoholic.
User avatar
JollyOrc
Elder Sage
Posts: 3984
Joined: Fri Jan 17, 2003 11:41 am
Timezone: Europe, CE(S)T
Location: Germany
Contact:

Post by JollyOrc » Wed Dec 17, 2003 5:21 pm

it can.
User avatar
TheBluDragon
Prince of Bloated Discourse
Posts: 279
Joined: Tue Jan 21, 2003 3:26 pm
Location: Bunbury, Western Australia (AWST: UTC+8 / GMT+8)

Post by TheBluDragon » Thu Dec 18, 2003 9:39 am

Hmm, I tried that too, and had the same result. :evil:
Gonna give it another go tonight using a slightly different tack.
"Evil is a point of view. God kills indiscriminately and so shall we. For no creatures under God are as we are, none so like him as ourselves" - Lestat
User avatar
JollyOrc
Elder Sage
Posts: 3984
Joined: Fri Jan 17, 2003 11:41 am
Timezone: Europe, CE(S)T
Location: Germany
Contact:

Post by JollyOrc » Thu Dec 18, 2003 9:53 am

can you pm me the whole script as you use it ? I'll have a look then
User avatar
Praetor
Apprentice Scholar
Posts: 542
Joined: Sun Jan 05, 2003 5:07 am
Contact:

Post by Praetor » Thu Dec 18, 2003 10:10 am

Instead of calling the function GetPCSpeaker() a billion times, use 1 function call and assign the result to a variable.
Post Reply