2016-10-30 10:29:01

Hi all.
In two cases, the error appears in the game.
Code:
if (key_pressed (KEY_ESCAPE))
{
s.destroy_all ();
for (int i = 0; i <5; i ++)
{
enemies (i) .e.destroy_all ();
enemies.remove_at (i);
}
mainmenu ();
}
Error:
Call stack size: 3

File: D: \ Warrior Adventure \ game.bgt
Line: 60 (1)
Function: void l1 ()

File: D: \ Warrior Adventure \ includes \ menu.bgt
Line: 24 (1)
Function: void mainmenu ()

File: D: \ Warrior Adventure \ game.bgt
Line: 21 (1)
Function: void main ()

Code:
if (hp <= 0)
{
for (int i = 0; i <5; i ++)
enemies.remove_at (i);
s.destroy_all ();
dlgplay ( "k" + random (1,4) + "wav.");
mainmenu ();
}
Error:
Call stack size: 4

File: D: \ Warrior Adventure \ game.bgt
Line: 75 (19)
Function: void l1 ()

File: D: \ Warrior Adventure \ includes \ menu.bgt
Line: 24 (1)
Function: void mainmenu ()

File: D: \ Warrior Adventure \ includes \ menu.bgt
Line: 30 (1)
Function: void mainmenu ()

File: D: \ Warrior Adventure \ game.bgt
Line: 21 (1)
Function: void main ()

[] Replaced by (), as the forum does not accept.
Help me please!

2016-10-30 10:38:44

class enemy
{
int health;
int speed;
int position;
bool died;
sound_pool e;
timer steper;
timer firer;
enemy()
{
health=100;
speed=300;
position=random(30,150);
died=false;
}
void move()
{
if(died==false)
{
if(position<x)
{
if (steper.elapsed>speed)
{
e.play_1d("step1_"+random(1,3)+".ogg", x, position, false);
position++;
steper.restart();
}
}
if(position>x)
{
if (steper.elapsed>speed)
{
e.play_1d("step1_"+random(1,3)+".ogg", x, position, false);
position--;
steper.restart();
}
}
}
}
void speak()
{
if(died==false)
{
e.play_1d("voice"+random(1,9)+".wav", x, position, true);
}
}
void fire()
{
if(died==false)
{
if(absolute(x-position)<=5)
{
if(firer.elapsed>800)
{
e.play_1d("pistol.wav", x, position, false);
hp-=random(15,30);
firer.restart();
}
}
}
}
void die()
{
if(died==false)
{
if(health<=0)
{
died=true;
e.destroy_all();
e.play_1d("die"+random(1,24)+".wav", x, position, false);
position=-500000;
}
}
}
}

2016-10-30 10:42:04 (edited by jonikster 2016-10-30 10:43:38)

Main file:
sound_pool s;
int x=1;
int hp;
timer step;
enemy@[] enemies(0);

void main()
{
show_game_window("Warrior Adventure");
set_sound_decryption_key("ILoveKids", true);
set_sound_storage("sounds.dat");
install_keyhook();
wait(1000);
dlgplay("logo.wav");
wait(500);
mainmenu();
}
void l1()
{
dlgplay("l1.wav");
s.play_stationary("ambiant1.wav", true);
hp=100;
for(int i=0; i<5; i++)
{
enemy enemy1();
enemies.insert_at(i, enemy1);
enemies(i).speak();
}
while(true)
{
for(int i=0; i<5; i++)
{
enemies(i).move();
enemies(i).fire();
enemies(i).die();
}
if(key_down(KEY_LEFT) and step.elapsed>300 and x>1)
{
x-=1;
s.play_stationary("step1_"+random(1, 3)+".ogg", false);
step.restart();
}
if(key_down(KEY_RIGHT) and step.elapsed>300 and x<150)
{
x+=1;
s.play_stationary("step1_"+random(1, 3)+".ogg", false);
step.restart();
}
if(key_pressed(KEY_ESCAPE))
{
s.destroy_all();
for(int i=0; i<5; i++)
{
enemies(i).e.destroy_all();
enemies.remove_at(i);
}
mainmenu();
}
if(key_pressed(KEY_C))
{
speak(x);
}
if(key_pressed(KEY_H))
{
speak(hp);
}
if(hp<=0)
{
for(int i=0; i<5; i++)
enemies.remove_at(i);
s.destroy_all();
dlgplay("k"+random(1,4)+".wav");
mainmenu();
}
if(key_pressed(KEY_SPACE))
{
s.play_stationary("pistol.wav", false);
for(int i=0; i<5; i++)
{
if(absolute(x-enemies(i).position)<=5)
{
s.play_1d("pain"+random(1,15)+".wav", x, enemies(i).position, false);
enemies(i).health-=random(15,30);
}
}
}
s.update_listener_1d(x);
wait(5);
}
}
void test()
{
dlgplay("test.wav");
}

2016-10-30 13:23:01

Try decrementing the counter ("i--;") in the for loop where you destroy enemy sounds. I mean the very first for loop right at the top when escape is pressed.
Lukas

I won't be using this account any more or participating in the forum activity through other childish means like creating an alternate account. I've asked for the account to be removed but I'm not sure if that's actually technically possible here. Just writing this for people to know that I won't be replying, posting new topics or checking private messages until the account is potentially removed.

2016-10-30 13:52:27

You can give an example?
Array I have written is correct?

2016-11-09 13:50:05

Sorry, my original answer was a bit rushed, but what I meant was that you have the following loop:

for(int i=0; i<5; i++)
{
enemies(i).e.destroy_all();
enemies.remove_at(i);
}

It should look like this instead:

for(int i=0; i<5; i++)
{
enemies(0).e.destroy_all();
enemies.remove_at(0);
}

Can you tell why?
Also, do you really need to use a separate sound pool for every enemy instance? That's probably not a good idea in most cases.

Lukas

I won't be using this account any more or participating in the forum activity through other childish means like creating an alternate account. I've asked for the account to be removed but I'm not sure if that's actually technically possible here. Just writing this for people to know that I won't be replying, posting new topics or checking private messages until the account is potentially removed.