2015-10-22 20:45:07

I'm developing a game which I hope you guys will like, but I'm having some issues.
I've had these issues before, and was never really able to come upon a surefire solution.
The code is in very early beta stages at the moment, so I will go ahead and paste it here.
But before I do, allow me to explain the issue I'm having.
I am using a class to define walls and traps (walls only currently), that the player will have to fly around.
Allow me to explain what I'm doing.
I want to check if the player is on the same x axis as a wall, and if the y position is less than the walls y position, it plays an alarm sound. If they are equal, bang. Game over.
But when I go to check said stats, it complains that "barier.x is not declared", or x is not a member of int.
I think, I might need to use handles which confuse the royal crap out of me.
Ideas?
Code is here, all you need is your own sounds as specified to run it successfully.
#include "sound_pool.bgt";
sound_pool pool;
int alarmsound;
double thrust=3;
int wallsound;
int thrustcounter;
int engine;
int level;
int health;
int maxhealth;
double speed;
double maxspeed=40;
int spawncounter;
int shipx;
int shipy;
tts_voice voice;
timer thrusttimer;
timer speedtimer;
timer movetimer;
timer spawntimer;
class wall
{
int x;
int y;
int damage;
void update()
{
pool.update_sound_2d(wallsound, shipx, shipy);
//if(shipy<barrier.y)
{
//if(shipx-3==barrier.x or shipx-2==barrier.x or shipx-1==barrier.x or shipx==barrier.x or shipx+1==barrier or shipx+2==barrier or shipx+3==barrier)
{
alarmsound=pool.play_stationary("sounds/alarm.ogg", true, true);
}
}
}
wall(int init_x, int init_y, int init_damage)
{
x=init_x;
y=init_y;
damage=init_damage;
wallsound=pool.play_2d("sounds/wall.ogg", shipx, shipy, x, y, true, true);
}
}
void main()
{
pool.volume_step=.35;
pool.pan_step=.05;
pool.behind_pitch_decrease=6;
show_game_window("Cannon crossfire: Civilian lines.");
voice.speak_wait("loading sounds. Pliz wait.");
newgame();
}
void newgame()
{
shipx=50;
shipy=0;
speed=80;
engine=pool.play_stationary("sounds/engine.ogg", true, true);
for(int i=0; i<6500; i++)
{
wall barrier(random(0, 100), random(0, 10000), 150);
}
wall barrier(50, 350, 0);
gameloop();
}
void gameloop()
{
speedtimer.resume();
while(true)
{
wait(10);
pool.update_listener_2d(shipx, shipy);
if(speed<=40)
{
speed=40;
force_key_up(KEY_DOWN);
}
else if(speed>42)
{
reset_forced_key(KEY_DOWN);
}
pool.items[engine].handle.pitch=speed;
if(speedtimer.elapsed>(7000/speed))
{
shipy++;
speed=speed+0.35;
speedtimer.restart();
pool.play_stationary("sounds/debugclick.ogg", false, false);
}
if(key_pressed(KEY_ESCAPE))
{
exit();
}
if(key_down(KEY_DOWN))
{
thrusttimer.resume();
if(thrusttimer.elapsed>thrustcounter)
{
speed=speed-thrust;
thrusttimer.restart();
}
}
if(key_pressed(KEY_C))
{
voice.speak_interrupt(shipx+", "+shipy);
}
if(key_pressed(KEY_S))
{
voice.speak_interrupt(speed);
}
}
}

2015-10-22 21:08:18 (edited by CAE_Jones 2015-10-22 21:17:10)

It looks like you aren't storing the walls you're creating. If I understand the code correctly, the newgame function does this:

assign shipx, shipy, and speed (global variables)
play the engine sound, and assign its slot number to the global variable engine
Create a local variable i, initialize it to 0
Since i is less than 6500, do the following:
create a local variable called barrier, that is a wall with random parameters.
delete barrier, since this iteration of hte loop is complete.
increment i.
repeat the last 4 lines until i is 6500 (Incidentally, that is a lot of objects and the sound pool will probably explode.)
delete i, since the loop is complete.
Create a local wall variable.
Call gameloop.
Delete barrier


