Reference to Object?
BlitzMax Forums/BlitzMax Programming/Reference to Object?
| ||
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. |
| ||
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 |
| ||
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 |
| ||
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. |
| ||
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. |
| ||
Ok, gotcha. |
| ||
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. |