Do I have a reference problem? can't load file with name from a table.

Hi

I think I read an answer to this on a blog which I can’t find anymore - A GREAT START :frowning:

I have a table which has been populated with file names within the App.

t is the table
picname = t.Pictures[i]
print (picname) --this prints (image.jpg

picture = display.newImage(picname,system.ResourceDirectory)
– picture = display.newImage(“image.jpg”,system.ResourceDirectory)

The file exists and is loaded with the last line. However it won’t load with the 2nd to last line, even though it is printed in the line above. I have tried storing the file name as both image.jpg and “image.jpg”

I believe this is a referencing issue with Lua - I might have the wrong terminology but in essence it won’t load as picname is a reference within Lua not the filename or something too programmy for me to fully understand
Anyone care to help or shed some light on a solution. Thanks ! [import]uid: 137150 topic_id: 34182 reply_id: 334182[/import]

local t = {} -- Builds the table "t" t.pictures = {} -- you must have this line or the table won't work t.pictures[1] = "image.jpg" local picname = t.pictures[1] local picture = display.newImage(picname)

That code should work. The trouble is more that

a. is the filename exactly correct?
b. is the image in a folder?
c. system.ResourceDirectory means it’s in the root directory of corona with main.lua. And I’m not sure you even need to specify that…have you tried without it?
d. If you’re not sure picname is working, add a print() statement to test it just before it is used. [import]uid: 41884 topic_id: 34182 reply_id: 135940[/import]

Are you sure your picture name doesn’t have any white space at the end?

try doing

[lua]print (picname…“test”)[/lua]

it should out put image.jpgtest. not image.jpg test. [import]uid: 147305 topic_id: 34182 reply_id: 135951[/import]

Ah that’s it the name has a new line in there!

I removed it with picname = string.sub(picname,1,string.len(picname)-1)

it works, is there something more elegant?

Next job is to find out why it’s storing the newline character, when it converts the data file into the table.

Thanks for all your help, should have noticed it didn’t print correctly. [import]uid: 137150 topic_id: 34182 reply_id: 135955[/import]

I ended up using the same thing you did when I was loading some text from a file that occasionally loaded a square character at the end of the line on Nooks. I couldn’t think of a more elegant solution, just different ones. In the end it has been working fine for months and is only 1 extra line of code so I have just left it.

[import]uid: 147305 topic_id: 34182 reply_id: 135956[/import]

local t = {} -- Builds the table "t" t.pictures = {} -- you must have this line or the table won't work t.pictures[1] = "image.jpg" local picname = t.pictures[1] local picture = display.newImage(picname)

That code should work. The trouble is more that

a. is the filename exactly correct?
b. is the image in a folder?
c. system.ResourceDirectory means it’s in the root directory of corona with main.lua. And I’m not sure you even need to specify that…have you tried without it?
d. If you’re not sure picname is working, add a print() statement to test it just before it is used. [import]uid: 41884 topic_id: 34182 reply_id: 135940[/import]

Are you sure your picture name doesn’t have any white space at the end?

try doing

[lua]print (picname…“test”)[/lua]

it should out put image.jpgtest. not image.jpg test. [import]uid: 147305 topic_id: 34182 reply_id: 135951[/import]

Ah that’s it the name has a new line in there!

I removed it with picname = string.sub(picname,1,string.len(picname)-1)

it works, is there something more elegant?

Next job is to find out why it’s storing the newline character, when it converts the data file into the table.

Thanks for all your help, should have noticed it didn’t print correctly. [import]uid: 137150 topic_id: 34182 reply_id: 135955[/import]

I ended up using the same thing you did when I was loading some text from a file that occasionally loaded a square character at the end of the line on Nooks. I couldn’t think of a more elegant solution, just different ones. In the end it has been working fine for months and is only 1 extra line of code so I have just left it.

[import]uid: 147305 topic_id: 34182 reply_id: 135956[/import]