Raised This Month: $ Target: $400
 0% 

random array


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Acid07
Member
Join Date: May 2009
Old 07-21-2009 , 10:18   random array
Reply With Quote #1

i would like to be able to make an array of up to 3 sounds and randomly choose one to play when i give the command to. could anyone give me help this will be my first proper plugin ive attempted.
Acid07 is offline
olj
Veteran Member
Join Date: Jun 2009
Old 07-21-2009 , 17:12   Re: random array
Reply With Quote #2

Well, since i managed to make an array of CVARS, i think you can easily make an array of sounds and use some randomize function to invoke them. Im noob myself at coding, but that code worked perfectly for CVARS array.

PHP Code:
new Handle:MobArray[3];

public 
OnPluginStart()
{
MobArray[0] = FindConVar("z_mega_mob_size");   //     put 
MobArray[1] = FindConVar("z_mob_spawn_max_size");// sounds 
MobArray[2] = FindConVar("z_mob_spawn_min_size");    //  here

Just learn how to get sounds and find how to use randomizing. Anyway i think that more experienced coders will give you something more valuable.
olj is offline
paegus
Senior Member
Join Date: Nov 2004
Location: Extreme low earth orbit
Old 07-22-2009 , 09:32   Re: random array
Reply With Quote #3

use GetRandomInt() and EmitSoundToAll() like so..
Code:
// ... #define MAXSOUNDS 3 // ... const String:szSounds[MAXSOUNDS][] = {  "path/to/sound.wav",  "path/to/another/sound.mp3",  "more/paths/to/more/sounds.wav" } // ... public Action:cmd_playsound(client, args) {  //...  EmitSoundToAll(szSounds[GetRandomInt(0,MAXSOUNDS-1)]) // -1 because arrays start from 0 instead of 1.  //... } //...
__________________
Live and learn or die and teach by example.
Plugins Mine | Hidden:SourceMod

Last edited by paegus; 07-22-2009 at 14:44. Reason: oops
paegus is offline
Dragonshadow
BANNED
Join Date: Jun 2008
Old 07-22-2009 , 11:44   Re: random array
Reply With Quote #4

Quote:
Originally Posted by paegus View Post
use GetRandomInt() and EmitSoundToAll() like so..
Code:
// ... #define MAXSOUNDS 3 // ... static const String:szSounds[MAXSOUNDS][] = { "path/to/sound.wav",
"path/to/another/sound.mp3",
"more/paths/to/more/sounds.wav" } // ... public Action:cmd_playsound(client, args) { //... EmitSoundToAll(szSounds[GetRandomInt(0,MAXSOUNDS-1)]) // -1 because arrays start from 0 instead of 1. //... } //...

I'm doing something similar, just with strings, may I ask why you use static const instead of new? (I've read the wiki but I still don't get statics nor consts)
Dragonshadow is offline
p3tsin
Senior Member
Join Date: Sep 2005
Location: Finland
Old 07-22-2009 , 12:15   Re: random array
Reply With Quote #5

Quote:
Originally Posted by Dragonshadow View Post
I'm doing something similar, just with strings, may I ask why you use static const instead of new? (I've read the wiki but I still don't get statics nor consts)
The wiki page is pretty straight forward. Although its quite possible you got confused because of improper use of the static keyword in the last post. Theres no reason to use static global variables in the main plugin file.

Const is obviously short for constant, it simply makes you unable to modify the values after variable declaration.

Quote:
Originally Posted by wiki
It is a good idea to use const in array parameters if you know the array won't be modified; this can prevent coding mistakes.
Back to the original topic. Dont forget to precache and add the sound files to the download table if youll be using EmitSoundToAll. The precaching part can be left out if youre using the "play" client command.
__________________
plop
p3tsin 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 11:45.


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