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.
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.
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).
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.
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!