AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Problem with "register_touch" (https://forums.alliedmods.net/showthread.php?t=312080)

LithuanianJack 11-15-2018 09:52

Problem with "register_touch"
 
Hello. I want to do that player who touch christmas tree would see a message, but I did something's wrong happened in my code. Can anyone help me?

PHP Code:

#include < amxmodx >
#include < amxmisc >
#include < fakemeta >
#include < cstrike >
#include < engine >
#include < hamsandwich >

new const MODEL[ ] = "models/xmas/xmas_tree.mdl";

new 
g_szConfigFile128 ];

public 
plugin_init( ) {
    
register_plugin"Xmas shit""1.0""xPaw & Juice" );
    
    
register_clcmd"tree_spawn",  "CmdSpawnTree",   ADMIN_MAP );
    
register_clcmd"tree_remove""CmdSpawnRemove"ADMIN_MAP );
    
register_clcmd"say /tree""CmdTreeMenu" );
    
    
register_event"CurWeapon""Weather""be" );
    
    
register_touch("env_tree","*","fwd_touch")
}

public 
fwd_touch(w_box,other){
     
    
client_print(other,print_chat,"Touched")
     
}

ShowTreeMenuid )
{
    new 
menu menu_create("Tree Menu""TreeMenu");
    
    
menu_additemmenu"Create Tree""1"ADMIN_MAP );
    
menu_additemmenu"Delete Tree""2"ADMIN_MAP );
    
menu_additemmenu"Save Menu""3"ADMIN_MAP );
    
    
menu_displayidmenu );
}

ShowSaveMenuid )
{
    new 
menu menu_create"Save Tree Menu""SaveMenu" );
    
    
menu_additemmenu"Load Saved Trees""1"ADMIN_MAP );
    
menu_additemmenu"Delete All Trees""2"ADMIN_MAP );
    
menu_additemmenu"Save All Trees^n""3"ADMIN_MAP );
    
menu_additemmenu"Back""4"ADMIN_MAP );
    
    
menu_displayidmenu );
}

public 
TreeMenuidmenuitem )
{
    if( 
item == MENU_EXIT )
    {
        
menu_destroy(menu);
        return;
    }
    
    new 
_accessinfo[2], callback;
    
menu_item_getinfo(menuitem_accessinfosizeof(info) - 1__callback);
    
menu_destroy(menu);
    
    switch( 
str_to_num(info) )
    {
        case 
1:
        {
            
client_cmdid"tree_spawn" );
            
ShowTreeMenu(id);
        }
        case 
2:
        {
            
client_cmdid"tree_remove" );
            
ShowTreeMenuid );
        }
        case 
3:
        {
            
ShowSaveMenuid );
        }
    }
}

public 
SaveMenuidmenuitem )
{
    if( 
item == MENU_EXIT )
    {
        
menu_destroy(menu);
        return;
    }
    
    new 
_accessinfo[2], callback;
    
menu_item_getinfo(menuitem_accessinfosizeof(info) - 1__callback);
    
menu_destroy(menu);
    
    switch( 
str_to_num(info) )
    {
        case 
1:
        {
            
DeleteAllTrees( );
            
LoadTrees( );
            
ShowSaveMenuid );
        }
        case 
2:
        {
            
DeleteAllTrees( );
            
ShowSaveMenuid );
        }
        case 
3:
        {    
            
SaveTrees( );
            
ShowSaveMenuid );
        }
        case 
4:
        {
            
ShowTreeMenuid );
        }
    }
}

public 
DeleteAllTrees( ) {
    new 
iEntity = -1;
    while( (
iEntity engfunc(EngFunc_FindEntityByStringiEntity"classname""env_tree")) )
    {
        
engfunc(EngFunc_RemoveEntityiEntity);
    }
}

public 
LoadTrees( ) {
    new 
iFile fopeng_szConfigFile"rt" );
    
    if( !
iFile )
        return;
    
    new 
Float:vOrigin], x16 ], y16 ], z16 ], szDatasizeof) + sizeof) + sizeof) + ];
    
    while( !
feofiFile ) ) {
        
fgetsiFileszDatacharsmaxszData ) );
        
trimszData );
        
        if( !
szData] )
            continue;
        
        
parseszDatax15y15z15 );
        
        
vOrigin] = str_to_float);
        
vOrigin] = str_to_float);
        
vOrigin] = str_to_float);
        
        
CreateTreevOrigin );
    }
    
    
fcloseiFile );
}

public 
Weatherid ) {
    if( 
is_user_aliveid ) ) 
        
client_cmdid"cl_weather 3" );
}

public 
plugin_precache( ) {
    
precache_modelMODEL );
    
create_entity"env_snow" );
}

public 
plugin_cfg( ) {
    new 
szMapName32 ];
    
get_mapnameszMapName31 );
    
strtolowerszMapName );
    
    
formatexg_szConfigFile127"addons/amxmodx/data/trees" );
    
    if( !
dir_existsg_szConfigFile ) ) {
        
mkdirg_szConfigFile );
        
        
formatg_szConfigFile127"%s/%s.txt"g_szConfigFileszMapName );
        
        return;
    }
    
    
formatg_szConfigFile127"%s/%s.txt"g_szConfigFileszMapName );
    
    if( !
file_existsg_szConfigFile ) )
        return;
    
    
LoadTrees( );

}

public 
CmdSpawnTree( const id, const iLevel, const iCid ) {
    if( !
cmd_accessidiLeveliCid) )
        return 
PLUGIN_HANDLED;
    
    new 
Float:vOrigin];
    
entity_get_vectoridEV_VEC_originvOrigin );
    
    
CreateTreevOrigin )
    
    return 
PLUGIN_HANDLED;
}

public 
CmdSpawnRemove( const id, const iLevel, const iCid ) {
    if( !
cmd_accessidiLeveliCid) )
        return 
PLUGIN_HANDLED;
    
    new 
Float:vOrigin], szClassName10 ], iEntity = -1iDeleted;
    
entity_get_vectoridEV_VEC_originvOrigin );
    
    while( ( 
iEntity find_ent_in_sphereiEntityvOrigin100.0 ) ) > ) {
        
entity_get_stringiEntityEV_SZ_classnameszClassName);
        
        if( 
equalszClassName"env_tree" ) ) {
            
remove_entityiEntity );
            
            
iDeleted++;
        }
    }
    
    if( 
iDeleted )
        
SaveTrees( );
    
    
console_printid"[AMXX] Deleted %i trees.%s"iDeletediDeleted == " You need to stand in tree to remove it" "" );
    
    return 
PLUGIN_HANDLED;
}

public 
CmdTreeMenu(id)
{
    if( !
access(idADMIN_MAP) ) return PLUGIN_HANDLED;
    
ShowTreeMenu(id);
    
    return 
PLUGIN_HANDLED;
}

CreateTree( const Float:vOrigin] ) {
    new 
iEntity create_entity"info_target" );
    
    if( !
iEntity )
        return 
0;
    
    
entity_set_stringiEntityEV_SZ_classname"env_tree" );
    
entity_set_intiEntityEV_INT_solidSOLID_NOT );
    
entity_set_intiEntityEV_INT_movetypeMOVETYPE_NONE );
    
    
entity_set_sizeiEntityFloat:{ -1.0, -1.0, -1.0 }, Float:{ 1.01.036.0 } );
    
entity_set_originiEntityvOrigin );
    
entity_set_modeliEntityMODEL );
    
    
drop_to_flooriEntity );
    
    return 
iEntity;
}

SaveTrees( ) {
    if( 
file_existsg_szConfigFile ) )
        
delete_fileg_szConfigFile );
    
    new 
iFile fopeng_szConfigFile"wt" );
    
    if( !
iFile )
        return;
    
    new 
Float:vOrigin], iEntity;
    
    while( ( 
iEntity find_ent_by_classiEntity"env_tree" ) ) > ) {
        
entity_get_vectoriEntityEV_VEC_originvOrigin );
        
        
fprintfiFile"%f %f %f^n"vOrigin], vOrigin], vOrigin] );
    }
    
    
