Raised This Month: $51 Target: $400
 12% 

Solved TF2 get nearby players in an area


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Rushy
AlliedModders Donor
Join Date: Jul 2015
Location: Melbourne, Australia
Old 02-27-2017 , 04:31   TF2 get nearby players in an area
Reply With Quote #1

Hi I am trying to get nearby clients when a player calls for a medic, to give the players a buff if a few people are close to each other.

What I need help with is looping through the other players and getting the plugin to determine which players are in the area.

So far it only shows data from the player making the call
__________________

Last edited by Rushy; 04-23-2017 at 09:03. Reason: removed bad code
Rushy is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 02-27-2017 , 05:47   Re: TF2 get nearby players in an area
Reply With Quote #2

After comparing the two positions, store the client index in an array that holds up to MaxClients players if the client is within range.

Something like this (simplified example, some declarations omitted):

Code:
int[] clientsInArea = new int[MaxClients]; // list of nearby players, hold up to amount MaxClients
int nClients; // number of players in range
for (int i = 1; i <= MaxClients; i++) {
    GetClientAbsOrigin(i, vecOther);
    if (GetVectorDistance(vecCaller, vecOther) <= 400.0) { // make sure you're comparing two floats or two ints
        clientsInArea[nClients++] = i; // add player index to list of players in area
    }
}

// do stuff here if x amount of players are nearby
if (nClients >= NUM_PLAYERS_REQUIRED_FOR_BUFF) {
    for (int i = 0; i < nClients; i++) {
        BuffPlayer(clientsInArea[i]);
    }
}
I forgot how to do SourcePawn pre-transitional syntax, but it should be easy enough to understand.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 02-27-2017 at 05:49.
nosoop is offline
Rushy
AlliedModders Donor
Join Date: Jul 2015
Location: Melbourne, Australia
Old 02-27-2017 , 07:25   Re: TF2 get nearby players in an area
Reply With Quote #3

Thanks for the reply, I took what you said and got to this:

Code:
  // do stuff here if x amount of players are nearby
		//made it above 0 just for testing, to force it to print
		if (nClients >= 0) {
		for (int e = 0; e < nClients; e++) {
        PrintToChat(e, "test test");
		}
			
	}
  
 
}
 return Plugin_Continue;
}
It only prints out the vecposition2 stuff and does not print out the PrintToChat test message to either client
__________________

Last edited by Rushy; 04-23-2017 at 09:04.
Rushy is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 02-27-2017 , 07:39   Re: TF2 get nearby players in an area
Reply With Quote #4

You need to access the array when you're processing the players in the second for loop; the array values are the player indices.

Also, you're currently trying to print to client 0 (server?) and 1 (which may or may not be the correct player or even a connected one).

Code:
for (...) {
    if (GetVectorDistance(...)) {
        /**
         * no point in user GetClientUserId here since we're going to refer to the array in the
         * same function (i.e., not using delayed callbacks)
         * 
         * if you need to fire off any timers, just pass the client into GetClientUserId later
         */
        clientsInArea[nClients++] = i;
    }
}

if (nClients >= 0) {
    for (int e = 0; e < nClients; e++) {
        int nearbyClient = clientsInArea[e];
        PrintToChat(nearbyClient, "beep boop");
        
        // if you need to RequestFrame or CreateTimer, definitely wrap the client index with a GetClientUserId here
    }
}
For example, if clients with indices 5, 6, and 19 are in range, you'll have this after the first for loop with my above example code:

Code:
nClients = 3;

clientsInArea[0] = 5;
clientsInArea[1] = 6;
clientsInArea[2] = 19;
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 02-27-2017 at 07:49. Reason: avoid long lines today
nosoop is offline
Rushy
AlliedModders Donor
Join Date: Jul 2015
Location: Melbourne, Australia
Old 02-27-2017 , 08:10   Re: TF2 get nearby players in an area
Reply With Quote #5

True I will start it off at 1 (If the server is added in), I did have checks in the for loop IsClientInGame and if not a bot. Wouldn't that make sure the player is connected as we are looping through the array entries that checks this before putting it in?


That did the trick! It is now printing to the second client.


But with distance, there was not much documentation on the GetVectorDistance method. Do you have any experience with it?
__________________

Last edited by Rushy; 04-23-2017 at 09:05.
Rushy is offline
nosoop
Veteran Member
Join Date: Aug 2014
Old 02-27-2017 , 09:07   Re: TF2 get nearby players in an area
Reply With Quote #6

The first for loop does need those checks; just make sure you're retrieving the correct values (the client indices) in the clientsInArea array in the second for loop.

Distance is measured in game / Hammer Units. You can use cl_showpos to see your current eye position.
I'm sure you're already aware that your code snippet is for distances greater than 2HU.
__________________
I do TF2, TF2 servers, and TF2 plugins.
I don't do DMs over Discord -- PM me on the forums regarding inquiries.
AlliedModders Releases / Github / TF2 Server / Donate (BTC / BCH / coffee)

Last edited by nosoop; 02-27-2017 at 09:11.
nosoop is offline
Rushy
AlliedModders Donor
Join Date: Jul 2015
Location: Melbourne, Australia
Old 02-27-2017 , 20:21   Re: TF2 get nearby players in an area
Reply With Quote #7

Cheers that helped a lot, yeah I realized that this morning...my tired brain late last night used the wrong operator

Thank you so much for helping me out
__________________
Rushy is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:00.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode