2011-07-27 05:52:25

Hello all.
I have again, started to look at the BGT scripting language. And if you've read my last post, I'm actually getting it a little more!
For example, different variable declarations, low-level array use, the creation of functions, objects and classes, along with inheritance, dictionaries, and the integrated objects and functions in BGT! But I still have one question for those who don't mind answering.
Can someone explain to me what a handle is and what it means to pass a handle? For example, robot @ whatever. I don't really know what or how they are used.
And also, can someone explain to me what the resize and set functions are used for in arrays or dictionaries?
I would greatly appreciate it if someone could answer these questions. This is what is troubling me the most.
Thanks for all who help.

2011-07-27 08:37:05 (edited by CAE_Jones 2011-07-27 08:40:54)

Handles are something of a memory address.
Every object you create is stored somewhere in memory. When you change the value of that object, or send it to a function, the engine can either copy the object, or send a reference to where it is stored. That's where handles come in.

I'm going to write an example that is more an analogy... if it confuses things further just ignore it:

 string[] data; // In this example, this array is similar to memory addresses in ram.

 // This function is actually pointless, because it copies the string, so the changes made to it are lost as soon as the function is finished.
 void do_stuff(string str) {
 str=str + " --- stuff done --- ";
 }

 // This function will have more of a noticeable effect, because it's given the address to the string rather than a copy of the data:
 void do_stuff2(uint index) {
 data[index]=data[index] + " --- stuff done --- ";
 }

That example isn't meant to use handles at all; it's using arrays to try and show how handles work.

I've done most of my programming before BGT in java and javascript, which always pass around handles instead of copies, so it's been a little difficult for me to get use to using handles as well. (C and C++ use something similar, though).

Basically, whenever you're dealing with objects, you almost always want to use handles.

For instance, say you have two classes: a Room and a Character class. The Room keeps an array of characters so you can easily tell who's in the room.
You'd want the array to be an array of handles instead of Character objects, though, otherwise you won't be able to do anything with those characters.

class Room {

 Character@[] characters;
 // <-- Methods and stuff go here... -->
 }

Here's the part that took longer for me to figure out...
The way you create objects in BGT has been a bit of a stumblingblock for the way I generally do things, so I tend to add a function to generate objects, similar to the new keyword in java. (If anyone could offer a better way to do the following example, I think it'd help me, too!)
I'm also going to use resize in this example. Resize basically changes the array's length. Since you have remove and insert methods, this may seem a little pointless, but hopefully this will help show a possible use.

 Room@[] map;

 Room@ newRoom() {Room ret(); return ret;}

 // Let's preten the character class has a constructor that takes name, and x and y position as parameters.
 Character@ newCharacter(string n, int x, int y) {Character ret(n, x, y); return ret;}

 void build_room_array(uint size) {

 // We have no idea how big the map array is when this function is called, so let's make sure it's the size we want:
 map.resize(size);

 // Now, let's add Rooms.
 // To make this example a little more interesting, let's add as many characters as the index of the room we're on.

 for(uint i=0; i<map.length(); i++) {
 // Since we used resize, you could have said i<size instead of i<map.length().
 @map[i]=newRoom();

 // Now, let's add characters.
 map[i].characters.resize(i);
 for(uint j=0; j<i; j++) {

 @(map[i].characters[j])=newCharacter("Char " + j + ".0", random(0, 10), random(0, 10));
 // I'm not sure if you need the parentheses around map[i].characters[j], but it feels safer to use them.
}// Character loop.
 }// Map loop.

 }// Function.

I hope that wasn't too confusing.

Another use for handles is when you're comparing objects. For example, let's say you have a Character object to represent the player, and this Character is included in the array with all of the non-player characters in a room. I think this is how you'd find the player in the array:

 int player_index(Room@ r) {
 int index=-1;
 for(uint i=0; i<r.characters.length(); i++) {
 if(@(r.characters[i])==@player) index=i;
 }
 return index;
 }

I don't think I'm good at explaining things... hopefully that was useful.

[edit] Heh, I didn't realize the forum automatically filtered the at sign. Just know that all of those [ a-t ]s in my examples are supposed to be the at sign.[/edit]

看過來!
"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.