trying to solidify my understanding of coding concepts, and failing

Hi all,
I've posted a few times on audiogames about how I will never be able to code games, even in comparatively simple languages such as BGT, and how frustrated I was while trying to learn. Nevertheless, something in me keeps me coming back and trying, despite my dozens of impulses to just throw in the towel and give up the endeavor. I guess I have always dreamed of designing something and trying to make it happen, because musically I'm pretty good at doing just that. and I enjoy games enough to have a spark in me that keeps wanting to try. And I do make progress with coding, so not all hope is lost.

I'm more comfortable with basic constructs like loops, functions and conditional statements than I ever was before. I'm not at a level most would consider good, but I definitely feel a lot better than I did, and have made a few small programs with those constructs. But I still struggle to know if i'm coding cleanly, or even to know if I can code something at all.

Right now, I'm working on trying to understand objects/classes in BGT. I get their purpose at least on a basic level. But when we start talking about handles is where I run into trouble.

The BGT manual does mention what handles are, but I'm having a hard time understanding how I can use them for things.

BGT Manual]You may want to pass objects around various functions of your own. To do this, it is essential that you pass handles, and not the object itself. A handle is essentially a reference to the original object. To do this, you would do something like the following:

sound@ play_sound(string filename)
{
sound the_sound;
the_sound.load(filename);
the_sound.play();
return the_sound;
}
void main()
{
show_game_window("My Game");
sound@ ambience=play_sound("curry.wav");
ambience.volume=-6;
@ambience=null;
}

In short, when you declare an object handle, you put an @ (at) sign after the variable type. You can then use this variable as if it were a standard object, the only difference being that you are referencing the original variable. When you want to change the value of your handle, for example to make it point to another object of the same type, you simply put the @ (at) sign before the variable name.
You can destroy a handle by setting its value to null. This is the object equivalent to 0. With this in mind, though you cannot manually destroy the contents of an object, a way to work around this is to set all your global objects as handles, and only use the objects themselves in functions. This way, although the objects themselves are local variables, they have global variables still referring to them. Therefore the object will always remain usable until its last handle is destroyed.

On a basic level, I understand what this is doing: it's basically showing how you can play a sound using a function and handles. My understanding right now is that a handle is just a reference to an object, but the object the handle references can change at any point. However, I'm not sure if there is a specific reason why a handle must be used when creating a function which returns an object. For example, why does play_sound start with sound@ the_sound? Perhaps this is just the way BGT does things, or maybe it makes logical sense? I dunno, I'm still trying to figure that out.

Apart from that, I think I've figured the example out, but I'm still stuck on where I would personally use handles myself. I'm sure if I were at a stage where I was ready to write complex games, I would already know, but at this stage, I don't really know and I feel like I need to before moving forward.

Something else I have been grappling with which is probably related.
I've noticed that there are limitations on when you can do certain things. For example, while you can do basic object setup globally, you can't actually use methods unless you are doing it inside a function/method. I just recently figured that out, and I'm certain this structure is a major source of my struggles.

As a test, I tried to make a player class with a move method. Getting the class to move my player left and right for a side scroller, for example, was actually easy. But when trying to figure out how I could hear footsteps, I ran into trouble. In the end, I had to settle for one of two ways, and I'm not sure if either are ideal.
Method 1. Create a global sound object for the footstep. Then inside the function which runs the game, load step.wav in that object so either it or my player class can make use of it.
Here's the code:

player you;
sound step;

class player
{
int pos = 1;
void move()
{
step.play();
pos++;
}
}

void main()
{
show_game_window ("test");
step.load("move.wav");
while (true)
{
if (key_pressed(KEY_UP))
{
you.move();
}
wait (5);
}
}

It looks a bit cluttered since I'm using my step sound three times: first time globally to set it up, second in the game function to load step.wav, and thirdly in my move method to actually play it. I have no idea if this is how you're actually supposed to write something like this, though.

Method 2. Create a sound object inside my player class, and load a sound there.
Here's the code:

player you;

class player
{
sound step;
int pos = 1;
void move()
{
step.load("move.wav");
step.play();
pos++;
}
}

void main()
{
show_game_window ("test");

while (true)
{
if (key_pressed(KEY_UP))
{
you.move();
}
wait (5);
}
}

I had to load move.wav inside my move method as this is the only place it would work. While it looks less cluttered this way since the player's sounds can fully be inside the actual player object, move.wav has to be loaded every time I move, which seems like an incredibly bad idea.

My intuition tells me handles would make sense here? But I'm only going on a hunch. I'm not feeling the Aha! moment I need to build my confidence. Maybe I'm missing something much more fundamental here. I guess I'm just in dire need of direction.

So, if you're still with me, any advice?
Thanks!

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : musicalman via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : musicalman via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : thggamer via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : thggamer via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : chrisnorman7 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : ogomez92 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : bhanuponguru via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : musicalman via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : musicalman via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : thggamer via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : camlorn via Audiogames-reflector

Reply via email to