Facebook Twitter YouTube Frictional Games | Forum | Privacy Policy | Dev Blog | Dev Wiki | Support | Gametee


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Switch statements with string case values
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#1
Switch statements with string case values

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:

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)

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

(This post was last modified: 07-22-2011, 11:00 AM by palistov.)
07-22-2011, 10:38 AM
Find
DRedshot Offline
Senior Member

Posts: 374
Threads: 23
Joined: Jun 2011
Reputation: 11
#2
RE: Switch statements with string case values

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:
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
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..

(This post was last modified: 07-22-2011, 11:06 AM by DRedshot.)
07-22-2011, 10:53 AM
Find
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#3
RE: Switch statements with string case values

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.

(This post was last modified: 07-22-2011, 11:07 AM by palistov.)
07-22-2011, 11:06 AM
Find
Ouroboros Offline
Junior Member

Posts: 20
Threads: 0
Joined: Jul 2011
Reputation: 0
#4
RE: Switch statements with string case values

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.
07-22-2011, 11:32 AM
Find
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#5
RE: Switch statements with string case values

It worked Ouroboros, thanks very much Smile

07-22-2011, 12:51 PM
Find




Users browsing this thread: 2 Guest(s)