Reference to Object?

BlitzMax Forums/BlitzMax Programming/Reference to Object?

Torrente(Posted 2006) [#1]
In my current game, I'm trying to implement homing missiles. I'm stuck on how to have a reference to the target that they're following.
I have a type Tenemy, which is the base type for all enemies in the game, and would like each homing missiles to pick an enemy from a Tlist (which contains all TEnemy instances) as their target.

However, I'm unsure of how to do this. My original thought was to have an object pointer to the missile's target, however this apparently isn't allowed in BlitzMax.

I need the information from the target so that I can have the missile follow it around.


Please tell me if I explained it badly, I wasn't sure how to put it into words.


Gabriel(Posted 2006) [#2]
Huh? All type instances in BMax are pointers/references.

Is it not just as simple as something like this?

Type TMissile
   Field Target:TEnemy
End Type



SculptureOfSoul(Posted 2006) [#3]
Just create another object of type TEnemy, call it say 'TargetEnemy', and put it in your Missile object.

When you want to select a target for the missile, simply set Missile.TargetEnemy = GetRandomEnemyFromListFunction()

Oh, and don't forget to cast your object back to TEnemy when you grab it from the list, because a list returns everything as type object.

Anyhow, any variable of a user defined type is simply a reference to the actual object. Blitz doesn't copy by value on assignment, instead it changes the lvalues internal pointer.
e.g.
Enemy1:TEnemy = new TEnemy
Enemy2:TEnemy
Enemy1.name = "Enemy1"
Enemy2 = Enemy1 'enemy2 is now pointing to the same object as enemy 1
Enemy2.name = "Fred"
print Enemy1.name 'prints fred


Gabriel(Posted 2006) [#4]
Just create another object of type TEnemy, call it say 'TargetEnemy', and put it in your Missile object.

Er.. why create another one? Then he's tracking the wrong enemy.


SculptureOfSoul(Posted 2006) [#5]
I meant a field of type TEnemy. Just worded poorly, although I thought the next line Missile.TargetEnemy made things clear.

M'eh, it's been a long day.


Gabriel(Posted 2006) [#6]
Ok, gotcha.


Torrente(Posted 2006) [#7]
Ah, I get it now. It seems to work perfectly. I was unaware that I could just set the field equal to the instance and have it be a reference. Thanks for the help.