GameMaker: Studio

GameMaker: Studio

查看统计:
twiggle 2014 年 5 月 14 日 下午 12:45
Why cant you use an expression on a switch statement?
Hi, im a super noob at this and I keep getting this error: case argument should be a constant.

Thats fine, however I looked in the reference and it said that it can be an expression...

So if you cant use an expression on a switch statement case, how do you overcome this problem? Do you just have to do a bunch of if-statements instead?
thanks.
< >
正在显示第 1 - 6 条,共 6 条留言
Keys 2014 年 5 月 14 日 下午 1:57 
Show us the code you used! \ o/
Sera 2014 年 5 月 14 日 下午 2:08 
Switch statements are mostly for checking several cases for one value. This arises from a pretty common case where you'd end up with something like this:

if RoomNumber = 1 then { }else if RoomNumber = 2 then { }else if RoomNumber = 3 or RoomNumber = 4 then { } etc...

This gets super unweildly, so some genius came up with switch, wherein you get this:

switch RoomNumber { case 1: break; case 2: break; case 3: case 4: break; }

Basically, if everything boils down to one outcome that then branches off a bunch of different ways, switch is your boy. That said, GM does seem fine with switch's condition being a statement, so long as that statement has a defined outcome it can refer to:

switch round(x/y) { }

That said, conditionals like "or" and "and" probably won't work here, because they're only valid when looking for a true or false result, which is where if stamants work out; if statements are strictly boolean, working in "true" or "false" as a whole, while switch is all about basing a decision on a single variable value or result.
twiggle 2014 年 5 月 14 日 下午 6:04 
thanks for the response!
I think i am starting to understand.

this was my code I was trying to run. (i no longer need it, but I just want to make sure I understand it)

switch (keyboard_key) { case vk_left: case (x > 40): x -= 4; break; case 3, 4... }

and it was saying the case # 2 had to be a constant.

would this work instead?

switch (keyboard_key) { case vk_left: case max(x, 40): case x: x -= 4; break; case (40): break; }

or am I still not understanding something? it's still really fuzzy in my mind right now, but im sure ill catch on sooner or later.
Sera 2014 年 5 月 14 日 下午 9:36 
As a general rule, I'd avoid expressions in "case" situations unless the situation absolutely calls for it, but your problem in the second one is that you can't nest cases; what you're doing there is wanting another switch.

switch (keyboard_key) { case vk_left: switch max(x, 40) { case x: x -= 4; break; case (40): break; //honestly I'd just use an if statement if it's between two things like this, mind. } break; }

When you put "case" statements in sequence, what you're really getting is this:

switch floor(random(10)) { case 0: //only happens if zero. break; case 1: case 2: //only happens if 1 or 2. break; case 3: case 5: //happens if 3 or 5. case 6: case 9: //happens if 3, 5, 6 or 9 because we never commited a break break; default: //happens if nothing else did, in this case meaning 4, 7, 8, or an error break; }

Because cases behave that way, any attempt to nest situations ends up actually meaning "if any one of these is the case." Your example would execute x-=4 if the left key was down, the unicode for any pressed key was the same as x, or x was less than 40 and the unicode for a pressed key was 40 as well. Converting that second case to a new switch statement or a quick if branch will work fine (you can logic within logic all you want), but cases just don't work that way.
twiggle 2014 年 5 月 15 日 上午 7:18 
aha! now I understand. For my actual code I ended up using 2 if statements, I was just asking because I wanted a clearer knowledge of it.

So basically the switch statement keeps going after the first "true" value, even if the value wasnt the same as the next? like, in your example, if the random number was 5, it would execute case 6 & 9 (if they didnt have breaks in them) even if the random number was 5?
Ok, that question was a little confusing, I will just do an example of what i'm asking:
switch floor(random(10)) { case 0: //only happens if zero. break; case 1: case 2: //only happens if 1 or 2. break; case 3: case 5: instance_create(100, 100, obj_object1); case 6: instance_create(200, 200, obj_object2); case 9: instance_create(450, 450, obj_object3); break; default: //happens if nothing else did, in this case meaning 4, 7, 8, or an error break; }

So in the code above, if the random number happened to be 5, it would create an instance of object 1, then continue onto case 6, create instance of object 2, then continue to case 9 and create object 3 before the switch statement is over?
Thanks
Sera 2014 年 5 月 15 日 上午 8:52 
That's how it works, yeah. :)
< >
正在显示第 1 - 6 条,共 6 条留言
每页显示数: 1530 50

发帖日期: 2014 年 5 月 14 日 下午 12:45
回复数: 6