ID:178583
 
Is there a way for an object to detect when a player is near it? What I am trying to do is when a player comes within 2 tiles of an object I want to output a message to the usr.

oview() or view() would work but you can't seem to use these to do what I want since I am not connecting it to a verb.

Shwn wrote:
Is there a way for an object to detect when a player is near it? What I am trying to do is when a player comes within 2 tiles of an object I want to output a message to the usr.

oview() or view() would work but you can't seem to use these to do what I want since I am not connecting it to a verb.

Three different approaches come to mind.

The first is to have the object check periodically for a person:

obj/sensor
New()
sense_loop()

proc
sense_loop()
for(var/mob/M in view(src))
M << "I see you!"
spawn(10)
sense_loop()

Cons: unless you code to account for who has been notified and when, the message will repeat as long as the person is near it.

The second is best if the object is stationary. Just define an area around the object that represents its detection range. Then define the area like this:

area/sensor
Entered(atom/movable/A)
if(istype(A, /mob))
M << "I see you!"

Cons: will conflict with any existing areas you may have defined. In this case, you could try turfs instead of an area.

The third is to do it from the mob's perspective:

mob
Move()
..()
for(var/obj/sensor/S in view())
src << "The sensor sees you!"

Cons: This has the repeated message effect much as the first, unless you code in some checks. The other problem is that this produces more overhead for the cpu, though not a lot if your moving mobs are limited in number.
In response to Skysaw
Thanks for the help, I think choice 1 is going to be my best bet. I have a street grid with street signs on each corner. As the player passes by the sign I want to output a message to them the street they are on. I thought about assigning areas but I would have to define an area for each corner and it will also confilct with existing areas. I will still have to deal with the repeating messages tho but I have an idea for that.

Thanks again
-Shwn