AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Entity Use (Like a button) (https://forums.alliedmods.net/showthread.php?t=273431)

redivcram 10-18-2015 17:53

Entity Use (Like a button)
 
I'm trying to make a plugin that spawns entities that behave like buttons, let's say it that way.
But whenever I spawn it and try to use it, nothing happens. (Simply stand close to it, press my use key "e" like to any other button/entity, and It's supposed to print out "Button Press", but nothing)

PHP Code:

#include <amxmodx>
#include <amxmisc>
#include <hamsandwich>
#include <fakemeta>
#include <fakemeta_util>

new g_Classname[] = "wboxexample";
new 
g_Model[] = "models/w_weaponbox.mdl";

public 
plugin_init()
{
    
register_plugin("Button Entity""1.0""redivcram");
   
    
RegisterHam(Ham_Use"func_button""EvUse"0);
   
    
register_clcmd("say /wb""CmdWB");
}

public 
plugin_precache()
{
    
precache_model(g_Model);
}

public 
CmdWB(id)
{
    new 
ent engfunc(EngFunc_CreateNamedEntityengfunc(EngFunc_AllocString"info_target"));
    
    new 
Float:origin[3];
    
fm_get_aim_origin(idorigin);
    
    
set_pev(entpev_classnameg_Classname);
    
engfunc(EngFunc_AllocString"func_button");
    
set_pev(entpev_target"weaponbox_test");
    
engfunc(EngFunc_SetModelentg_Model);
    
engfunc(EngFunc_SetSizeentFloat:{-25.0, -25.0, -25.0}, Float:{25.025.025.0});
    
set_pev(entpev_solidSOLID_BBOX);
    
engfunc(EngFunc_SetOriginentorigin);
}

public 
EvUse(entid)
{    
    if(!
is_user_alive(id))
        return 
HAM_IGNORED;
    
    new 
target[33];
    
pev(entpev_targettargetsizeof (target) - 1);

    if(
equali(target"weaponbox_test"))
    {
        
client_print(idprint_chat"Button Press");
    }
    return 
HAM_IGNORED;



redivcram 10-20-2015 15:19

Re: Entity Use (Like a button)
 
Up

milutinke 10-21-2015 06:26

Re: Entity Use (Like a button)
 
I wrote this using my mobile phone.
I have not tested it, but it should work.

Code:

#include < amxmodx >
#include < amxmisc >
#include < fakemeta >

public plugin_init( ) {
        register_plugin( "Use Entity like Button", "1.0", "Milutinke (ByM)" );
       
        register_forward( FM_EmitSound, "fw_EmitSound" );
}

public fw_EmitSound( id, iChannel, szSound[ ], Float: fVol, Float: fAttn, iFlags, iPitch ) {
        if( !is_user_alive( id ) )
                return FMRES_IGNORED;
       
        if( equal( szSound, "common/wpn_denyselect.wav" ) ) {
                if( IsAimingAtEnt( id, "Entity Name Here" ) ) {
                        //Your code here.
                }
               
                return FMRES_SUPERCEDE;
        }

        return FMRES_IGNORED;
}

stock bool: IsAimingAtEnt( id, const szEntityClassName[ ] ) {
        if( !strlen( szEntityClassName ) || !is_user_alive( id ) )
                return false;
               
        static iTarget, iBody, szClassName[ 32 ];
        get_user_aiming( id, iTarget, iBody );
        pev( iTarget, pev_classname, szClassName, charsmax( szClassName ) );
       
        if( equal( szEntityClassName, szClassName ) )
                return true;
               
        return true;
}


redivcram 10-21-2015 07:43

Re: Entity Use (Like a button)
 
Tried this:

Spoiler


I'm not even aiming at the spawned entity, the message prints anyway and the sound effect when a button is pressed doesn't play.

milutinke 10-21-2015 08:56

Re: Entity Use (Like a button)
 
I am sorry, my bad.
I have put true instead of false on last line in the following stock.

This should now work fine:
Code:

stock bool: IsAimingAtEnt( id, const szEntityClassName[ ] ) {
        if( !strlen( szEntityClassName ) || !is_user_alive( id ) )
                return false;
               
        static iTarget, iBody, szClassName[ 32 ];
        get_user_aiming( id, iTarget, iBody );
        pev( iTarget, pev_classname, szClassName, charsmax( szClassName ) );
       
        if( equal( szEntityClassName, szClassName ) )
                return true;
               
        return false;
}


redivcram 10-21-2015 09:09

Re: Entity Use (Like a button)
 
Nice... are there actually more methods of doing that? Besides when the user is aiming at the entity. If you look at the code I made above, It would actually print the message when he stands near the button & when he's aiming at it. From what I can see, It's supposed to turn the entity into a button.

milutinke 10-21-2015 09:55

Re: Entity Use (Like a button)
 
I am using old phone because my new one is dead xD
So i have Android 2.3.3 and i cant scroll the code, when i can use pc i will see it.
You can add boolean to player and in that way stop user spamming and clicking agin.
Yes there is more efficent ways but this first fell on top of my mind, i have to study so i am bored and lazy to thing about it for longer time :P

For example:
Code:

...
if( IsAimingAtEnt( id, "..." ) && !ClickedButton[ id ] ) {
//do your stuff
ClickedButton[ id ] = true;
}

//if you want to turn it to switch
else if( IsAimingAtEnt( id, "..." ) && ClickedButton[ id ] ) {
//agin do your stuff
ClickedButton[ id ] = false;
}


redivcram 10-21-2015 18:50

Re: Entity Use (Like a button)
 
Wel that's easy to make myself lol
This makes the entity usable only when aimed at... If I'm using a timer with that players would cheat lol.. they can press it from anywhere they can see it

milutinke 10-22-2015 08:45

Re: Entity Use (Like a button)
 
You can use: entity_range( iEntityId, iPlayer )
That will solve the problem.

Example:
Code:

if( entity_range( iEntityId, iPlayer ) <= 30 ) {
//Activate button
}


jimaway 10-22-2015 14:50

Re: Entity Use (Like a button)
 
Code:
new ent = engfunc(EngFunc_CreateNamedEntity, engfunc(EngFunc_AllocString, "func_button"));

this should do it imo

and remove these lines
Code:
set_pev(ent, pev_classname, g_Classname);     engfunc(EngFunc_AllocString, "func_button");

and if you still can't get it working you can take a look at this plugin, there's a functionality that lets you create buttons in there


All times are GMT -4. The time now is 22:18.

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