Raised This Month: $51 Target: $400
 12% 

Solved About EmitSoundToClient()


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
reid123455
Junior Member
Join Date: May 2015
Old 03-24-2019 , 05:44   About EmitSoundToClient()
Reply With Quote #1

Hello everyone

Sorry for my bad English first

I am a new one for writing plugin

This time, I try to write a plugin for playing music

I want the music can be played when every rounds and warmup starting in CS:GO

But I guess I don't really know how to use HookEvent and EmitSoundToClient() function

No any music is played when the plugin working

Here is my source code
PHP Code:
/*include head file*/
#include <sourcemod>
#include <soundlib>
#include <sdktools>

/*define file path*/
#define WarmUpMusicFile "sound/misc/test/warmup.mp3"
#define RoundMusicFile1 "sound/misc/test/round1.mp3"

#pragma semicolon 1

public Plugin myinfo =
{
    
name "Round music",
    
author "Reid",
    
description "Play music in every rounds.",
    
version "0.0.1",
    
url ""
};

public 
OnPluginStart()
{
    
/*command*/
    
RegConsoleCmd("sm_volume"Command_Volume"Set music volume.");
    
    
    
/*hook event*/
    
HookEvent("player_spawn"OnPlayerSpawnEventHookMode_Post);
    
HookEvent("round_start"OnRoundEventHookMode_Post);
}

public 
OnMapStart()
{
    
PrecacheSound(WarmUpMusicFile);
    
PrecacheSound(RoundMusicFile1);
}

public 
Action OnPlayerSpawn(Event event, const char[] namebool dontBroadcast)
{
    
float g_WarmUpTime = (FindConVar("mp_warmuptime")).FloatValue;
    
    
CreateTimer(g_WarmUpTimeTimer_WarmUpMusicevent.GetInt("userid"));
}

public 
OnRound(Event event, const char[] namebool dontBroadcast)
{
    
float g_RoundTime = (FindConVar("mp_roundtime")).FloatValue;
    
    
CreateTimer(g_RoundTimeTimer_RoundMusic);
}

public 
Action Timer_WarmUpMusic(Handle timer)
{
    for (
int i 1<= MaxClientsi++) 
    { 
        
EmitSoundToClient(i"misc/test/warmup.mp3");
    }
}

public 
Action Timer_RoundMusic(Handle timer)
{
    for (
int i 1<= MaxClientsi++) 
    { 
        
EmitSoundToClient(i"misc/test/round1.mp3");
    }
}

