Switching - By Ad0
Hello, and
welcome to another edition of Ad0's coding tutorial. This
tutorial has freshbaked code knowledge straight from mama's
oven :)
This
tutorial will cover how to switch between severeal modes,
functions and so on using the switch case. This is very
helpful to have if you are planning to have different fire
modes on a shotgun for instance.
Well,
enough sweetie talkin'! Let's get to the serious hardcore
coding!!! (Excuse my mood)
First,
open g_cmds.c in the game project. This is the ONLY file we
are going to code in today. First find a spot for creating
your function. Call it Cmd_SuperSwitch_f. Make the function
"frame/template" as the Cmd_Score_f, but don't add the line
inside (DeathmatchScoreboardMessage( ent );) Now we're
ready to code.
This is a
bit hard to explain without any code, so I have to show you
some of it, but don't paste! Just read it, learn it, and then
write it off, or better, write it in your own, personal
way. The very first thing to do, is to create an array, and
at the same time, declare it as a char var, and
MAX_TOKEN_CHARS is the value that is the max amout the letters
you shall have in our current array:
char
MySwitchArray[MAX_TOKEN_CHARS];
Now,
that's an array! The very next thing to do is to add a
declaration for our integer, a global declaration. This can be
done by adding it under #include "g_local.h" in g_cmds.c, also
not inside our function, but outside.
int
MyGlobalSuperSwitchI;
Ok, now we
got all the declarations done. The next thing to do is to
create a switch statement using the case label. This is a
little example og all that in use in standard ANSI
C:
switch (num)
{ case
1: do_stuff(); break; case
2: do_some_other_stuff_as_well(); break; default: MaybeAnErrorMessageIfYouWantTo(); }
That's the
example. Now you see how case is used. Now, that we know that,
we can use case 1, case 2, etc. as cycling through those cases
by using a count-upwards by making our global int,
MyGlobalSuperSwitchI, incremented by adding "++" and a ";" of
course. The "++" is called the preincrement operator, and is
made to count upwards. The meaning of this in this case, is to
count through the cases, like case 1, and then case 2, like
described above. I haven't told you why we use an array.
Now I am going to tell you: This must be used, because we
assign each int number to a "happening", in this case, text
messages.
Now, you
can just use the example and use my explaination and create
the case statement and the count the preincrement operator to
count upwars the cases, one step each time of execution of my
function. I will now add a code for giving you the
opportunity to understand this in practical use in Quake 3
coding:
void Cmd_SuperSwitch_f(gentity_t *ent)
{ char
MySwitchArray[MAX_TOKEN_CHARS];
MyGlobalSuperSwitchI++; // Counts upwards, so it starts on
1
switch
(MyGlobalSuperSwitchI)
{ case 1:
strcpy( MySwitchArray, "Sentence Number ONE\n"
);
break;
case 2: strcpy( MySwitchArray, "Sentence Number TWO\n"
); break;
case 3: strcpy( MySwitchArray, "Sentence Number THREE\n" );
break;
case 4: strcpy( MySwitchArray, "...and so on!\n" );
break;
}
if
(MyGlobalSuperSwitchI >= 4) // Checks when it has hit the
end
{
MyGlobalSuperSwitchI = 0; // Starts on 0, also the
first.
} trap_SendServerCommand(
ent-g_entities, va("print \"%s\"", MySwitchArray)); // Sends
the message locally to the
client }
This is an
example on how you can switch through the different sentences
by executing this function. The only thing that remains now,
is to add it to the cvar list in the bottom g_cmds.c file.
Just look at the code there and understand it. I show you this
one for reference:
Cmd_SuperSwitch_f
(ent); else if
(Q_stricmp (cmd, "switch") == 0)
This code
is very handy if you want to switch through playermodes, flags
messages, whatever. I hope you have use for
it!
I hope you understood
this... Just send questions to my mail address
above.
For my music, go here!
Cheers!
|