ConvertFloatToSeconds Stock (Useful for Songs)

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1229
Gender:
United States of America

ConvertFloatToSeconds Stock (Useful for Songs)

Post by Archive-Poster »

Today, I was making a Song Player and my array of Songs had a dimension just for the music length, most of people don't know the exact length of a song in seconds so they write it in a 'float' form (Example 3:15), the way YouTube displays it. So I was in need of a stock that would Convert that 'float' length to seconds but couldn't find any, that's why I'm posting this stock here so other's can find what they need.

3:15

3 = Number of minutes.
15 = Number of seconds.

Stock:

Code: Select all

ConvertFloatToSeconds( const Float:fTime )
{
    new iSeconds, iMinutes, szExtraSeconds[ 1 ], iExtraSeconds, szString[ 5 ];

    float_to_str( fTime, szString, charsmax( szString ) );

    szExtraSeconds[ 0 ] = szString[ 2 ];
    add( szExtraSeconds, charsmax( szExtraSeconds ), szString[ 3 ] );

    iExtraSeconds = str_to_num( szString[ 2 ] );

    iMinutes = floatround( fTime, floatround_floor );

    iSeconds = iMinutes * 60;

    return iSeconds += iExtraSeconds;
} 
The reason I needed this stock was that to make the song more relaxable by lowering the volume of the game sounds when the music started and letting the player feel the music. So I needed this stock to create a task to be executed when music would end and increase the game volume of the player again.

Usage of the stock:

Code: Select all

#define MUSIC_LENGTH 3.15

set_task( float( ConvertFloatToSeconds( MUSIC_LENGTH ), "OnTaskEndSong", id, _, _ ) );
Test Logs:

Code: Select all

L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 3.14 | New Converted Value: 194
L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 0.26 | New Converted Value: 26
L 11/22/2017 - 16:52:21: [DEBUG] Old Float Value: 3.06 | New Converted Value: 186

Who is online

Users browsing this forum: No registered users and 0 guests