Welcome to Open Carnage

A resource for Halo Custom Edition and MCC modding, with unique means of rewarding content creation and support. Have a wander to see why we're worth the time! - EST. 2012

Sign in to follow this  
Followers 0
Nickster5000

Music Player

clua_version = 2.04;

set_callback("tick", "OnTick");
set_callback("command", "OnCommand");
set_callback("map load", "OnMapLoad");
--set_callback("","");

musicPlayer = false; --this controls whether the music player is off or on

skipSong = false; --this allows the player to skip a song in the playlist

songCount = 6; --amount of songs in the playlist

--used for array access

minSongs = 0; 
maxSongs = songCount - 1;

isPlaying = false; --checks to see if a song is playing or not. Controlled by tick count.

tickCount = 0; --used to determine if a song is over or not.

songSelection = nil; --used for array access

SONGS = --playlist
{
    [0] = " twisted_metal\\music\\loops\\dragula ",
    [1] = " twisted_metal\\music\\loops\\gentlemenStartYourWeapons ",
    [2] = " twisted_metal\\music\\loops\\objectIdentified ",
    [3] = " twisted_metal\\music\\loops\\raceToDestruction ",
    [4] = " twisted_metal\\music\\loops\\ready2Die ",
    [5] = " twisted_metal\\music\\loops\\readySetDestroy ",
}

--to calculate song lengths: 
-- given a song length of 3:15 as an example.
-- 1. Multiply 3 * 60 (since there are 180 seconds in 3 minutes)
-- 2. add + 15 (since there are 15 additional seconds in the song
-- 3. multiply the entire result by 30 (since there are 30 ticks in one second)
-- RESULT: (3 * 60 + 15) * 30 for a 3:15 long song.

SONGLENGTHS = --lengths of each song in the playlist. They align with their respective slots in SONGS
{
    [0] = (4 * 60 + 37) * 30, --8310
    [1] = (3 * 60 + 23) * 30, --6090
    [2] = (3 * 60 + 9) * 30, --5670
    [3] = (3 * 60 + 17) * 30, --5910
    [4] = (3 * 60 + 50) * 30, --6900
    [5] = (3 * 60 + 37) * 30, --6510
    
}

function OnTick()

    if musicPlayer then --if player wants music

            if isPlaying == false then --if a song is not playing
                --pick a random song to play,
                math.randomseed(os.time());
                songSelection = math.random(minSongs,maxSongs);
                --and play the song
                playMusic(songSelection, 1);
                isPlaying = true;
            else --if a song is playing
                if tickCount >= SONGLENGTHS[songSelection] or skipSong == true then --if the song has ended or the player wants to skip the song
                    --stop the music.
                    tickCount = 0;
                    playMusic(songSelection, 0);
                    isPlaying = false;
                    skipSong = false;
                else --if the song has not ended
                    --increment the tick count. 
                    tickCount = tickCount + 1;
                    --console_out(tickCount);
                end
            end
    else --if the player does not want music
            for i = 0, maxSongs do
               playMusic(i, 0); --safely stop all songs from playing.
            end
            isPlaying = false;

    end

end

function playMusic(songSelection, toggle) --playMusic is a utility function
        
        if toggle == 1 then
            execute_script("sound_looping_start"..SONGS[songSelection].."none 1");
        else
            execute_script("sound_looping_stop"..SONGS[songSelection]);
        end
end

function OnCommand(command)
    if command.sub(command,0,4) == "play" then --if the command is play
        musicPlayer = true; --turn on the music player
        return false
    elseif command.sub(command,0,4) == "stop" then --if the command is stop
        musicPlayer = false; --turn off the music player
        return false;
    elseif command.sub(command,0,4) == "skip" and musicPlayer == true then --if the command is skip
        skipSong = true; --skip the song
        return false;
    else
        return true;
    end
end

A music player that I made for Chimera.

 

Hopefully someone will find it useful! 

Share this post


Link to post
Share on other sites

Tiddy-bits:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0
  • Recently Browsing   0 members

    No registered users viewing this page.