Raised This Month: $ Target: $400
 0% 

Entity Use (Like a button)


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-18-2015 , 17:53   Entity Use (Like a button)
Reply With Quote #1

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 is offline
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-20-2015 , 15:19   Re: Entity Use (Like a button)
Reply With Quote #2

Up
redivcram is offline
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 10-21-2015 , 06:26   Re: Entity Use (Like a button)
Reply With Quote #3

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;
}
milutinke is offline
Send a message via Skype™ to milutinke
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-21-2015 , 07:43   Re: Entity Use (Like a button)
Reply With Quote #4

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.
redivcram is offline
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 10-21-2015 , 08:56   Re: Entity Use (Like a button)
Reply With Quote #5

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;
}
milutinke is offline
Send a message via Skype™ to milutinke
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-21-2015 , 09:09   Re: Entity Use (Like a button)
Reply With Quote #6

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.
redivcram is offline
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 10-21-2015 , 09:55   Re: Entity Use (Like a button)
Reply With Quote #7

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

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;
}
milutinke is offline
Send a message via Skype™ to milutinke
redivcram
Veteran Member
Join Date: Jul 2014
Location: Serbia
Old 10-21-2015 , 18:50   Re: Entity Use (Like a button)
Reply With Quote #8

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
redivcram is offline
milutinke
AlliedModders Donor
Join Date: Jun 2012
Location: Serbia
Old 10-22-2015 , 08:45   Re: Entity Use (Like a button)
Reply With Quote #9

You can use: entity_range( iEntityId, iPlayer )
That will solve the problem.

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

Last edited by milutinke; 10-22-2015 at 08:47.
milutinke is offline
Send a message via Skype™ to milutinke
jimaway
Heeeere's Jimmy!
Join Date: Jan 2009
Location: Estonia
Old 10-22-2015 , 14:50   Re: Entity Use (Like a button)
Reply With Quote #10

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

Last edited by jimaway; 10-22-2015 at 14:53.
jimaway 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 22:18.


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