Solved Issue with Switch() { case: }

Question that is answered or resolved.

Bruce

Active member
Hello everyone,

First of all, I know this code works:

Code:
    switch(AttackType){
        case 1:
            return
            break;
        case 2:
            return
            break;
        default :
            return
            break;  
    }

But

This code is giving me an error:
Code:
    switch(AttackType){
        case openborconstant("ATK_NORMAL11"):
            return setspawnentry("name", "Burn1");
            break;
        case openborconstant("ATK_NORMAL12"):
            return setspawnentry("name", "Shock1");
            break;
        default :
            return setspawnentry("name", "");
            break;  
    }

The error message was the case requires an integer or string.
Isn't openborconstant the inetger or string?
How can I set this up with openborconstant?
I know I can do it with if-else statements, but I wanted to set it up with case statements.

Thank you
 
You can't. Openborconstant() is preprocessed whenever possible, but it is still a function, and you can never use functions in case statements. You must use if/else.

DC
Won't be possible to do something like this?

C-like:
char atType1 = openborconstant("ATK_NORMAL11"):
char atType2 = openborconstant("ATK_NORMAL12"):

  switch(AttackType){
        case atType1:
            return setspawnentry("name", "Burn1");
            break;
        case atType2:
            return setspawnentry("name", "Shock1");
            break;
        default :
            return setspawnentry("name", "");
            break; 
    }

but yeah, for only two options, I think IF/ELSE would do the job.
 
Won't be possible to do something like this?

C-like:
char atType1 = openborconstant("ATK_NORMAL11"):
char atType2 = openborconstant("ATK_NORMAL12"):

  switch(AttackType){
        case atType1:
            return setspawnentry("name", "Burn1");
            break;
        case atType2:
            return setspawnentry("name", "Shock1");
            break;
        default :
            return setspawnentry("name", "");
            break;
    }

but yeah, for only two options, I think IF/ELSE would do the job.

No, you can't do that either. Note this is not an OpenBOR caveat. It's from C in general (and for that matter, most other languages too). As a general rule, switch statements have to be hard coded values known at compile time.

So you could #define a constant like #define MY_CONSTANT 2 and it will work. However, openborconstant() will not, because it is not technically a constant. It's a function that gets constant values from the engine. Some of those are runtime constants that don't have values in code. The engine has to define them as a game boots up (ex. Follow5+ or attack types 11+).

HTH,
DC
 
Won't be possible to do something like this?

C-like:
char atType1 = openborconstant("ATK_NORMAL11"):
char atType2 = openborconstant("ATK_NORMAL12"):

  switch(AttackType){
        case atType1:
            return setspawnentry("name", "Burn1");
            break;
        case atType2:
            return setspawnentry("name", "Shock1");
            break;
        default :
            return setspawnentry("name", "");
            break;
    }

but yeah, for only two options, I think IF/ELSE would do the job.
lol I have tried this before, and it didn't work because openborconstant is a constant (function) like DCurrent said.
Yes, if/else statements work. Case statements look nicer and neater to me, that's why I decided to try it.
 
Back
Top Bottom