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

String help!


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
bl1nk123
Junior Member
Join Date: Jun 2008
Old 07-22-2008 , 13:26   String help!
Reply With Quote #1

Ok, basically i want to play a random song from a list. I was thinking that you have the string like so
string = "song1.wav,song2.wav,song3.wav..."
Now i want to beable to slit that string and store all the values to an array so i can do like SongList[random number]. How can i split a string at every ","?

Examples would be wonderful instead of just a function
bl1nk123 is offline
DJ Tsunami
DJ Post Spammer
Join Date: Feb 2008
Location: The Netherlands
Old 07-22-2008 , 14:26   Re: String help!
Reply With Quote #2

ExplodeString
__________________
Advertisements | REST in Pawn - HTTP client for JSON REST APIs
Please do not PM me with questions. Post in the plugin thread.
DJ Tsunami is offline
Greyscale
SourceMod Plugin Approver
Join Date: Dec 2007
Location: strYoMommasHouse[you];
Old 07-22-2008 , 14:40   Re: String help!
Reply With Quote #3

Example

Code:
new String:random[32*3] = "bl1nk123, DJ Tsunami, Greyscale"; // created 32 slots of memory for each of the 3 choices
new String:randomarray[3][64];
ExplodeString(random, ",", randomarray, 3, 64);

new randint = GetRandomInt(0, 2);
PrintToServer("The winner is %s", randomarray[randint]);



__________________

Last edited by Greyscale; 07-22-2008 at 14:44.
Greyscale is offline
XARIUS
SourceMod Donor
Join Date: May 2008
Location: Atlanta, GA
Old 07-22-2008 , 14:56   Re: String help!
Reply With Quote #4

Wrote this really simple one real quick as a test. I'd never tried anything like that before

PHP Code:
#include <sourcemod>
 
public Plugin:myinfo =
{
    
name "My First Plugin",
    
author "Me",
    
description "My first plugin ever",
    
version "1.0.0.0",
    
url "http://www.sourcemod.net/"
};
 
new 
String:mysongs[256]
new 
String:songs[12][64];
new 
songsfound;
new 
randomsong;

public 
OnPluginStart()
{
  
mysongs "song1.mp3,song2.mp3,song3.mp3,song4.mp3,song5.mp3"
  
RegAdminCmd("sm_testsongs"Command_TestSongsADMFLAG_GENERIC);
}

public 
Action:Command_TestSongs(clientargs)
{
    
songsfound 0;
    
randomsong 0;
    
songsfound ExplodeString(mysongs","songs1264);
    if (
songsfound 2)
    {
      
PrintToChatAll("Playing Song: %s"songs[randomsong]);
      return 
Plugin_Handled;
    } else {
      
randomsong GetRandomInt(0,songsfound 1)
      
PrintToChatAll("Playing Song: %s"songs[randomsong]);
      return 
Plugin_Handled;
    }

Far more efficient ways of writing it, but this seems to work for example purposes
XARIUS is offline
bl1nk123
Junior Member
Join Date: Jun 2008
Old 07-22-2008 , 15:06   Re: String help!
Reply With Quote #5

Thanks guys!! Very quick responses!

XARIUS your script will only support 12 songs right
bl1nk123 is offline
XARIUS
SourceMod Donor
Join Date: May 2008
Location: Atlanta, GA
Old 07-22-2008 , 15:55   Re: String help!
Reply With Quote #6

Quote:
Originally Posted by bl1nk123 View Post
Thanks guys!! Very quick responses!

XARIUS your script will only support 12 songs right
Right, you could easily make that higher

new String:songs[24][64]; <-- 24 songs.

Last edited by XARIUS; 07-22-2008 at 16:18.
XARIUS is offline
Kigen
BANNED
Join Date: Feb 2008
Old 07-22-2008 , 20:27   Re: String help!
Reply With Quote #7

Quote:
Originally Posted by XARIUS View Post
Right, you could easily make that higher

new String:songs[24][64]; <-- 24 songs.
If you want dynamically allocated space you can move into adt_arrays. They're a whole lot of fun. However, if your going to statically declare what songs are available wouldn't it be better to do:

Code:
new String:songs[][] = { "song1.mp3", "song2.mp3", "song3.mp3" };
?
Kigen is offline
XARIUS
SourceMod Donor
Join Date: May 2008
Location: Atlanta, GA
Old 07-22-2008 , 21:04   Re: String help!
Reply With Quote #8

Quote:
Originally Posted by Kigen View Post
If you want dynamically allocated space you can move into adt_arrays. They're a whole lot of fun. However, if your going to statically declare what songs are available wouldn't it be better to do:

Code:
new String:songs[][] = { "song1.mp3", "song2.mp3", "song3.mp3" };
?
I haven't used ADT arrays before, however I assumed that the OP was probably looking for something along the lines of a big convar or string in a config file somewhere, so that song/filenames could be modified without recompiling.

T'was only an example plugin

EDIT: And since yer being all helpful, go check out my GetClientMenu thread and see if you can't figure that one out for me.. lol
XARIUS 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 20:02.


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