Reading and writing files to disk - RhythmLunatic/stepmania GitHub Wiki
Copypasted from my old crappy blog that I don't use anymore
The Writing Part
In this tutorial, I will be writing a "now playing" file to be read by OBS or some other software. This sounds pointless, but it's an easy way for external software to read the current song without having to edit the source code. For example, you could create a hardware ticker and then display the song on the ticker by reading the file.
Now on with the tutorial. For the purposes of writing a "now playing" file, we should modify ScreenStageInformation, which is the screen that is shown before starting gameplay.
Open ScreenStageInformation overlay, background, underlay, etc, and find somewhere to insert code that is OUTSIDE of an ActorFrame.
First, we have to create a RageFileHandle, which is simply
local Handle = RageFileUtil.CreateRageFile();
Then you want to open a file using the handle you just created. In this case, we're writing to a file named NowPlaying.txt in the theme's root directory. (The file does not need to exist already)
local pass = Handle:Open(THEME:GetCurrentThemeDirectory().."NowPlaying.txt", 2);
The '2' in the Open argument is write mode. You can see the other modes here.
Open() returns true or false depending on if the file was successfully opened. Thus, check the result and write whatever you want:
if pass then
Handle:Write(GAMESTATE:GetCurrentSong():GetDisplayMainTitle()); --Write the title of the song to the file.
Handle:Flush(); --Flush contents to disk.
end
Handle:Close(); --Close the handle at the end.
So all in all:
local Handle = RageFileUtil.CreateRageFile();
local pass = Handle:Open(THEME:GetCurrentThemeDirectory().."NowPlaying.txt", 2);
Handle:Write(GAMESTATE:GetCurrentSong():GetDisplayMainTitle()); --Write the title of the song to the file.
Handle:Flush(); --Flush contents to disk.
end
Handle:Close(); --Close the handle at the end.
Of course, this will not clear the song at the end, but after reading this tutorial that should be up to you to do. For example, you can use a handle to blank the file in ScreenStageEvaluation.
The Reading Part
No, this example does not use RageFileManager.
Let's use Rave It Out's ScreenStageInformation overlay as an example. In Rave It Out, if a song has a msg.txt it will be displayed instead of the regular phrases. This tutorial is heavily simplified, so if you want the full RIO features check out the code and theme.
-- First, the regular phrases.
local phrases = {
"Mokou is smoking hot. Literally.",
"An osu original",
"When's Mahvel?",
"When's Melty?",
"It's Mahvel baybee!",
"Also check out Melty Blood Actress Again Current Code for SM5!",
"Also check out Pump It Up: Delta 2!",
"Also check out DanceDanceRevolution: SuperNOVA 3!",
"Also check out DanceDanceRevolution: Starlight!",
"Also check out Sushi Violation!",
"Also check out StepF2 and StepP1!",
"Also check out StepPrime!",
"Shoutouts to Midflight Digital",
"A real time music game that's hard and fast. It's too cool!",
"Are you tired of reading these yet?",
"What is a man? A miserable little pile of secrets!"
};
local message;
local song = GAMESTATE:GetCurrentSong()
-- Now we have to check if msg.txt exists.
local songmsgpath = song:GetSongDir().."msg.txt";
local songhasmsg = FILEMAN:DoesFileExist(songmsgpath)
if songhasmsg then
--File.Read(path) reads to string.
message = File.Read(songmsgpath)
else
message = phrases[math.random(#phrases)];
end;
--And now we have our message, so all we need is to display it somehow.
return Def.ActorFrame{
LoadFont("Common Normal")..{ --MESSAGE
Text=message;
InitCommand=cmd(xy,_screen.cx,SCREEN_BOTTOM-60;zoom,0.75;maxwidth,SCREEN_WIDTH;wrapwidthpixels,780;maxheight,100);
};
};
The code is public domain.