Appendix V
Brain Teaser Answers
Brain Teaser 1: Why wouldn't this variation of the code above work to start game in any of the four possible connection modes?
// if not a single player game, wait for connection
ifdef server;
while(connection== 0) {wait(1);} // wait until level loaded and connection set
ifelse; // not server
while(connection== 0) {wait(1);} // wait until level loaded and connection set
endif;
This would not work in the event if you were trying to start in single player mode, where both server and client variables are not defined. What would happen is the program would determine that server variable is not defined, then go directly into ifelse and get stuck in and endless loop waiting for a connection in single player mode that would never be made.
Brain Teaser 2: Why is it a good idea to define a variable like
define PROF_NUCLEAR_SCIENTIST, 1; // Nuclear scientist
and use it in an if statement like
if (my.profession == PROF_NUCLEAR_SCIENTIST)
instead of just doing an if statement like so?
if (my.profession == 1)
If you define the variable it will be easier to understand when reading the code, especially for someone who is working on the code that didn't actually write it. Nothing is worse then reading through some script trying to figure out what is going on in it and see a statement like tbelow that may take hours to backtrack through the script to figure out what these values mean. Let's say you are working for a team and the bossman says, "The bullets for the Assassasin's XX47 Killer rifle aren't going fast enough when using 7.2mm round, could you speed that up for me?"
You look at the code and you feel a bit of anxiety when this shows up.
if ((my.profession == 1)||( my.profession == 4)||(my.profession == 8))
{
if (((my.weapon == 3) || (my.weapon==15))&&(my.ammo == 13))
{
you.speed = 10;
}
if (((my.weapon == 7) || (my.weapon==12))&&(my.ammo == 5))
{
you.speed = 15;
}
etc.....
}
if ((my.profession == 1)||( my.profession == 2)||(my.profession == 18))
{
if (((my.weapon == 4) || (my.weapon==5))&&(my.ammo == 3))
{
you.speed = 20;
}
if (((my.weapon == 11) || (my.weapon==22))&&(my.ammo == 19))
{
you.speed = 15;
}
etc.....
}
etc....
"Hey Bossman, the person who wrote that can fix it themself, I aint going to do it!" Or just speed them all up and maybe the bossman won't notice.
Also, defines are very useful for set values you might change later, like
define PROF_NUCLEAR_SPEED, 6; // speed of a nuclear scientist
Now, if you start testing, you don't have to search through code to find where you set the player's speed. All you have to do is look in your defines and change it to the new speed you want. This is especially useful if the value is used multiple times within the program. You don't have to find the multiple occurences, but only have to change the one define.
Brain Teaser 3: Why don't we just set the text pos_x in the text declaration itself like so?
// text to display the number of people connected to game
text txt_people_connected
{
pos_x = screen_size.x - 350;
Because declarations can't handle mathimatical equations. It would result in an error.
Brain Teaser 4: Why did we place the function animate() above the action move and not below it?
The reason I go out of my way sometimes to place a function above another is if a function or action is calling another function, it won't recognize that function if it is below it without declaring function prototypes. The exception to this rule in the main() function which recognizes all functions even if they are below it.
So for our example, if I would have placed function animate() below action move I would get an function does not exist error when action moved tried to call it unless I put a prototype for function move into declarations like so.
// Declarations
// Function Prototypes
function animate();
If you place the functions in the correct places you seldom need to do function prototypes.