AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Hook one entity button (https://forums.alliedmods.net/showthread.php?t=326728)

MrLalatg 08-14-2020 04:15

Hook one entity button
 
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 :cry:

MAGNAT2645 08-14-2020 11:44

Re: Hook one entity button
 
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.

MrLalatg 08-14-2020 12:18

Re: Hook one entity button
 
Quote:

Originally Posted by MAGNAT2645 (Post 2714200)
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?

MAGNAT2645 08-14-2020 14:25

Re: Hook one entity button
 
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;
        }
}


MrLalatg 08-14-2020 14:55

Re: Hook one entity button
 
Quote:

Originally Posted by MAGNAT2645 (Post 2714224)
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?

MAGNAT2645 08-14-2020 15:26

Re: Hook one entity button
 
Quote:

Originally Posted by MrLalatg (Post 2714228)
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)


All times are GMT -4. The time now is 03:54.

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