AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   How do I use client_cmd(spk) properly? (https://forums.alliedmods.net/showthread.php?t=332708)

Agent3554 05-28-2021 09:00

How do I use client_cmd(spk) properly?
 
I want to play random sound in each different conditions, but it constantly calls for new RandomSound[64], I don't know if that was a proper way to call each of them

Making each audio groups for each condition, one condition play random sound within its group
PHP Code:

new g_WhizSounds[][] = 

    
"misc/whizz1.wav",
    
"misc/whizz2.wav",    
    
"misc/whizz3.wav",
    
"misc/whizz4.wav"
}

new 
g_SnapSounds[][] = 

    
"misc/snap1.wav",
    
"misc/snap2.wav",
    
"misc/snap3.wav",
    
"misc/snap4.wav"
}

new 
g_ThudSounds[][] = 

    
"misc/thud.wav"


Not sure if it's possible to pre-cache all of them at once with just a single 'for'
PHP Code:

public plugin_precache()
{
    for (new 
0sizeof(g_WhizSounds); ++i)
    {    
        
precache_sound(g_WhizSounds[i]);
    }
    for (new 
0sizeof(g_SnapSounds); ++i)
    {    
        
precache_sound(g_SnapSounds[i]);
    }
    for (new 
0sizeof(g_ThudSounds); ++i)
    {    
        
precache_sound(g_ThudSounds[i]);
    }


This is where it gets messy
PHP Code:

if (flDistance get_pcvar_float(gs_thuddist) || !fm_is_ent_visible(idtarget))
{
    if (
flDistance get_pcvar_float(gs_snapdist) || !fm_is_ent_visible(idtarget))
    {
        new 
RandomSound[64]
        
formatex(RandomSoundcharsmax(RandomSound), "%s"g_SnapSounds[random(sizeof(g_SnapSounds))]) 
        
client_cmd(target"spk %s"RandomSound)
        continue;
    }
    new 
RandomSound[64]
    
formatex(RandomSoundcharsmax(RandomSound), "%s"g_ThudSounds[random(sizeof(g_ThudSounds))]) 
    
client_cmd(target"spk %s"RandomSound)
    continue;
}
else if (
flDistance get_pcvar_float(gs_whizdist) || !fm_is_ent_visible(idtarget))
{
    continue;
}
new 
RandomSound[64]
formatex(RandomSoundcharsmax(RandomSound), "%s"g_WhizSounds[random(sizeof(g_WhizSounds))]) 
client_cmd(target"spk %s"RandomSound

Notice the constant use of "new RandomSound[64]" and "formatex()", it looks un-optimizing to me, but I don't know how do I make it simpler, not even sure if calling them on each condition really helps

I tried putting "RandomSound[64]" before the conditional operators, but I fear I'll have some issues with "formatex()"

Is there another way of using 'spk' each audio group in an optimized way?

CrazY. 05-28-2021 09:28

Re: How do I use client_cmd(spk) properly?
 
You don't need formatex.
Code:

new RandomSound[64]
        formatex(RandomSound, charsmax(RandomSound), "%s", g_SnapSounds[random(sizeof(g_SnapSounds))])
        client_cmd(target, "spk %s", RandomSound)

:arrow:
Code:

        client_cmd(target, "spk %s", g_SnapSounds[random(sizeof(g_SnapSounds))])
As for the loop, you did right. There is no workaround.

jimaway 05-28-2021 09:31

Re: How do I use client_cmd(spk) properly?
 
Code:
client_cmd(target, "spk %s", g_SnapSounds[random(sizeof(g_SnapSounds)-1)])

no idea why you're using formatex to copy the string in the first place

Agent3554 05-28-2021 09:42

Re: How do I use client_cmd(spk) properly?
 
Quote:

Originally Posted by CrazY. (Post 2748053)
You don't need formatex.
Code:

new RandomSound[64]
        formatex(RandomSound, charsmax(RandomSound), "%s", g_SnapSounds[random(sizeof(g_SnapSounds))])
        client_cmd(target, "spk %s", RandomSound)

:arrow:
Code:

        client_cmd(target, "spk %s", g_SnapSounds[random(sizeof(g_SnapSounds))])
As for the loop, you did right. There is no workaround.

Quote:

Originally Posted by jimaway (Post 2748054)
Code:
client_cmd(target, "spk %s", random(sizeof(g_SnapSounds)-1))

no idea why you're using formatex to copy the string in the first place

Thank you very much!


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

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