Eval A String In Corona?

Hej,

I am 1 week into Corona and I am absolutely loving it, everything works like a charm.

But today I got a question and it seems that it is not really possible, but before I throw away all my code I just wanted to ask, whether someone knows anything about eval a string.

So i tried to write a saveToFile module which i could call like this : saveToFile ( { “obj.x” , “200” } )

function saveToFile (  thisTable  )

                local path = system.pathForFile( “data.txt”, system.DocumentsDirectory )                

                local file = io.open( path, “w+b” )

                local writeString = ("""…thisTable[1]…"", "…thisTable[2])

                end

              

                file:write( writeString )          

                io.close( file )

                

                writeTable = nil

                writeString = nil

end

So it writes everything to a file and it’s great, I am having great fun. But when I try to resume the game and set the obj to its previously stored position I dont quite can figure out how to get it to work.

My attempt was:

eval ( thisTable[i]…"="… thisTable[i+1])

but since eval is not working in corona I am stuck.

If maybe someone knows the answere, I would be very delighted.

Thank you

Philip

Hi there,

Glad you’re enjoying working in Corona.

I see what you’re trying to do, but I think there’s an easier way to approach it than evaluating a string.  Instead, take a look at the JSON library (you can find it in the API docs).  It’s a simple way of converting a Lua table into JSON, which is a format you can save to a file as text, and then back from JSON to a Lua table.  It should make your saving/loading much easier.

  • Andrew

Hej Andrew,

WOW that’s even easier than I thought!

1000x thanks for this one!

I love the support of the community :slight_smile:

Hi there,

Glad you’re enjoying working in Corona.

I see what you’re trying to do, but I think there’s an easier way to approach it than evaluating a string.  Instead, take a look at the JSON library (you can find it in the API docs).  It’s a simple way of converting a Lua table into JSON, which is a format you can save to a file as text, and then back from JSON to a Lua table.  It should make your saving/loading much easier.

  • Andrew

Hej Andrew,

WOW that’s even easier than I thought!

1000x thanks for this one!

I love the support of the community :slight_smile: