AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Grenade Name return (https://forums.alliedmods.net/showthread.php?t=293511)

EFFx 02-03-2017 01:23

Grenade Name return
 
How can I return the grenade's name when it touch a player?

I have a method but I want to know if have any other better than mine.
Thats the code that I'm using:

PHP Code:

new const szGrenades[] =
{
    
"smokegrenade",
    
"flashbang"
}

new 
g_gGrenadeType 

PHP Code:

register_touch("player","grenade","fwd_touch")
register_clcmd("say /grenadeUsed""showLastGrenade"

PHP Code:

public showLastGrenade(id)
{
     
console_print(id,"%s",GetGrenadeType(g_gGrenadeType)) 

PHP Code:

public fwd_touch(touchedtoucher
{
    new 
id pev(toucherpev_owner)
    if(
get_user_team(id) != get_user_team(touched))
    {
        new 
GrenadeType = (get_pdata_int(toucher114) & 3)
        
        if(
GrenadeType != CSW_HEGRENADE)
        {
            if(
fGetDistance(touchertouched) <= 5.0)
            {
                
g_gGrenadeType GrenadeType
            
}
        }
    }


PHP Code:

Float:fGetDistance(const touchedPlayer, const toucherPlayer)
{
    new 
Float:fOrigin[2][3]
    
pev(touchedPlayerpev_originfOrigin[0])
    
pev(toucherPlayerpev_originfOrigin[1])
    
    new 
Float:floatDistance fOrigin[1][2] - fOrigin[0][2]
    
    return 
floatDistance


PHP Code:

GetGrenadeType(GrenadeType)
{
    switch(
GrenadeType)
    {
        case 
CSW_SMOKEGRENADEg_gGrenadeType szGrenades[0]
        case 
CSW_FLASHBANGg_gGrenadeType szGrenades[1]
    }
    return 
g_gGrenadeType



edon1337 02-03-2017 06:42

Re: Grenade Name return
 
This?
PHP Code:

stock fm_cs_get_grenade_type(index) {
    
// you can comment/remove this
    // if you are sure that the entity
    // with the given index are valid
    
if (!pev_valid(index))
        return 
0
    
    
// you can comment/remove this
    // if you are sure that the entity
    // with the given index are "grenade"
    
new classname[9]
    
pev(indexpev_classnameclassname8)
    if (!
equal(classname"grenade"))
        return 
0
    
    
if (get_pdata_int(index96) & (1<<8))
        return 
CSW_C4
    
    
new bits get_pdata_int(index114)
    if (
bits & (1<<0))
        return 
CSW_HEGRENADE
    
else if (bits & (1<<1))
        return 
CSW_SMOKEGRENADE
    
else if (!bits)
        return 
CSW_FLASHBANG
    
    
return 0



EFFx 02-03-2017 16:55

Re: Grenade Name return
 
No, as the fwd_touch says, the CSW_ prefix is already taken, i want return a string for show on console_print().

Bugsy 02-03-2017 17:58

Re: Grenade Name return
 
Are you aware that your fGetDistance() function only gets the distance of the Z axis (up and down distance)?

EFFx 02-03-2017 18:02

Re: Grenade Name return
 
No, I just copied it from my runboost. But it works.

Bugsy 02-03-2017 18:03

Re: Grenade Name return
 
Well, that's what it does. Use get_distance_f() to get actual distance.

0=X
1=Y
2=Z

EFFx 02-03-2017 18:36

Re: Grenade Name return
 
Yea, thats what I have:

PHP Code:

if(fDistance(touchertouched) <= 20.0

PHP Code:

Float:fDistance(touchertouched)
{
    new 
Float:fOrigin[2][3]
    
pev(toucherpev_originfOrigin[0])
    
pev(touchedpev_originfOrigin[1])
    
    return 
get_distance_f(fOrigin[0], fOrigin[1])


What about the string return?

Bugsy 02-03-2017 21:30

Re: Grenade Name return
 
'Yes that's what I have'. No what you have is this, unless you are saying you have fixed it?
PHP Code:

Float:fGetDistance(const touchedPlayer, const toucherPlayer)
{
    new 
Float:fOrigin[2][3]
    
pev(touchedPlayerpev_originfOrigin[0])
    
pev(toucherPlayerpev_originfOrigin[1])
    
    new 
Float:floatDistance fOrigin[1][2] - fOrigin[0][2]
    
    return 
floatDistance


To explain better: This only measures the distance going up and down. So if you were standing on the same level ground as me but I was across the map, your distance function would return 0.

Aside from your method being wrong, you also do not take into consideration the orientation of one player to the other. So if I'm standing on your head and you subtract my Z from yours, the distance will be negative (hypothetically) and it would never satisfy your distance condition. This can be fixed by getting the absolute value, abs(), of the Z1-Z2 value.

I've never returned a string from a function because I remember reading somewhere that it does not work properly. This may only be in some scenarios like returning from a native or something. With that said, I always stick to not returning a string. You can easily avoid returning a string by just returning the index to the weapon name in the array.

EFFx 02-03-2017 21:34

Re: Grenade Name return
 
I meant that I have fixed.

And I said "return a string" meaning "how can I return the smokegrenade/flashbang on the console_print()".

Bugsy 02-03-2017 21:36

Re: Grenade Name return
 
It looks messy, I'll need to see more of your plugin to fix this
PHP Code:

enum
{
    
Smoke,
    
Flash
}

GetGrenadeType(GrenadeType)
{
    switch(
GrenadeType)
    {
        case 
CSW_SMOKEGRENADEg_gGrenadeType Smoke
        
case CSW_FLASHBANGg_gGrenadeType Flash
    
}
    return 
g_gGrenadeType
}  

public 
showLastGrenade(id)
{
     
console_print(id,"%s"szGrenadesg_gGrenadeType ] )  




All times are GMT -4. The time now is 21:02.

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