Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Music Controller (https://forums.graalonline.com/forums/showthread.php?t=80254)

xXziroXx 06-27-2008 02:21 AM

Music Controller
 
A script that controls what song is played, and when.


PHP Code:

//#CLIENTSIDE
function onCreated()
{
  
this.songList = new TStaticVar();
  
  
/*
    this.songList.SONGNAME = { ARG1,
      { ARG2 }
    };
    ARG1 = determines if the song should stop playing when
           the players level name isn't in ARG2.
    ARG2 = an array with the levels the song should be played
           on, or what the level name should begin with.
           
           ~~~ EXAMPLE ~~~
           
    this.songList.("My Song Name") = { false,
      { "mylevelname.nw", "mylevelname2.nw", "my_gmap_" }
    };
  */
}

function 
onPlayerEnters()
{
  
temp.songList this.songList.getDynamicVarNames();
  for (
temp.songtemp.songList) {
    
temp.data this.songList.(@temp.song);
    
temp.song temp.song ".mp3";
    
temp.file getmusicfilename();
    
    
// Is temp.data[1] the players current level?
    
if (player.level.name in temp.data[1]) {
      if (
fileExists(temp.song)) playlooped(temp.song);
    }
    
// Ah well... is the player on a gmap, or does temp.data[1]
    // contain what the players level start with?
    
else if (isonmap || onSpecialLevel(temp.data[1])) {
      for (
temp.leveltemp.data[1]) {
        if (
player.level.name.starts(temp.level)) {
          if (
fileExists(temp.song)) playlooped(temp.song);
          break;
        }
      }
    }
    
    
// Should the song play in this level?
    
if (isMusicPlaying() && temp.file.ends(temp.song)) {
      if (
temp.data[0] == true && !(player.level.name in temp.data[1]) && !onSpecialLevel()) stopMusic();
    }
  }
}

function 
onSpecialLevel(levels)
{  
  for (
temp.levellevels) {
    if (
temp.level.name.ends(".nw")
     || 
temp.level.name.ends(".graal")) continue;
    
    if (
player.level.name.starts(temp.level)) return temp.level;
  }


http://forums.graalonline.com/forums...reputation.gif As always, please, use the forums reputation system to show if you like the script or not. http://forums.graalonline.com/forums...reputation.gif

DrakilorP2P 06-27-2008 01:59 PM

As a firm believer in code readability of irrational proportions, I must object to the ambiguity of temp.data[i].

TheStan 06-27-2008 06:29 PM

Excellent script Ziro. Tho' I might not be using the ENTIRE script. I do plan on using the format you used to list your songs. :O

cbk1994 06-27-2008 06:54 PM

Quote:

Originally Posted by DrakilorP2P (Post 1399681)
As a firm believer in code readability of irrational proportions, I must object to the ambiguity of temp.data[i].

Yes, his code is horribly formatted, but it's personal preference of course.

Looks cool, I wish Graal offered more control for music, it's been brought up many times.

xXziroXx 01-30-2010 05:36 PM

Here's a new and much much better version of it:

PHP Code:

//#CLIENTSIDE
function onCreated()
{
  
this.songList = new TStaticVar();
  
  
// Music file stored locally
  
this.songList.("My Example Music.mp3") = {
    { 
"mylevel.nw""my_gmap" }
  };
  
  
// Streamed song
  
this.songList.("My Example Music 2.mp3") = {
    { 
"mylevel2.nw""my_gmap2" },
    
"http://www.myexamplewebsite.com/music/"
  
};
}

function 
onPlayerEnters()
{
  
// Get the list of songs
  
temp.songList this.songList.getDynamicVarNames();
  
  for (
temp.songtemp.songList) {
    
// Data specific to this song
    
temp.data this.songList.(@temp.song);
    
// Is the song being streamed or played locally?
    
temp.streamed = (temp.data[1] == "" false true);
    
temp.volumetype = (temp.streamed "radiovolume" "mp3volume");
    
// Song name
    
temp.songpath = (temp.streamed temp.data[1] @ temp.song temp.song);
    
temp.printname extractfilebase(temp.song);
    
// File name
    
temp.currentsong extractfilename(getmusicfilename());
    
    
// If the current song isn't supposed to play in this area,
    // check if it's currently playing and stop it, then continue
    // with the next song.
    
if (!(player.level.name in temp.data[0])
     && !
onSpecialLevel(temp.data[0])) {
      if (
temp.currentsong == temp.song) {
        
player.chat "Stopped playing \"" temp.printname "\"...";
        
stopMusic();
      }
      continue;
    }
    
    
// Is the player already playing this song?
    
if (temp.currentsong == temp.song)
      break;
    else if (
temp.currentsong != temp.song)
      
stopMusic();
    
    
// If this song isn't streamed, does the player have it?
    
if (temp.streamed)
      
temp.fileexists true;
    else
      
temp.fileexists fileExists(temp.song);
    
// If he hasn't, warn about it and then stop.
    
if (!temp.fileexists) {
      
player.chat "Couldn't play \"" temp.printname "\"! Download from Start > Install Packages > Audio/Music";
      break;
    }
    
    
// Ok, let's see if volume is enabled or not!
    
temp.volumeenabled = (makevar("$pref::audio::" temp.volumetype) == false true);
    
// If it's not, warn about it and then stop.
    
if (!temp.volumeenabled) {
      
player.chat =  "Couldn't play \"" temp.printname "\"! Make sure your" SPC temp.volumetype SPC "is turned on in F3 options!";
      break;
    }
    
    
playlooped(temp.songpath);
    
player.chat "Playing \"" temp.printname "\"...";
  }
}

function 
onSpecialLevel(levels)
{  
  for (
temp.levellevels) {
    if (
temp.level.name.ends(".nw")
     || 
temp.level.name.ends(".graal"))
      continue;
    
    if (
player.level.name.starts(temp.level))
      return 
temp.level;
  }




All times are GMT +2. The time now is 12:17 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.