Check if PC is nearby?
Dragonfolk2000
Member Posts: 388
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?
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?
0
Comments
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.
The distance check will work, but you might want to add refinements e.g. LineOfSightObject().
Update: this is line of sight, even if the player can't actually SEE the rats. So I need to readdress this.
In the toolset, if you filter functions for "distance", you'll see two more functions of that sort.
The Lexicon is also searchable.