Missile Tracking

Started by
37 comments, last by Jotaf 15 years, 2 months ago
Ran through the code with a friend last night. The problem stems from the way I try to pass the target pointer to the Guided_Missile_Launcher object. The way I want it written is so that I don't have to manually update it when the target changes. I had thought that passing the pointer (roughly: Rotatable *playertarget = *iterator) like as a pointer argument (..., Rotatable *targ) should work, but it does not update when the original pointer does. What I'm looking for is the proper way to pass the pointer.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Advertisement
So you're caching the pointer to the target? If you do that, and the original pointer changes, you need to update the cached copy. You need some kind of UpdateTarget(Rotatable* target) call that resets the cached copy.

I'm not sure why you want to cache the target in the launcher itself. The missiles should have a pointer to it's target, but the launcher only needs to know about the target when it fires a missile, so it can pass it to the missile. Caching it is just asking for trouble later, if the target is deallocated/invalidated.
the launcher's pointer to the target should be equal to the playertarget pointer (that is to say they should ultimately point to the same place. that way, when I change targets elswhere, the change should propagate on its own). That way it wouldn't require updates (which I don't give it). So I'm definitely passing the pointer incorrectly. I just haven't a clue how to properly write it to do what I want (ugh, I can get everything else working [grin]). I can post the relevant code in its entirely this evening if you think it will help.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
I think if you post the relevant code, we can help you with that.

This is just a guess, but I think you're doing the pointer thing wrong:

MissileLauncher:
-keeps a pointer to the target: eg Rotatable *m_pTarget

Rocket:
-keeps a pointer to the target: eg Rotatable *m_pTarget

If I understood you correctly, you do 'change the target elsewhere', say:
MissileLauncher::setTarget( Rotatable *pTarget ) { m_pTarget = pTarget; }


Now this is what I think you're doing wrong (if it's not the case, spare my life please):
Changing the literal value of the pointer (eg. making it point to another address) anywhere else won't change the the literal value of the pointer of your rocket. The rocket's target will still point towards the first target, but it can react on changes of the first target's position for example (because the object m_pTarget points to changes its position). You will need to update the target for the rocket as well, in order to react on changing targets.
I wouldn't recommend that approach, as you are increasing coupling between two objects. This will only give you nightmares in the future. Not to mention, you have no way of knowing when the data the pointer is pointing to gets destroyed. There are certainly other ways of having the propagation happen "automatically", using a more secure design. However, theoretically, you could pass a pointer to a pointer, and cache that instead.

Rotatable **mTarget = &target

Then, to get the active target, you just dereference it like so:
Rotatabe *curTarget = *mTarget;

Still, I seriously don't recommend this approach. You should re-design your objects to communicate through interfaces, instead of fondling each other's data in inappropriate ways ;)
I will post the code this evening (when I am on my dev machine). For now, I will try to clarify what I am doing.

I have a pointer *playertarget which points (at any given time) to the player's attack target. The player can change targets at will by pressing 't' or if the current dies. In either of these cases the object *playertarget points two changes.

The Guided_Missile_Launcher object's *target pointer needs to point to the same object as *playertarget. Once it does, as *playertarget changes (totally outside the scope of the weapons subsystem) so should *target. When a new missile object is spawned, it should point at the current target (so, it should point to what *target and *playertarget point to). If the current target dies before the missile hits it, no matter, the missiles are not designed to redirect to new targets once fired. the big thing is that all new missiles should fire towards whatever *playertarget is pointing towards at any given time.

Here's a bit of scope information

MAIN GAME (GLOBAL)
-- SHIPS SUBSYSTEM contains *playertarget
-- WEAPONS SUBSYSTEM contains Guided_Missile_Launcher and Guided_Missile, etc
-- launcher gets passed *playertarget in construction, should point *target through it.
-- Etc etc etc.

The game's core can see the public parts of these subsystems and issue commands that way, but the subsystems have no means of communication with eachother. So, ideally, updates to *playertarget should be totally transparently propagate through *target as if *target actually was *playertarget. The individual missiles don't need to care :-p
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
I hope this is no missunderstanding, but I still think you're doing the wrong thing.

float *foo = NULL;float *bar = NULL;float pi = 3.14159f;foo = &#960; // foo points to the address of pibar = foo; // bar points to the address of pi (the literal value of foo is assigned to bar)float e = 2.71f;foo = &e; // foo points to the address of ebar; // bar <b>still</b> points to the address of pi


It does matter. You jave to clarify whether you change the object, your pointer points to (eg assign the pointer another address) or just change some value inside the target class.
that is the problem I'm having. I'm not sure how to achieve what I want (as defined in my last post). I could probably shoehorn manual updates (mostly by exposing playertarget to global scope, but it would be greatly preferable not to.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.
Somewhere in your code, when you change your target (if the old one was destroyed), you could simply loop through all your rockets and replace their old target address with the new one.


I have no need to update the rocket's targets -- they don't have the "fuel" for it. But as things stand, I have to make the launchers themselves poll to check for updated targets, and I'd prefer not to. And I can't directly pass them *playertarget because of scope (which can easily be changed) and more importantly because the weapons system is supposed to be modular. It is designed so that I can install a weapon of any type on any suitable object (any ship or whatnot. The player's host object is derived from ship). If I bound the code to directly read from *playertarget, things would get very weird when I would try to use the missile launchers on enemy ships (they would target the player's enemies... aka themselves. while that would be funny, it would not be correct).

I hope I'm making myself sufficiently clear. Apologies if I am not.
There was a saying we had in college: Those who walk into the engineering building are never quite the same when they walk out.

This topic is closed to new replies.

Advertisement