Frictional Games Forum (read-only)

Full Version: Switch statements with string case values
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey guys, I recently posted in a thread that you could use strings in a case value for switch functions. I was wrong! I have begun to prefer switch functions because they're easy to read and easy to tweak.

Anyways, here's what I thought would work:

Code:
AddTimer("timerone", 1, "TimerFunction");
AddTimer("timertwo", 2, "TimerFunction");
AddTimer("timerthree", 3, "TimerFunction");


void TimerFunction(string &in timer)
{
    switch(timer) {
        case timerone:
            //cool stuff
        break;
        case timertwo:
            //cool stuff
        break;
        case timerthree:
            //cool stuff
        break;
    }
}


And here is what I'm currently trying (I get errors on recompiling the script)

Code:
void CaveBreathTimer(string &in timer)
{
    switch(CaveBreathStep(timer)) {
        case 1:
            //stuff 1
        break;
        case 2:
            //stuff 2
        break;
        case 3:
            //stuff 3
        break;
    }
}

int CaveBreathStep(string &in stepname)
{
    if(stepname == "cavebreath") return 1;
    if(stepname == "screenshake") return 2;
    if(stepname == "other") return 3;
}

And here's the error I get.

INFO : Compiling int CaveBreathStep(string&in)
ERR : Not all paths return a value

I'm trying to use a subroutine to return an integer which is used by the switch function to determine which case to execute, but the subroutine I wrote is giving me the above error. This way I can avoid using long-winded if statements. If this doesn't work I'll resort to sucking it up and neatly tabulating my if statements.

Also, is there any way to use straight-up use strings as case values in switch functions with AngelScript? I searched online and found a few vague threads that showed C++ code doing it.

I'm stepping into unknown territory, so if you could elaborate on this, please do. This is my first time trying to use return values Smile Thanks
ok, i was the OP in the other thread, and I think I know how to get it working. I'm not an expert when it comes to scripting, so I cannot separate the integers from the string but you could do this:
Code:
void func()
{
AddTimer("1" , 1.0f , "Timer);
AddTimer("2" , 2.0f , "Timer);
AddTimer("3" , 3.0f , "Timer);
}
void Timer(string &in asTimer)
{
int x = asTimer;
switch9(x){
case 1:
     // stuff
     break;
case 2:
     // stuff
     break;
case 3:
     // stuff
     break;
     }
}
you mght also be able to do this
Code:
AddTimer("T1" , 1.0f , "Timer");
AddTimer("T2" , 2.0f , "Timer");


void Timer(string &in asTimer)
{
int x = SubString(asTimer , 2 , 1);
switch(x){
   etc...
}

}
im not sure about the proper syntax fo SubString though.

edit: I've just reread youre original post, I'm not sure if the above will help you at all. I dont know much about return values myself


Another Edit: I think I know the problem in your original code - you haven;'t got an if statement for if the asTimer is not any of the above, maybe
else return 0;
becausee maybe asTimer="" before the timer is called..
What you posted is a switch function with integer-type case values, which is the norm with Amnesia scripting. That structure is great for step-wise in-game events, like fainting in a hallway or a cave-in with lots of sounds and such.

However, I'm trying to find a way to execute a case based on a timer's name, which isn't simply an integer. I want the timer to be named according to what it does, so I and anyone else reading it can easily follow the flow of the script.

Also I'm not sure if that will work, since you're trying to convert a string to an integer in that code.
If you haven't fixed it already, you should try returning 0 at the bottom of the ifs and see what happens. I'm thinking that the function doesn't like not having a return for if none of the ifs are true.
It worked Ouroboros, thanks very much Smile