public 
Action Command_Volume(int clientint volume)
{
    
//
    

There is no any compile error and log error
Can anyone help me for fixing and finishing this?

Thanks

Last edited by reid123455; 03-28-2019 at 14:20. Reason: problem solved
reid123455 is offline
adma
Senior Member
Join Date: Oct 2015
Old 03-24-2019 , 06:18   Re: About EmitSoundToClient()
Reply With Quote #2

1. PrecacheSound does not need "sounds/" in the path
2. You need to AddFileToDownloadsTable the custom sounds. Unlike PrecacheSound and EmitSoundToClient, you need "sounds/" in the path
3. You are creating timers every time someone spawns, and then in the timer callback you are looping every player and playing the sound. This means if 10 people spawn, there will be 10 timers, so every player will have the sound played to them 10 times.
4. You NEED a IsClientInGame() check in your for (int i = 1; i <= MaxClients; i++) loops before doing anything with the index. EmitSoundToClient() will throw an error if you give it an index of a player who is not in-game
5. You pass the client userid in CreateTimer(g_WarmUpTime, Timer_WarmUpMusic, event.GetInt("userid")) but your callback does not use this. Your callback needs to be like public void Timer_WarmUpMusic(Handle timer, int userid) { //... }
6. Be consistent and use transitional syntax. E.g. public OnMapStart() -> public void OnMapStart()
adma is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 03-24-2019 , 06:21   Re: About EmitSoundToClient()
Reply With Quote #3

AddFileToDownloadsTable("sound/misc/test/warmup.mp3");
AddFileToDownloadsTable("sound/misc/test/round1.mp3");
farawayf is offline
Nexd
BANNED
Join Date: Dec 2013
Location: Hungary
Old 03-26-2019 , 04:14   Re: About EmitSoundToClient()
Reply With Quote #4

What about
PHP Code:
#include <emitsoundany> 
Then
PHP Code:
EmitSoundToClientAny(client“any/kind/of/sound/music.mp3”); //without the sound/ folder 
Or EmitSoundToAllAny ?
And instead of PrecacheSound you can use PrecacheSoundAny
Nexd is offline
impossible_cc
Senior Member
Join Date: Sep 2018
Location: Ukraine
Old 03-26-2019 , 06:44   Re: About EmitSoundToClient()
Reply With Quote #5

Quote:
Originally Posted by Nexd View Post
What about
PHP Code:
#include <emitsoundany> 
Then
PHP Code:
EmitSoundToClientAny(client“any/kind/of/sound/music.mp3”); //without the sound/ folder 
Or EmitSoundToAllAny ?
And instead of PrecacheSound you can use PrecacheSoundAny
As I know it is not needed anymore
__________________
impossible_cc is offline
NanoC
Veteran Member
Join Date: Jan 2016
Location: Argentina
Old 03-27-2019 , 20:06   Re: About EmitSoundToClient()
Reply With Quote #6

You can just use
EmitSoundToAll("folder/sound_file.mp3");

And that sound will be played for everyone. If you want to reproduce a sound just for a client, do:
EmitSoundToClient(client, "folder/sound_file.mp3");
__________________
NanoC is offline
Send a message via Skype™ to NanoC
reid123455
Junior Member
Join Date: May 2015
Old 03-28-2019 , 11:00   Re: About EmitSoundToClient()
Reply With Quote #7

Sorry for very very late reply

Finally, the sounds can be played after I add AddFileToDownloadsTable() function

And I've done a lot of different trial these days, but I'm still confusing about HookEvent timing

Sometime warmup music played after warmup was end

Sometime round music played with warmup music

Totally, my plugin still has no progress for now

Last edited by reid123455; 03-28-2019 at 11:01.
reid123455 is offline
farawayf
Senior Member
Join Date: Jan 2019
Old 03-28-2019 , 13:09   Re: About EmitSoundToClient()
Reply With Quote #8

Try this

PHP Code:


public Action OnPlayerSpawn(Event event, const char[] namebool dontBroadcast

    new 
client_id GetEventInt(event"userid");
    new 
client GetClientOfUserId(client_id);
    
    new 
Float:g_WarmUpTime GetConVarFloat(FindConVar("mp_warmuptime"));
     
    
CreateTimer(g_WarmUpTimeTimer_WarmUpMusicclient); 


public 
OnRound(Event event, const char[] namebool dontBroadcast

    new 
Float:g_RoundTime GetConVarFloat(FindConVar("mp_roundtime"));
     
    
CreateTimer(g_RoundTimeTimer_RoundMusic); 


Last edited by farawayf; 03-28-2019 at 13:10.
farawayf is offline
reid123455
Junior Member
Join Date: May 2015
Old 03-28-2019 , 14:19   Re: About EmitSoundToClient()
Reply With Quote #9

Thanks for all replys, I really appreciate

I finally know how to use timer correctly xd

But I still don't know why so many Events can't work

I can only use player_spawn for warmup music and round_start for round music

But the timing is the same when warmup is fired

So I use a global valuable for counting round number to solve this
reid123455 is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Ukraine on fire
Old 03-29-2019 , 21:28   Re: About EmitSoundToClient()
Reply With Quote #10

Maybe, use ready plugin? https://forums.alliedmods.net/showthread.php?t=315145
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch & FreeZone)
[My plugins] [My tools] [GitHub] [Articles] [HiJackThis+] [Donate]
Dragokas 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 00:56.


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