- In a rhythm game, have a class that is used solely for keeping the beat.
In my games I call it the Conductor. It should have an easy function/variable that gives the song position, to be used by everything that needs to be synced to the beat. In this game for example, the Conductor has a variable called songposition which is pretty much the cornerstone of everything in the game.
The above are the variables in the Conductor class. Some are specific to my game, but the general ones that I always have are
bpm, which gives the bpm of the song
crotchet, which gives the time duration of a beat, calculated from the bpm
offset, always important due to the fact that MP3s always have a teeny gap at the very beginning, no matter what you do, which is used for metadata (artist name, song name, etc)
songposition, a variable that should be set directly from the corresponding variable on the Audio object. This varies from engine to engine, but in Unity for example, the variable to use is AudioSettings.dspTime. What I do is, in the same frame that I play the song, I record the dspTime at that moment, so then my song position variable is set on every frame as follows:
songposition = (float)(AudioSettings.dspTime – dsptimesong) * song.pitch – offset;
Aside: the song.pitch is an inbuilt variable in Unity that gives the speed the song is playing at. By incorporating it into my song position variable, I can change the playback speed and still keep everything in sync. This was used in my game to slow all the songs down 20% because I only realised after composing the music that it was too difficult.
Anyway, now that we have set up our Conductor, time to take care of the objects that need to sync to it!
I just found this online, but how would I convert this into lua code