So I'm trying to write a lua script to call a specific line from two separate txt files of the same length. It's a sort of joke of the day type script that needs a riddle and an answer. My problem so far is either with retrieving the data from the file or calling up the specific line. I'm very new to the script and was able to get this far in about 1 day, and have been trying for the past day or 2 just to figure out where it is failing. Could someone please take a look at this code and tell me if I'm doing something wrong that is simple or recommend a way for me to go about this differently?
function Initialize()
mtRiddle = SKIN:GetMeter("RiddleMeter")
mtAnswer = SKIN:GetMeter("AnswerMeter")
end
function Update()
RiddleFilePath = "C:\Users\Alex\Documents\Rainmeter\Skins\mayal0\@Resources\riddles.txt"
AnswerFilePath = "C:\Users\Alex\Documents\Rainmeter\Skins\mayal0\@Resources\answers.txt"
luarand = math.random(1,71)
ostime = os.date('*t')
modday = math.fmod(ostime.yday,71)+1
RiddleTodayTable = ReadFileLines(RiddleFilePath)
RiddleToday = RiddleTodayTable[modday]
AnswerTodayTable = ReadFileLines(AnswerFilePath)
AnswerToday = AnswerTodayTable[modday]
SKIN:Bang('!SetOption RiddleMeter Text '..RiddleToday)
SKIN:Bang('!SetOption AnswerMeter Text '..AnswerToday)
end
ReadFileLines = function(FilePath)
FilePath = SKIN:MakePathAbsolute(FilePath)
local File = io.open(FilePath)
if not File then
print('ReadFile: unable to open file at ' .. FilePath)
return
end
local Contents = {}
for Line in File:lines() do
table.insert(Contents, Line)
end
File:close()
return Contents
end
To avoid this I usually just use back slashes when specifying file paths in Lua since Windows will allow the use of either.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
So I've tried switching it the slashes and it didn't fix the problem. I have it set up right now to display the value of modday for the RiddleMeter and then display the what should be the correct answer string in the AnswerMeter. It's showing the test string, but not the answer. Could it be something wrong with my readfilelines function or me not calling the position of the outputted table properly? Could it be trying to find a position in the table labeled "modday" instead of the value of the variable modday?
function Initialize()
mtRiddle = SKIN:GetMeter("RiddleMeter")
mtAnswer = SKIN:GetMeter("AnswerMeter")
end
function Update()
RiddleFilePath = "C:/Users/Alex/Documents/Rainmeter/Skins/mayal0/@Resources/riddles.txt"
AnswerFilePath = "C:/Users/Alex/Documents/Rainmeter/Skins/mayal0/@Resources/answers.txt"
luarand = math.random(1,71)
ostime = os.date('*t')
modday = math.fmod(ostime.yday,71)+1
RiddleTodayTable = ReadFileLines(RiddleFilePath)
RiddleToday = RiddleTodayTable[modday]
AnswerTodayTable = ReadFileLines(AnswerFilePath)
AnswerToday = AnswerTodayTable[modday]
SKIN:Bang('!SetOption RiddleMeter Text '..modday)
SKIN:Bang('!SetOption AnswerMeter Text '..RiddleToday)
end
ReadFileLines = function(FilePath)
FilePath = SKIN:MakePathAbsolute(FilePath)
local File = io.open(FilePath)
if not File then
print('ReadFile: unable to open file at ' .. FilePath)
return
end
local Contents = {}
for Line in File:lines() do
table.insert(Contents, Line)
end
File:close()
return Contents
end
function Initialize()
RiddleTodayTable = ReadFileLines(SKIN:ReplaceVariables('#@#riddles.txt'))
AnswerTodayTable = ReadFileLines(SKIN:ReplaceVariables('#@#answers.txt'))
end
function Update()
local ostime = os.date('*t')
local modday = math.fmod(ostime.yday, 71)+1
SKIN:Bang('!SetOption', 'RiddleMeter', 'Text', RiddleTodayTable[modday])
SKIN:Bang('!SetOption', 'AnswerMeter', 'Text', AnswerTodayTable[modday])
end
ReadFileLines = function(FilePath)
FilePath = SKIN:MakePathAbsolute(FilePath)
local File = io.open(FilePath, 'r')
local Contents = {}
if File then
for Line in File:lines() do
table.insert(Contents, Line)
end
File:close()
else
print('ReadFile: unable to open file at ' .. FilePath)
end
return Contents
end
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .
That worked. Thank you very much. Is there a reason that the tables were made in the initialize function section as opposed to the update function? Also, what purpose does local serve? Am I supposed to put that before any variables that I use that aren't used elsewhere in the script?
I want to make sure I learn from my mistakes. Thank you very much for your help, I really appreciate it.
Mayal0 wrote:That worked. Thank you very much. Is there a reason that the tables were made in the initialize function section as opposed to the update function? Also, what purpose does local serve? Am I supposed to put that before any variables that I use that aren't used elsewhere in the script?
I want to make sure I learn from my mistakes. Thank you very much for your help, I really appreciate it.
Reading the files only once instead of on each update is a good idea. Means a lot less work is being done.
You should use local variables when you can. They are "faster" than global variables for whatever reason. Yes, it is for variables that you don't use anywhere else. They only exist in the scope in which you use them.
GitHub | DeviantArt | Tumblr
This is the song that never ends. It just goes on and on my friends. Some people started singing it not knowing what it was, and they'll continue singing it forever just because . . .