The barrier variable created inside newgame (or inside the loop) cannot be accessed by any other functions. You probably want a global variable--in this case, probably an array or dictionary--to hold all the walls created in the loop.
Try taking the line
wall barrier(50, 350, 0);
And either putting it outside the function, putting it at the start of gameloop, or splitting it into three lines:


wall@ barrier; //outside of any functions or classes.


// inside newgame:
wall temp(50, 350, 0);
@barrier=temp;


I think BGT will complain about the last one, because something something constructors. It'd be easier just to move wall barrier(50, 350, 0); into a different scope.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-10-22 21:10:10

er, crap.
This is really my first time attempting to use classes. What would be the ideal way to set and store the walls then?
Thanks for your reply.

2015-10-22 21:17:52

(Sorry, somehow it hit submit before I finished writing the post. I tried to edit more detail in.)

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-10-22 22:50:34

I mostly understand what your saying, but beyond this my head exploded:
create a local variable called barrier, that is a wall with random parameters.
delete barrier, since this iteration of hte loop is complete.
increment i.
repeat the last 4 lines until i is 6500 (Incidentally, that is a lot of objects and the sound pool will probably explode.)
delete i, since the loop is complete.
Create a local wall variable.
I suspect all of this will be done in a forloop, but unfortunately my head explodes every time I attempt to put it into code.

2015-10-23 00:25:37

That was what your current code does.
What you want to do is more like this:


for(uint i=0; i<wall_num; i++) {
wall barrier(random(0, 100), random(0, 100), random(0, 100));
walls.insert_last(barrier);
}

Assuming that you have an array of walls, which needs to take handles, like so:

wall@[] walls;

Also, if BGT complains about "no default constructor", just add the line "wall() {}" to the wall class.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-10-23 01:50:24

I've done all the necessary modifications to the code and I now get the following error when attempting to run. Is this me being an idiot or is there something severely wrong?
File: D:\spaceshiptest\game.bgt
On line: 68 (1)
Information: Compiling void gameloop()

File: D:\spaceshiptest\game.bgt
On line: 76 (97)
Line: if(shipx-3==barrier.x or shipx-2==barrier.x or shipx-1==barrier.x or shipx==barrier.x or shipx+1==barrier or shipx+2==barrier or shipx+3==barrier)
Error: No conversion from 'wall@&' to 'int' available.

File: D:\spaceshiptest\game.bgt
On line: 76 (117)
Line: if(shipx-3==barrier.x or shipx-2==barrier.x or shipx-1==barrier.x or shipx==barrier.x or shipx+1==barrier or shipx+2==barrier or shipx+3==barrier)
Error: No conversion from 'wall@&' to 'int' available.

