Call of Duty 5: World at War => Help Desk => Scripting => Topic started by: Ege115 on May 11, 2014, 09:16:33 am
Title: case statement
Post by: Ege115 on May 11, 2014, 09:16:33 am
I am just playing around with the "case" statement. I have not used this statement before so this is my first time. And I found this statement really useful. :D
Anyway, I am using this script which has a case statement. But when I am loading my map, I get a script compile error saying, "illegal case statement". What did I do wrong with the case statement?
Thanks in advance.
Title: Re: case statement
Post by: YaPh1l on May 11, 2014, 09:52:01 am
case statements are only allowed within a switch block.
- Phil.
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 10:17:14 am
case statements are only allowed within a switch block.
- Phil.
Uhm, a what? So what exactly is it I need to change at the case statement? :o
Title: Re: case statement
Post by: daedra descent on May 11, 2014, 02:05:36 pm
Like this:
EDIT: Really i'd just suggest an if statement instead of a case statement for what your trying to do. Case statements are primarily used as a way to return values, not do lengthy code.
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 03:38:40 pm
EDIT: Really i'd just suggest an if statement instead of a case statement for what your trying to do. Case statements are primarily used as a way to return values, not do lengthy code.
Okey thank you, that seems to have fixed it. Now an another error pops up. The script compile error is saying, "missing case statement" And developer 1 says that it is something regarding those first three lines you often have in scripts. Is it an another file I need to include there? If so which one?
Title: Re: case statement
Post by: PROxFTW on May 11, 2014, 04:43:31 pm
No, nothing wrong with that more than likely. But make sure you add a default case statement. Most likely why that is happening is there is a value being returned that is not listed down.
Title: Re: case statement
Post by: n123q45 on May 11, 2014, 05:47:13 pm
this is how a case statement is setup this is used instead of lots of if else statements. you use this when the variable is set before this script. I used undefined to show that it has to be set before.
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 06:01:40 pm
No, nothing wrong with that more than likely. But make sure you add a default case statement. Most likely why that is happening is there is a value being returned that is not listed down.
Hmm, well I still don't know exactly what I need to do, what value? :o I'm a noob.
Title: Re: case statement
Post by: YaPh1l on May 11, 2014, 06:05:35 pm
The value of the variable you "switch over". Although that shouldn't be causing any problems, it just wouldn't execute any case at all.
- Phil.
Title: Re: case statement
Post by: Ege115 on May 11, 2014, 06:17:51 pm
Well, then I have no idea what's wrong. I don't know how I should correct the script myself. D: I can't see anything wrong with it.
Title: Re: case statement
Post by: mrpeanut188 on May 11, 2014, 06:22:00 pm
Well, then I have no idea what's wrong. I don't know how I should correct the script myself. D: I can't see anything wrong with it.
Like said, it 'switches' to the value returned and executes the code. Let's say you have the case for 'm9' and 'thompson'. If you carry any other gun, presumably it'd pop out an error or do nothing. If it IS popping out an error, you have to add a default case so that if it isn't one of the listed guns it's execute the code in the default case.
Like said, it 'switches' to the value returned and executes the code. Let's say you have the case for 'm9' and 'thompson'. If you carry any other gun, presumably it'd pop out an error or do nothing. If it IS popping out an error, you have to add a default case so that if it isn't one of the listed guns it's execute the code in the default case.
Shouldn't it be because it switches to the m9 inside of the function itself?
Hmm, that can't be the issue as this script works in other cases. This time I am making it with the case statement, but it just doesn't takes the weapon.
Title: Re: case statement
Post by: PROxFTW on May 11, 2014, 07:25:57 pm
Test this
Title: Re: case statement
Post by: jjbradman on May 11, 2014, 10:13:27 pm
dont pass the string "m9" to the other funtion. instead define it inside the other funtion. i got problems with switch in that case where all the values inside the switch block would get executed
Title: Re: case statement
Post by: treminaor on May 12, 2014, 12:21:36 am
That would be because you didn't place break statements at the end of each case.
More case statements is exactly what I am doing. this is the complete whole script if you want to see that. And I tried your script PROxFTW, but it didn't take the weapon either. :/ Isn't it wierd that it just WONT work even if the script says that it will take the weapon the player has? :/
Title: Re: case statement
Post by: daedra descent on May 12, 2014, 05:16:59 am
Because your not using the case statement right. it should be something like this:
again, i really suggest just using an if statement for all of this.
EDIT: Thought i'd make a little example to show why & the difference between the switch and if statements.
The cases only act as a separate if statement for each possibility. The only difference(i think) is that the switch function's different cases can apply to different entities at the same time while if statements can't.
Title: Re: case statement
Post by: treminaor on May 12, 2014, 05:39:00 am
More case statements is exactly what I am doing. this is the complete whole script if you want to see that. And I tried your script PROxFTW, but it didn't take the weapon either. :/
Isn't it wierd that it just WONT work even if the script says that it will take the weapon the player has? :/
The point of the switch case is that there are multiple outcomes based on the value of the variable you are switching, so I combined all of your cases into one switch statement.
Title: Re: case statement
Post by: Ege115 on May 12, 2014, 04:01:17 pm
Oh, thank you so much it worked! I saw what I did wrong now, lol. Well, now I have learned how "case" works, now what's next I need to learn in scripting? :D
Thank you all of you, I give you all each +1. ;)
Title: Re: case statement
Post by: n123q45 on May 12, 2014, 07:04:58 pm