ID:261448
 
If a Client is in an area, how do you make it if another Client tryes to enter that area, it makes him do something.
SSJ4_Link wrote:
If a Client is in an area, how do you make it if another Client tryes to enter that area, it makes him do something.

You could loop through all the mobs in the area to see if one has a client, but that would be potentially slow. I'd give the special area a var to count how many clients are present inside.

area
var
clients = 0

Entered(O) // O entered this area
if(ismob(O)) // see if O is a mob
var/mob/M = O // make a mob alias
if(M.client)
clients++ // increase the area client count
..() // do any other Entered() procs that apply

Exited(O) // O exited this area
if(ismob(O)) // see if O is a mob
var/mob/M = O // make a mob alias
if(M.client)
clients-- // decrease the area client count
..() // do any other Exited() procs that apply

We should also change the clients count if someone logs in or out from a mob

mob
Login()
// get the area the mob is in
var/area/A = get_area()

if(A) A.clients++ // increase the client count
..() // do other Login() procs that apply

Logout()
// get the area the mob is in
var/area/A = get_area()

if(A) A.clients-- // decrease the client count
..() // do other Logout() procs that apply

proc
get_area()
// get the area the mob is in
var/atom/A = loc
while(A && !isarea(A))
A = A.loc
return A

Now, you just define the special area that makes the second client "do something"

area
special_area
Enter(O) // O tried to enter this area
if(ismob(O))
var/mob/M = O // get a mob alias to O
if(M.client && src.clients) // if the mob has a client, and the area already has clients
M.DoSomething() // call M's DoSomething() proc
return ..() // return whatever Enter() would return if it hadn't been overidden


In response to Shadowdarke
That's pretty good code, though you'd be better off with a list. With a list, you can also save the hassle of Login() and Logout() by simply rechecking mob.client for every mob in the area any time it would matter. I find player lists can become dreadful nightmares if you overstress them by doing too much at login or logout; places like that are where the worst bugs creep in.
area
var/list/players

Entered(atom/movable/A)
if(ismob(A))
var/mob/M=A
if(M.client)
UpdatePlayers()
if(!players) players=list() // initialize the list
players+=M
if(players.len>1) DoSomething(players)
Exited(atom/movable/A)
if(ismob(A))
players-=A // who cares if it has a client or not

proc/UpdatePlayers() // compensate for logout
if(!players) return
for(var/mob/M in players)
if(!M || !M.client)
players-=M
else
var/atom/L=M.loc
while(L && L!=src) L=L.loc
if(!L) players-=M // they're not even here!
if(!players.len) players=null // free up the list

Lummox JR
In response to Shadowdarke
Shadowdarke wrote:
SSJ4_Link wrote:
If a Client is in an area, how do you make it if another Client tryes to enter that area, it makes him do something.

You could loop through all the mobs in the area to see if one has a client, but that would be potentially slow. I'd give the special area a var to count how many clients are present inside.

area
var
clients = 0

Entered(O) // O entered this area
if(ismob(O)) // see if O is a mob
var/mob/M = O // make a mob alias
if(M.client)
clients++ // increase the area client count
..() // do any other Entered() procs that apply

Exited(O) // O exited this area
if(ismob(O)) // see if O is a mob
var/mob/M = O // make a mob alias
if(M.client)
clients-- // decrease the area client count
..() // do any other Exited() procs that apply

We should also change the clients count if someone logs in or out from a mob

mob
Login()
// get the area the mob is in
var/area/A = get_area()

if(A) A.clients++ // increase the client count
..() // do other Login() procs that apply

Logout()
// get the area the mob is in
var/area/A = get_area()

if(A) A.clients-- // decrease the client count
..() // do other Logout() procs that apply

proc
get_area()
// get the area the mob is in
var/atom/A = loc
while(A && !isarea(A))
A = A.loc
return A

Now, you just define the special area that makes the second client "do something"

area
special_area
Enter(O) // O tried to enter this area
if(ismob(O))
var/mob/M = O // get a mob alias to O
if(M.client && src.clients) // if the mob has a client, and the area already has clients
M.DoSomething() // call M's DoSomething() proc
return ..() // return whatever Enter() would return if it hadn't been overidden


I was gonna do this but I was afraid of bugs. But I think I'm just gonna leave this whole code out. It seems llike to much of an hassle that wasn't even a good idea in the first place.