File: D:\spaceshiptest\game.bgt
On line: 76 (137)
Line: if(shipx-3==barrier.x or shipx-2==barrier.x or shipx-1==barrier.x or shipx==barrier.x or shipx+1==barrier or shipx+2==barrier or shipx+3==barrier)
Error: No conversion from 'wall@&' to 'int' available.
modified code is below
#include "sound_pool.bgt";
wall@ barrier; //outside of any functions or classes.
wall@[]walls;
sound_pool pool(500);
int alarmsound;
double thrust=3;
int wallsound;
int thrustcounter;
int engine;
int level;
int health;
int maxhealth;
double speed;
double maxspeed=40;
int spawncounter;
int shipx;
int shipy;
tts_voice voice;
timer thrusttimer;
timer speedtimer;
timer movetimer;
timer spawntimer;
class wall
{
int x;
int y;
int damage;
void update()
{
//pool.update_sound_2d(wallsound, shipx, shipy);
}
wall(int init_x, int init_y, int init_damage)
{
x=init_x;
y=init_y;
damage=init_damage;
wallsound=pool.play_2d("sounds/wall.ogg", shipx, shipy, x, y, true, true);
}
}
void main()
{
pool.volume_step=1.75;
pool.pan_step=.85;
pool.behind_pitch_decrease=4;
show_game_window("Cannon crossfire: Civilian lines.");
voice.speak_wait("loading sounds. Pliz wait.");
newgame();
}
void newgame()
{
// inside newgame:
wall temp(50, 350, 0);
@barrier=temp;
shipx=50;
shipy=0;
speed=80;
engine=pool.play_stationary("sounds/engine.ogg", true, true);
int wall_num=400;
for(uint i=0; i<wall_num; i++)
{
wall barrier(random(0, 100), random(0, 3000), random(0, 100));
walls.insert_last(barrier);
wall@[] walls;
}
//wall barrier(50, 350, 0);
gameloop();
}
void gameloop()
{
speedtimer.resume();
while(true)
{
wait(10);
if(shipy<barrier.y)
{
if(shipx-3==barrier.x or shipx-2==barrier.x or shipx-1==barrier.x or shipx==barrier.x or shipx+1==barrier or shipx+2==barrier or shipx+3==barrier)
{
alarmsound=pool.play_stationary("sounds/alarm.ogg", true, true);
}
}
pool.update_listener_2d(shipx, shipy);
if(speed<=40)
{
speed=40;
force_key_up(KEY_DOWN);
}
else if(speed>42)
{
reset_forced_key(KEY_DOWN);
}
pool.items[engine].handle.pitch=speed;
if(speedtimer.elapsed>(7000/speed))
{
shipy++;
speed=speed+0.35;
speedtimer.restart();
pool.play_stationary("sounds/debugclick.ogg", false, false);
}
if(key_pressed(KEY_ESCAPE))
{
exit();
}
if(key_down(KEY_DOWN))
{
thrusttimer.resume();
if(thrusttimer.elapsed>thrustcounter)
{
speed=speed-thrust;
thrusttimer.restart();
}
}
if(key_pressed(KEY_C))
{
voice.speak_interrupt(shipx+", "+shipy);
}
if(key_pressed(KEY_S))
{
voice.speak_interrupt(speed);
}
}
}

2015-10-23 02:57:52

The trouble is with the parts that say "==barrier" without any properties (barrier.x? barrier.y?).

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-10-23 03:14:52

I think so, I would like to point out this is my first attempt at using classes. And handles to objects confuse the royal crap out of me.
Thanks for all your help

2015-10-24 03:55:53

is anyone willing to help me any further?
I'm still confused at post 8, and have tried re reading the chapter on objects, classes and handles.
I get the idea of classes, just handles themselves confuse me.

2015-10-24 04:29:31

The error it gave points to this line:
if(shipx-3==barrier.x or shipx-2==barrier.x or shipx-1==barrier.x or shipx==barrier.x or shipx+1==barrier or shipx+2==barrier or shipx+3==barrier)

It should be:

if(shipx-3==barrier.x or shipx-2==barrier.x or shipx-1==barrier.x or shipx==barrier.x or shipx+1==barrier.x or shipx+2==barrier.x or shipx+3==barrier.x)


However, there is a much simpler way to accomplish that condition:

if(absolute(barrier.x-shipx)<=3)
^ Since you want to know if the distance between shipx and the barrier is less than or equal to 3, just take the absolute value of the difference, and that is the total distance.

A problem you will run up on rather quickly is writing collision detection for all the different walls. A way to speed this up would be to add a method to the wall class. It might be something like:

bool contains(int px, int py) {
return (absolute(x-px)<=3 and absolute(y-py)<=3);
}

Or something like that.

As for handles in general... if you need an object to be a property of a class, or if you just need an object to survive multiple scopes, or if you need an array of objects, you'll need to use handles. You can treat handles like the objects they point to for almost everything; you mostly only need to bother with the at sign for assignment and equality (E.G., if(@handle==null) ). The biggest difference between handles and regular variables is that variables get copied when they are assigned, but multiple handles can still point to the same object.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-10-24 07:27:08 (edited by x0 2015-10-24 07:27:50)

Omar, I would be more than happy to help you. If you want, just add me on skype and i can help you with almost anything oyu need help with with bgt. As long as you're not using geometry... Cae's your go to for that.

----------
An anomaly in the matrix. An error in existence. A being who cannot get inside the goddamn box! A.K.A. Me.

2015-10-24 08:14:49

Thanks cae_jones. Slowly I'm putting this together.
coltonhill,
I would be glad to receive your assistance. You may send me a forum email. If you want my twitter its @firebird1409