fcloseiFile );



Kushfield 11-15-2018 11:19

Re: Problem with "register_touch"
 
You can't touch entities with SOLID_NOT state - they can be passed as if they didn't exist, it's just a visual object.

LithuanianJack 11-15-2018 12:29

Re: Problem with "register_touch"
 
Quote:

Originally Posted by Kushfield (Post 2624170)
You can't touch entities with SOLID_NOT state - they can be passed as if they didn't exist, it's just a visual object.

So what I have to do? How I can make not “visual object”?

Kushfield 11-15-2018 12:41

Re: Problem with "register_touch"
 
Try the other possible values for pev_solid until you reach your goal.
http://www.amxmodx.org/api/hlsdk_con...v-solid-values
I believe SOLID_TRIGGER would still keep it passable but also trigger touch when passed, but I'm not sure if that's what you're looking for.

Natsheh 11-15-2018 15:45

Re: Problem with "register_touch"
 
Its possible to define touch in a non solid entities with constantly checking if there is a player nearby or inside using entity think or simply just change its solid type to trigger solid

LithuanianJack 11-16-2018 03:07

Re: Problem with "register_touch"
 
Quote:

Originally Posted by Kushfield (Post 2624184)
Try the other possible values for pev_solid until you reach your goal.
http://www.amxmodx.org/api/hlsdk_con...v-solid-values
I believe SOLID_TRIGGER would still keep it passable but also trigger touch when passed, but I'm not sure if that's what you're looking for.

Quote:

Originally Posted by Natsheh (Post 2624207)
Its possible to define touch in a non solid entities with constantly checking if there is a player nearby or inside using entity think or simply just change its solid type to trigger solid

I tried SOLID_TRIGGER, but text prints only when player inside the tree (in the center) and text spams chat with 10-15 messages. How to fix it? And I tried SOLID_BBOX, but it is still "transparent", can walk through.

HamletEagle 11-16-2018 06:13

Re: Problem with "register_touch"
 
Quote:

Originally Posted by LithuanianJack (Post 2624247)
I tried SOLID_TRIGGER, but text prints only when player inside the tree (in the center) and text spams chat with 10-15 messages. How to fix it? And I tried SOLID_BBOX, but it is still "transparent", can walk through.

Of course it gets printed multiple times, touch is called as long as you are touching the object, it's not a one time thing.

LithuanianJack 11-16-2018 06:18

Re: Problem with "register_touch"
 
Quote:

Originally Posted by HamletEagle (Post 2624260)
Of course it gets printed multiple times, touch is called as long as you are touching the object, it's not a one time thing.

I understand this. How to make one time print?

Kushfield 11-16-2018 10:24

Re: Problem with "register_touch"
 
It gets printed only in the center because thats how big you have set the entity size (entity_set_size( iEntity, Float:{ -1.0, -1.0, -1.0 }, Float:{ 1.0, 1.0, 36.0 } ); )
This means the entity size is 1 unit in each direction from the center (except upwards, which is 36)

If you want to print the text just once, you need to create some variable that stores if players have already got the message or not, then check and set that variable's value in the touch callback. Touch gets called on every frame where the object is touched.

LithuanianJack 11-16-2018 12:10

Re: Problem with "register_touch"
 
Quote:

Originally Posted by Kushfield (Post 2624311)
It gets printed only in the center because thats how big you have set the entity size (entity_set_size( iEntity, Float:{ -1.0, -1.0, -1.0 }, Float:{ 1.0, 1.0, 36.0 } ); )
This means the entity size is 1 unit in each direction from the center (except upwards, which is 36)

If you want to print the text just once, you need to create some variable that stores if players have already got the message or not, then check and set that variable's value in the touch callback. Touch gets called on every frame where the object is touched.

Thanks for your explanation, but I can’t do this because I’m beginner. Can you write simple code? I need an example to learn


All times are GMT -4. The time now is 20:33.

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