Game crashes! no error message, just stops

I am programming a game that has bots. Everything worked fine until I tried to make the bots respawn. I know that the error lies within the following code, however, I don't know specifically what is wrong.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
void bot::reset_bot(){
     dead = 0;
     dir = 1;
     attack = 0;
     if (ds==1){
                 x = p.x + 7;
                 y = p.y - 8;}
                 
     if (ds==2){
                 x = p.x + 12;
                 y = p.y - 8;}
                 
     if (ds==3){
                 x = p.x + 17;
                 y = p.y - 8;}
                 
     if (ds==4){
                 x = p.x + 22;
                 y = p.y - 8;}
                 
     if (ds==5){
                 x = p.x + 25;
                 y = p.y - 8;}}
                 
void check_bot_respawn(){
     if (g1.dead==1){
        if (p.x-g1.x>=17)
           g1.reset_bot();}
     if (g2.dead==1){
        if (p.x-g2.x>=17)
           g2.reset_bot();}
     if (g3.dead==1){
        if (p.x-g3.x>=17)
           g3.reset_bot();}
     if (t1.dead==1){
        if (p.x-t1.x>=17)
           t1.reset_bot();}
     if (t2.dead==1){
        if (p.x-t2.x>=17)
           t2.reset_bot();}}


The reset_bot functions are successfully executed at the beginning of the game, but when they get called by check_bot_respawn, the program quits.

Please tell me what is wrong with my code.
Last edited on
I don't see anything wrong with this code (apart form weird formatting).
The problem might be elsewhere.
Put some debug output (like cout << "this is check_bot_respawn()!/n") in your functions (all of them) to see which one is the last executed (causes the problem).
Wouldn't be easier to do backtrace with a debugger?
Through commenting out the various functions getting called in the game loop, I was able to determine that check_bot_respawn() was the problem. The game would run fine, then when check_bot_respawn() called reset_bot(), the game closed.

Here is my game loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void world1_loop(){
     while (w1c==0){
           w1_move_p();
           g1.move_w1();
           g2.move_w1();
           g3.move_w1();
           t1.move_w1();
           t2.move_w1();
           
           w1_gravity();
           if (p.jump==1)
           w1_jump();
           
           check_kill();
           w1c = check_lose_w1();
           w1c = check_win_w1();
           
           draw_screen_w1();
           
           //problem here
           check_bot_respawn();

           if (key[KEY_ENTER])
           w1c = pause();
           
           
           if (botc>0)
           rest(40);
           else
           rest(80);}


I will try debugging stuff I guess.
Solved it! I don't know what happened with the original code, but I made check_bot_respawn() part of my "bot" class and put the respawn code right in the conditional statements rather than calling a separate function.
Thanks for your responses!
Solved it! I don't know what happened with the original code, but I made check_bot_respawn() part of my "bot" class and put the respawn code right in the conditional statements rather than calling a separate function.
Thanks for your responses!
Topic archived. No new replies allowed.