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
DRedshot Offline
Senior Member

Posts: 374
Threads: 23
Joined: Jun 2011
Reputation: 11
#1
switch statements

I just read a thread here. I decided to make a new thread because his question has already been answered.

basically, ive never used switch before, because ive never known how to use it, until now. But apparantly 'case' only works with integers, so i couldnt do for example: case asEntity:

but my question is: could i do this with a switch command
void RandomFunction()
{
AddTimer("1" , 1.0f , "Timer");
AddTimer("2" , 2.0f , "Timer");
AddTimer("3" , 3.0f , "Timer");
AddTimer("4" , 4.0f , "Timer");
}
void Timer(string &in asTimer)
{
switch(asTimer){
case 1:
// Do Stuff
break;
case 2:
// Do More Stuff
break;
case 3:
// Do Other Stuff
break;
case 4:
// Do Funky Stuff
break;
}
}
Or would i have to declare asTimer as an integer?
it would make timers much less boring - thanks!

(This post was last modified: 07-20-2011, 08:57 PM by DRedshot.)
07-20-2011, 08:00 PM
Find
Khyrpa Offline
Senior Member

Posts: 638
Threads: 10
Joined: Apr 2011
Reputation: 24
#2
RE: switch statements

I like to use fEventSpeed to set the time between each case, but this switch thing can be done in various ways (copied this style from frictional)...
void RandomFunction()  //this thing here shows that you can start this thing from anything
{AddTimer("tostarthetimer" , 0.0f , "Timer");}

void Timer(string &in asTimer) //must be timer in this
{AddLocalVarInt("Stage", 1);
float fEventSpeed = 1.0f;
    switch(GetLocalVarInt("Stage"))
    {    case 1:
            //dostuff
            break;
        case 2:
            //Do More Stuff
            break;
        case 3:
            //Do Other Stuff
            break;
        case 4:
            //Do Funky Stuff
            break;
        }
if(GetLocalVarInt("Stage") < 5)   AddTimer("loopiething", fEventSpeed, "Timer");
}

Another way of using switch (I dont really like this way ^^)
http://wiki.frictionalgames.com/hpl2/tut...ncedtimers

Oh now I get what you actually meant... Mybad this was completely offtopic!
(This post was last modified: 07-20-2011, 08:30 PM by Khyrpa.)
07-20-2011, 08:24 PM
Find
Kyle Offline
Posting Freak

Posts: 911
Threads: 36
Joined: Sep 2010
Reputation: 7
#3
RE: switch statements

(07-20-2011, 08:24 PM)Khyrpa Wrote: I like to use fEventSpeed to set the time between each case, but this switch thing can be done in various ways (copied this style from frictional)...
void RandomFunction()  //this thing here shows that you can start this thing from anything
{AddTimer("tostarthetimer" , 0.0f , "Timer");}

void Timer(string &in asTimer) //must be timer in this
{AddLocalVarInt("Stage", 1);
float fEventSpeed = 1.0f;
    switch(GetLocalVarInt("Stage"))
    {    case 1:
            //dostuff
            break;
        case 2:
            //Do More Stuff
            break;
        case 3:
            //Do Other Stuff
            break;
        case 4:
            //Do Funky Stuff
            break;
        }
if(GetLocalVarInt("Stage") < 5)   AddTimer("loopiething", fEventSpeed, "Timer");
}

Another way of using switch (I dont really like this way ^^)
http://wiki.frictionalgames.com/hpl2/tut...ncedtimers

Oh now I get what you actually meant... Mybad this was completely offtopic!

Thanks for radding off on my wiki page! Tongue

07-20-2011, 08:31 PM
Find
Khyrpa Offline
Senior Member

Posts: 638
Threads: 10
Joined: Apr 2011
Reputation: 24
#4
RE: switch statements

(07-20-2011, 08:31 PM)Kyle Wrote: Thanks for radding off on my wiki page! Tongue

I'm not sure what radding means, but I guess its something negative. I just happen to like the event speed thing more, x == stuff seems scary to me!

OnTopic:

void Timer(string &in asTimer)
{SetLocalVarInt("whatcase", asTimer);
switch(GetLocalVarInt("whatcase"))
{case 1:
// Do Stuff
break;
case 2:
// Do More Stuff
break;
case 3:
// Do Other Stuff
break;
case 4:
// Do Funky Stuff
break;
}
}

should do it
(This post was last modified: 07-20-2011, 08:41 PM by Khyrpa.)
07-20-2011, 08:38 PM
Find
DRedshot Offline
Senior Member

Posts: 374
Threads: 23
Joined: Jun 2011
Reputation: 11
#5
RE: switch statements

thanks Khyrpa, i don't think it was completely off topic!
as for the other way
Quote:Another way of using switch (I dont really like this way ^^)
http://wiki.frictionalgames.com/hpl2/tut...ncedtimers
That is exactly what my original post is about, I don't like using:
if(asTimer=="Time1"){// do stuff}
else if(asTimer=="Time1"){// do stuff}
else if(asTimer=="Time1"){// do stuff}
else if(asTimer=="Time1"){// do stuff}
because you end up with loads of curly brackets, and loads of lines of code. My original question was how to use the switch statement to do the above code in a neater way with less brackets Tongue

edit, just read your second post, i think this thread is solved. Thanks!

(This post was last modified: 07-20-2011, 08:44 PM by DRedshot.)
07-20-2011, 08:42 PM
Find
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#6
RE: switch statements

The cases don't have to be integers. You can do this

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;
    }
}

07-20-2011, 08:46 PM
Find
DRedshot Offline
Senior Member

Posts: 374
Threads: 23
Joined: Jun 2011
Reputation: 11
#7
RE: switch statements

wow i didn't even know that, thanks palistov. That makes this whole thread pointless...
the only reason I assumed you couldn't use strings is from reading this page. Oh well, thanks everyone!

07-20-2011, 08:49 PM
Find
palistov Offline
Posting Freak

Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation: 57
#8
RE: switch statements

Just found out that you can't use strings as cases in a switch function. The values needs to be an integer. There's functionality for string use in switch-case functions, but not for AngelScript I guess Sad

Just resort to if statements. I'll try to find a nice way to use switch statements with strings, as I personally think they are much cleaner and easier to read for someone trying to learn from your script.

07-22-2011, 10:34 AM
Find




Users browsing this thread: 1 Guest(s)