Author Topic: How to handle targets?  (Read 9376 times)

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
How to handle targets?
« on: January 29, 2017, 04:32:22 PM »
When npc targets something it has to know the location of the object (often another moving object) and so get the location from the object. But sometimes objects get destroyed and if the npc is still trying to get the location it may be a null pointer waiting for it. I made a list of npcs for each object so they know who is targetting and when destroyed can message back to npcs to stop targetting them. But feels like it's not the most efficient way to store targetters, because most game objects are never targetted by npcs. How to message a npc so it knows when the target is destroyed?

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: How to handle targets?
« Reply #1 on: January 29, 2017, 06:36:09 PM »
How do you know the object you are reaching for has fallen off the table disappeared? The object does not call you back, you see that yourself and update your aim accordingly. I think it is worth to think from this perspective.

As for the purely technical side of your rather specific situation, smart pointers to the rescue? If you keep a weak pointer then it becomes easy to tell if the object is still alive.

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: How to handle targets?
« Reply #2 on: January 30, 2017, 02:10:12 PM »
you see that yourself and update your aim accordingly.

I wonder if you understood the question. All monsters can target anything, including the player and other game objects. I've started to reprogram the system so that the target becomes an object that holds a list of creatures targetting it. When a object is destoyed the target is also destroyed, and targetters get a message to stop targetting it. When the monster stops targetting (for example it dies) something it can be also removed from the target's list. The target's pointer can be kept in monster's class, because when the monster is informed about the target that's going to be destroyed the pointer is reset to zero (or flag is set for no target at the moment).

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: How to handle targets?
« Reply #3 on: January 30, 2017, 04:50:48 PM »
That does not look like a question anymore. If you already have a solution and proud of it and it even works, okay, use it. If you want to discuss something instead, please pay attention. (Well, who am I kidding here.)

So the situation is, objects may disappear abruptly so you made a list of reverse references back to actors to inform those actors about disappearences, right? And now you wonder if it is efficient to keep these references thing in every object and asking whether there is a more efficient way to notify actors that their target has expired.

I suggest to take a step back and take another look at the source of the problem: actors noticing that object they are interested in has disappeared. Reverse references is not the only way. If you keep a simple forward reference in a form of a weak_ptr in the actor to the target, your actor NPC will know that the object has disappeared right at its next update step. Without any extra machinery involved.

The passage about IRL was to hint that there is no need to anything inform anyone to notice changes. You just notice changes next time you pay attention to something and see that weak_ptr has expired the object is not where it was.

Tzan

  • Rogueliker
  • ***
  • Posts: 193
  • Karma: +0/-0
    • View Profile
Re: How to handle targets?
« Reply #4 on: January 30, 2017, 05:24:04 PM »
Before Cfyz replied I had thought the same thing.

Npc keeps the reference to the one thing it is targeting.
Then check for null reference each turn.

(Or set a flag on the target, indicating its not active anymore, for example, if it was sent back to an object pool.
Then the test each turn would be to see if its still active.)
« Last Edit: January 30, 2017, 05:27:56 PM by Tzan »

Krice

  • (Banned)
  • Rogueliker
  • ***
  • Posts: 2316
  • Karma: +0/-2
    • View Profile
    • Email
Re: How to handle targets?
« Reply #5 on: January 31, 2017, 09:03:07 AM »
a) I don't want to use smart pointers. b) How the heck weak_ptr is doing it under the hood? When you delete an object it has to be somehow notified to whatever has that object's pointer.

Cfyz

  • Rogueliker
  • ***
  • Posts: 194
  • Karma: +0/-0
    • View Profile
    • Email
Re: How to handle targets?
« Reply #6 on: January 31, 2017, 02:56:40 PM »
a) You are already trying to do something like that (automatic lifetime management) with your idea of reverse references. But smart pointers are well-tested core part of the language.
b) No one has to be notified. A weak_ptr instance shares reference counter with the original shared_ptr so you always can check if that counter has already hit zero.
Once more, there is no need to actively notify anyone. When you will need the information about target being present or not (when you finally get to updating the NPC), the information is already there in the form of an empty pointer.