Re: Using handles in bgt somewhat confuse me

This part is indeed bizarre and confusing and most languages tend to just simplify it by making all object references into handles by default.

I think it's easier to figure out by trial and error. Does the script do something weird? Try throwing in at signs.

Here are the cases I can think of OTTOMH:

(Eh, remember that the forum sometimes filters the at sign into [a-t] to help protect people from email spammers.)

Comparing objects

You'll usually want to check two handles to see if they're pointing to the same object, rather than creating your own opCmp method to determine if two objects are clones of each other.
So if you have two object handles A and B, you'd compare them like so:
if(@a==b)
or
if(@a==@b)
(It's kinda confusing if you do or don't need the at on the right side of the equals. -_- )

You can usually get away with just putting the at sign at the beginning of the co mparison. I think the compiler use to give you warnings that it was converting the right side for you, but people did this so often out of convenience that Philip disabled that warning.

There are two particular cases that do this in opposite ways.
If you want to check if a handle is null, just do this:
if(@A==null)

However, if you want to check a handle inside an object's methods, say, if you want to see if an attack hits every target on the map and you want to skip the object that's performing the attack, you might do this:
if(@target==@this)
I think this is the only case where you need handles on both sides.

Assignment

If you want to assign a reference to a handle (and you usually do, unless they're temporary objects or fixed global objects), you have two ways of doing it.
If you're doing this when declaring the object, you would do it like so (my_class is a class):
my_class temp;
my_cl ass@ handle=temp;

Or, if you're assigning to a preexisting handle:
my_class temp;
@handle=temp;

(Actually, I don't know if it matters whether there is a space between the at sign and the variable name or not.)


You'll inevitably want objects to remember other objects. For example, a map class containing an array of enemies or items, and maybe references to neighboring maps. You might define such a class like so:

class map {
enemy@[] enemies;
item@[] items;
map@ north=null, south=null, east=null, west=null;

// (More properties, Constructors, etc.)

void setNorth(map@ onNorth) {
   [ a-t ](this.north)=onNorth;
   // Or, you can just say @north=onNorth.
}

void addEnemy(enemy@ e) {
   this.enemies.insert_last(e);
}

bool hasNorth() {
   return [ a-t ](this.north)!=null;
}

}

You can't have object properties inside classes; they must be handles. Which basically means throw an at sign into the declarations, assignments, and comparisons.


This leads to one complaint I have about BGT/angelscript, which is the lack of a built-in way to initialize handles.
In java and _javascript_ and many other languages, you can do this:
handle=new my_class();
In Python, you can do this:
handle=my_class()

In BGT, you need to create a temporary object, and set your handle to point toward it. Since I use handles for nearly everything, this is so needlessly tedious that I usually write my own functions that work like the new command.

my_class@ new_my_class() {
  my_class ret;
  return ret;
}

Then I just say
@handle=new_my_class();
whenever I need a new one.
Yes, that is indeed even more tedious if you use multiple constructors.

Oh, and dictionaries. If you keep objects in dictionaries, it will break if you don't use a handle to retrieve it. I can never remember how this works and just copy from the couple times I did it right. I think it works like:
my_class@ result;
dict.get(key, @result);

(Actually, I got confused and copied from the time I got it to work, again.)

_______________________________________________
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : Omar Alvarado via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Victorious via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : sneak via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : sneak via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Omar Alvarado via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : severestormsteve1 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Lucas1853 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : sneak via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Kyleman123 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector

Reply via email to