It is currently April 20th, 2024, 8:58 am

attempt to index global 'file'(a nil value)

Discuss the use of Lua in Script measures.
comic3344
Posts: 13
Joined: November 13th, 2012, 4:17 am

attempt to index global 'file'(a nil value)

Post by comic3344 »

a problem make my skin not work

the code:

Code: Select all

function Initialize()
       
end -- function Initialize

function Update()
         local file
         file = io.open("A.txt","r")
         wordsline = file:read()
         SKIN:Bang('!SetOption MeterStrMW Text '..'\" '..wordsline..' \"')
         file:close()
end -- function Update
please help me ~ :-( thank you~~
User avatar
Kaelri
Developer
Posts: 1721
Joined: July 25th, 2009, 4:47 am

Re: attempt to index global 'file'(a nil value)

Post by Kaelri »

The problem is here:

Code: Select all

file = io.open("A.txt","r")
File paths in Lua are not automatically relative to the skin folder, the way regular path options in Rainmeter are. You'll need to specify the location explicitly. We added a built-in MakePathAbsolute function to do this. Example:

Code: Select all

FilePath = SKIN:MakePathAbsolute('A.txt')
file = io.open(FilePath, 'r')
Or, if you really want to be compact:

Code: Select all

file = io.open(SKIN:MakePathAbsolute('A.txt'), 'r')
comic3344
Posts: 13
Joined: November 13th, 2012, 4:17 am

Re: attempt to index global 'file'(a nil value)

Post by comic3344 »

it 's ok now~ thank you ~

but why it still doesn't work when i use a complete path as,

"D:\DATA\Rainmeter\skin\A.txt" ?
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: attempt to index global 'file'(a nil value)

Post by jsmorley »

comic3344 wrote:it 's ok now~ thank you ~

but why it still doesn't work when i use a complete path as,

"D:\DATA\Rainmeter\skin\A.txt" ?
the "\" character is reserved in Lua as an "escape" character, so you would need "D:\\DATA\\Rainmeter\\skin\\A.txt"
comic3344
Posts: 13
Joined: November 13th, 2012, 4:17 am

Re: attempt to index global 'file'(a nil value)

Post by comic3344 »

oh~ i see ~ thank you~ :D