Skip to content

Check if PC is nearby?

When a creature spawns I need to check if the PC can see the creature that just spawned. Essentially the PC is trying to figure out where all these rats are coming from and there are several locations they can camp to wait and see if rats are coming from that location. So if the rats are already spawned then the PC can't learn anything. But if the PC is nearby and can observe the rats coming out then I need stuff to happen.

My problem is that I don't know how to check all nearby objects and see if PC is one of them. These rats are being spawned with a script that's running on a timer so would it be better to slip this code in there instead of the rat's onSpawn?

Comments

  • ProlericProleric Member Posts: 1,306
    Given the rule "if the rats are already spawned then the PC can't learn anything", OnSpawn is the right place.

    In single player, GetObjectSeen() using GetFirstPC() will tell you whether the PC can see the spawn or not.

    In multiplayer, cycle through the PCs using GetFirstPC() / GetNextPC().

    If you want to specify a maximum distance e.g. 20m or whatever, instead of whether the PC sees the spawn, GetDistanceBetween() the PC and OBJECT_SELF (the spawning rat) will tell you whether the PC is "nearby" or not.

    If you ever need to check all nearby objects, GetNearestObject() is the function to use. Not necessary in this special case, where the object is a PC.
  • ProlericProleric Member Posts: 1,306
    edited August 30
    On reflection, you will need to test GetObjectSeen. I'm not quite sure whether that function uses the table of creatures the PC can see, or, if so, whether it's updated on spawn. There might be a slight delay before the PC sees the rat.

    The distance check will work, but you might want to add refinements e.g. LineOfSightObject().
  • ForSeriousForSerious Member Posts: 452
    edited September 5
    Another way to do it could be: draw a trigger and check to see the the PC (or any PC) is in the area of the trigger on spawn.
Sign In or Register to comment.