1

I've been coming back to BGT every now and then while learning other languages, and have found my knowledge of others helping me understand some of the more vague parts of the manual. Most of my experience has been with Python, though I do have some experience with C++, but nothing yet that seems to help me understand the bit about BGT handles. I couldn't find much in the manual, so thought I'd throw it out here. Can someone explain exactly what they are, what they do, how they're used, and why they're used? Thanks in advance!

Los Angeles Based musician, blogger, and programmer.
https://artistibarra.com/

Thumbs up

2 (edited by Hijacker 2018-04-10 09:14:30)

Hi,

i'm not surprised that you don't know about handles when coming from Python, since Python does all the handle stuff for you already. I'm a bit surprised that you don't know them from the C++ region though. Anyway, here's a bit of explanation.

Any variable contains data, data which needs to be somewhere within the RAM, data which can grow in size. An integer variable with 32 bits is 4 bytes in size, which isn't that much, thats why you usually don't use integer handles that much. But there are variables which can actually grow rather large over time. Consider reading a file into a string which is multiple megabytes in size. The string will have exactly the same size, probably even more, due to null termination and what else. Whenever you call a function which uses this string and doesn't use handles, the compiler will create a copy of this string just to be used within this function. Copying is a rather time-consuming task, especially if the data which needs to be copied is huge.
And now consider classes which contain multiple of those strings, each time you call a function with them as parameter they will get copied over, and those copies will be recycled as soon as the function terminates.
To prevent the compiler from copying huge amounts of data where it isn't actually needed, some inteligent guys invented handles. A handle simply knows the location of specific data inside the RAM, and what type the data is which you can find there. Thats all. That results in a fixed handle size, no matter what data you put into the variable. The handle size differs from OS to OS, but its usually something between 4 and 16 bytes.
OK, now you can, instead of copying data around in-memory, simply code a function which doesn't take the data itself, but a handle which shows the location of the data instead, because this handle is much smaller in size than the data itself. The function can then follow the handle towards the data and process it just like before.
There are even more possible situations in which handles can become useful, like writing functions which return more than just one value, or just-in-place modification of data without the need of a return value, but those are more advanced and can be found on several handle (or pointer) tutorials on the net.
Well, in BGT the thing works like this:

void print_string([email protected] data)
{
  // bgt does the dereferencing for us already, which is kinda confusing at times, but you'll probably get the hang of it
  alert("handle printer", data);
}
void main()
{
  string data = "i'm quite a small string, but even i am already bulkier than my handle.";
  [email protected] data_handle = @data;
  print_string(@data);
  // and because bgt always dereferences variables for us, we can access data_handle just like a usual string
  // so we need to reference it as well
  print_string(@data_handle);
  // it will print the exact same string twice, but in fact, its only stored in memory once
  // and thanks for the smart bgt interpreter, we can even do this:
  print_string(data);
  // since the interpreter knows that print_string() accepts a pointer, it will automatically and silently create a reference for us
}

Hope that helped.
Best Regards.
Hijacker

Thumbs up +2

3

This is basically references and passing by reference in C++.

I don’t believe in fighting unnecessarily.  But if something is worth fighting for, then its always a fight worth winning.
check me out on Twitter and on GitHub

Thumbs up

4

Thanks so much! I've only very recently started on C++, so have only gotten as far as if statements and now starting onn loops. Haven't yet gotten to what would be comparable to this. Just to make sure I understand, here's the same example with my notes as to what I think I understand is happening. Please correct me if I'm wrong.

// Creates a function that takes a handle to a string as a parameter.
void print_string([email protected] data)
{
  // Prints the alert with the string for which the handle that was passed to it points to.
  alert("handle printer", data);
}
void main()
{
  string data = "i'm quite a small string, but even i am already bulkier than my handle.";
  // Not too sure here, but assuming it creates a handle that points to the previous data handle?
  [email protected] data_handle = @data;
  // Calls the print_string function and passes it a handle to the string data.
  print_string(@data);
  // Again, not sure, but assuming it calls the function again, passing it the handle that points to the handle of string data?
  print_string(@data_handle);
  // Again not sure, but guessing it creates a handle to string data?
  print_string(data);
}

Thanks again for all the help!

Los Angeles Based musician, blogger, and programmer.
https://artistibarra.com/

Thumbs up

5

I'm not quite sure why, but generally speaking, the only time you need the @ sighn on the right side of an = is when comparing (ex, if(@[email protected]), but @a=b).

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up

6

I've spent some time playing with this. While trying this out with strings, I get a compilation error saying that object handles are not supported for this type. Am I missing something?

Los Angeles Based musician, blogger, and programmer.
https://artistibarra.com/

Thumbs up

7

I'm not sure that it works with strings in bgt. I don't think it works with vectors, either. And not for primitives. So mostly for dictionaries, sounds, classes you create, that sort of thing.

Some of my games
Keep up to date by following @Jeqofire on twitter!
Ear Ninja?

Thumbs up +1

8

Thanks all for the help. Got another question though, which I believe may be related to this. I've started playing with classes, but having trouble accessing an instance of a class declared in one function from another function. I'm guessing this can be done by using handles? I now understand the general idea of handles, but am still unclear as to how exactly they're implemented, such as how they're declared and manipulated as far as the BGT syntax goes.

Los Angeles Based musician, blogger, and programmer.
https://artistibarra.com/

Thumbs up