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

Hook one entity button


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
MrLalatg
Junior Member
Join Date: Mar 2020
Old 08-14-2020 , 04:15   Hook one entity button
Reply With Quote #1

I've never worked with entities before and cannot find much info about them, but I suppose that the buttons on maps are entities (for example on minigame maps like mg_lego_multigames).

The thing is the creator of the map has done so when the button is pressed something happens, for example, some convars are changed and so on.

I want to know, can I somehow hook a press event of a specific button on the map to run some code? Or at least to change some cvars

Help me plz
MrLalatg is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 08-14-2020 , 11:44   Re: Hook one entity button
Reply With Quote #2

You can HookEntityOutput (for every entity of the same class):
Code:
HookEntityOutput( "func_button", "OnPressed", MyCallback ); // hooks "OnPressed" output for every entity with "func_button" classname
or HookSingleEntityOutput (for specific single entity):
Code:
HookSingleEntityOutput( iEntity, "OnPressed", MyCallback, true ); // hooks specific iEntity (index) and calls MyCallback only once (last boolean argument) when entity fires "OnPressed" output
Outputs for func_button.
__________________

Last edited by MAGNAT2645; 08-14-2020 at 11:46.
MAGNAT2645 is offline
MrLalatg
Junior Member
Join Date: Mar 2020
Old 08-14-2020 , 12:18   Re: Hook one entity button
Reply With Quote #3

Quote:
Originally Posted by MAGNAT2645 View Post
You can HookEntityOutput (for every entity of the same class):
Code:
HookEntityOutput( "func_button", "OnPressed", MyCallback ); // hooks "OnPressed" output for every entity with "func_button" classname
or HookSingleEntityOutput (for specific single entity):
Code:
HookSingleEntityOutput( iEntity, "OnPressed", MyCallback, true ); // hooks specific iEntity (index) and calls MyCallback only once (last boolean argument) when entity fires "OnPressed" output
Outputs for func_button.
How can i determine a entity index?
MrLalatg is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 08-14-2020 , 14:25   Re: Hook one entity button
Reply With Quote #4

There's a lot of methods to determine entity index.
You can use FindEntityByClassname but this can find ANY entity with specified classname (you should put some additional checks to limit your searching).
You can also retrieve entity's targetname (or HammerID) and then do a loop to find specific entity:
Code:
#define TARGETNAME "PUT TARGETNAME HERE"

char sName[50];
int iEntity = MaxClients + 1;
while ( ( iEntity = FindEntityByClassname( iEntity, "func_button" ) ) != -1 ) {
	GetEntPropString( iEntity, Prop_Data, "m_iName", sName, sizeof( sName) );
	if ( strcmp( sName, TARGETNAME ) == 0 ) {
		HookSingleEntityOutput( iEntity, "OnPressed", MyCallback );
		break;
	}
}
__________________

Last edited by MAGNAT2645; 08-14-2020 at 14:29.
MAGNAT2645 is offline
MrLalatg
Junior Member
Join Date: Mar 2020
Old 08-14-2020 , 14:55   Re: Hook one entity button
Reply With Quote #5

Quote:
Originally Posted by MAGNAT2645 View Post
There's a lot of methods to determine entity index.
You can use FindEntityByClassname but this can find ANY entity with specified classname (you should put some additional checks to limit your searching).
You can also retrieve entity's targetname (or HammerID) and then do a loop to find specific entity:
Code:
#define TARGETNAME "PUT TARGETNAME HERE"

char sName[50];
int iEntity = MaxClients + 1;
while ( ( iEntity = FindEntityByClassname( iEntity, "func_button" ) ) != -1 ) {
	GetEntPropString( iEntity, Prop_Data, "m_iName", sName, sizeof( sName) );
	if ( strcmp( sName, TARGETNAME ) == 0 ) {
		HookSingleEntityOutput( iEntity, "OnPressed", MyCallback );
		break;
	}
}
Sorry for my stupidity, but i don't really understand how can i find it.
For example i have the compiled map with the buttons, what should i do to find hammerid or anything of a specific button on a map?
MrLalatg is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 08-14-2020 , 15:26   Re: Hook one entity button
Reply With Quote #6

Quote:
Originally Posted by MrLalatg View Post
Sorry for my stupidity, but i don't really understand how can i find it.
For example i have the compiled map with the buttons, what should i do to find hammerid or anything of a specific button on a map?
You can create some tools to help yourself:
Code:
public void OnPluginStart() {
	RegConsoleCmd( "sm_info", CMD_Info );
}

public Action CMD_Info(int client, int args) {
	int iTarget = GetClientAimTarget( client, false );
	if ( iTarget != -1 ) {
		char sName[64], sClass[64];
		GetEntPropString( iTarget, Prop_Data, "m_iName", sName, sizeof( sName ) );
		if ( sName[0] == '\0' )
			strcopy( sName, sizeof( sName ), "(EMPTY)" );

		GetEntityClassname( iTarget, sClass, sizeof( sClass ) );

		if ( HasEntProp( iTarget, Prop_Data, "m_iHammerID" ) )
			PrintToChat( client, "[Entity %i] ClassName: %s | TargetName: %s | HammerID: %i", iTarget, sClass, sName, GetEntProp( iTarget, Prop_Data, "m_iHammerID" ) );
		else
			PrintToChat( client, "[Entity %i] ClassName: %s | TargetName: %s", iTarget, sClass, sName );
	}

	return Plugin_Handled;
}
In game look at the entity and type command /info into chat (or sm_info into console)
__________________
MAGNAT2645 is offline
Reply


Thread Tools
Display Modes

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 19:14.


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