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

Assigning a client index to a button


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
adma
Senior Member
Join Date: Oct 2015
Old 04-12-2016 , 18:39   Assigning a client index to a button
Reply With Quote #1

Hi Sourcemod scripting,

I'm looking for a way to store a client index to a map button (func_button), with the purpose of being able to find the last player who pressed that certain button. I've tried things, which just lead to stack errors and things. I've come to the conclusion I have no idea what I'm doing.

Thanks
adma is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 04-12-2016 , 19:25   Re: Assigning a client index to a button
Reply With Quote #2

Lol.... great post...

You would want use an array for this.

On button press
--- loop array to find button index
----- if found store the userid of the pressee
----- if not found add the button index and userid

On round start
--- clear array
__________________
Neuro Toxin is offline
adma
Senior Member
Join Date: Oct 2015
Old 04-12-2016 , 21:45   Re: Assigning a client index to a button
Reply With Quote #3

Sorry for the really undetailed post. I rushed because of school, I'll be able to update in a few hours.
adma is offline
Phil25
AlliedModders Donor
Join Date: Feb 2015
Old 04-13-2016 , 00:52   Re: Assigning a client index to a button
Reply With Quote #4

You could also hook the button press when that particular button spawns and do your work in the callback, so you wouldn't have to loop through any arrays.
Phil25 is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 04-13-2016 , 02:13   Re: Assigning a client index to a button
Reply With Quote #5

I was thinking that. But if you store against a client index the last pressed button you could only store the very last button a client pressed.

If using an array like i first mentioned. If the same player pressed every button you would be able to figure this out.

Edit: I've also raised this thought with dynamic if you want to add any thoughts...
__________________

Last edited by Neuro Toxin; 04-13-2016 at 02:15.
Neuro Toxin is offline
adma
Senior Member
Join Date: Oct 2015
Old 04-13-2016 , 02:42   Re: Assigning a client index to a button
Reply With Quote #6

Okay, I'm home now. Sorry for the bad explanation. Basically what I'm looking for is a way pair a clients index with a button once they hit the button. I am very new to this type of thing. This is what I've got but it doesn't exactly work...

Code:
public OnButtonPressed(const String:output[], buttonEnt, attacker, Float:delay)
{
	decl String:entity[1024];
	new lastClientid = GetClientOfUserId(attacker); // is this necessary ?
	new button[2048]; // ??
	
	GetEntPropString(buttonEnt, Prop_Data, "m_iName", entity, sizeof(entity));
	
	if (IsClientInGame(attacker) && IsPlayerAlive(attacker))
	{
		 button[buttonEnt] = attackerID;
	}
}
Probably looks dumb but hey that's progress.

Last edited by adma; 04-13-2016 at 02:44.
adma is offline
ddhoward
Veteran Member
Join Date: May 2012
Location: California
Old 04-13-2016 , 02:52   Re: Assigning a client index to a button
Reply With Quote #7

Why all that stuff with iName? You're declaring the string, and putting something into it, but then you don't do anything with it afterwards.

If you declare your storage array within the function, then the data will all be erased once the function ends... you need to make it global, or perhaps static?

Also, where does OnButtonPressed come from?
__________________

Last edited by ddhoward; 04-13-2016 at 02:52.
ddhoward is offline
adma
Senior Member
Join Date: Oct 2015
Old 04-13-2016 , 03:04   Re: Assigning a client index to a button
Reply With Quote #8

Hi, thanks for the reply. I forgot to remove it but its for printing stuff into the chat, I'll give out what I got now. I also made the array global.
Code:
#include <sourcemod>
#include <sdktools>
#include <morecolors>

#define MAXENTITIES 2048

new bool:isListening[MAXPLAYERS + 1] = false;
new button[MAXPLAYERS + 1];

public Plugin:myinfo = 
{
  name = "Button Announce",
  author = "adma",
  description = "Announces button press in chat.",
  version = "3.0"
};

public OnPluginStart()
{
	RegConsoleCmd("sm_button", OnButtonListen, "Enables button stuff.");
	HookEntityOutput("func_button", "OnPressed", OnButtonPressed)
}

public Action:OnButtonListen(int client, int args)
{
	if (isListening[client])
	{
		isListening[client] = false;
		ReplyToCommand(client, "[SM] No longer listening for button presses.")
		return Plugin_Handled;
	}
	else
	{
		isListening[client] = true;
		ReplyToCommand(client, "[SM] Now listening for button presses");
		return Plugin_Handled;
	}
}

public OnButtonPressed(const String:output[], buttonEnt, attacker, Float:delay)
{
	decl String:entity[1024];
	new lastClientid = GetClientOfUserId(attacker);
	
	GetEntPropString(buttonEnt, Prop_Data, "m_iName", entity, sizeof(entity));
	
	if (IsClientInGame(attacker) && IsPlayerAlive(attacker) && !isListening[attacker])
	{
		button[buttonEnt] = lastClientid;
	}
	
	if (IsClientInGame(attacker) && IsPlayerAlive(attacker) && isListening[attacker])
	{
		CPrintToChat(attacker, "%N {uncommon}was the last to press button \x01%s", button[buttonEnt], entity);
	}
}
This doesn't throw any errors. The only problem is that the plugin thinks lastClientid = 0 regardless if someone has pressed the button or not.

Last edited by adma; 04-13-2016 at 03:12. Reason: Unnecessary include
adma is offline
Neuro Toxin
Veteran Member
Join Date: Oct 2013
Location: { closing the void; }
Old 04-13-2016 , 04:12   Re: Assigning a client index to a button
Reply With Quote #9

Code:
button[lastClientid] = buttonEnt;
Without using a proper array, you can only store the button against the client in the array
__________________

Last edited by Neuro Toxin; 04-13-2016 at 04:13.
Neuro Toxin is offline
adma
Senior Member
Join Date: Oct 2015
Old 04-13-2016 , 04:29   Re: Assigning a client index to a button
Reply With Quote #10

Quote:
Originally Posted by Neuro Toxin View Post
Code:
button[lastClientid] = buttonEnt;
Without using a proper array, you can only store the button against the client in the array
Thanks for the insight. This purpose I won't need to get the clients last button hit. Just only need the client that last hit a certain button. Is there any way I can store the client against the button? My goal is to enable !button, hit a button and the buttons client will print in chat. Appreciate the help .
adma 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 16:48.


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