🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Understanding a Lua File (Sol C++)

Started by
7 comments, last by LedMar 3 years, 3 months ago

Hello,

I just have a quick question because I'm trying to parse a Tiled Editor export to a Lua file using Sol3 for C++.

The beginning of the file starts like this:

return {
version = "1.4",
luaversion = "5.1",
tiledversion = "1.4.1",
orientation = "orthogonal",
renderorder = "right-down",
width = 32,
height = 20,

…..

}

I'm still trying to figure out how to read stuff using Sol3, but I can't seem to access “return” to get the data from it. I don't know that much Lua, and doing a major brush up, but I know the basics and at first, I thought it might have been a table, but there is no ‘=’ sign (return = { }). I'm not sure what to classify this to use the proper method of finding the variables.

Hopefully, someone can clear that up for me, otherwise, I'll eventually figure it out!

Thanks!

Advertisement

‘return’ means the same thing in Lua as in C++: it returns a value. Here the value that it returns is a table.

Ah okay,

Thanks! So it is a table. Well, that helps. I must have been reading the table wrong then.

@a light breeze

So let me ask you this? Since it's returning a table and it has no name, what would be doing to store it into a name to read it?

In the Sol library, they have a convenient way of just doing Lua["Table"]["Version"]. Except this is just returning a table with no identifier?

auto script = lua.load("return {x = 24}"); // Load a script somehow.
auto result = script(); // Run it, and save the return value.
assert(result["x"] == 24); // You now have the result.

(Disclaimer: I haven't actually tested the above code.)

Hey appreciate it! It's 4:30 here I've been messing with this for 4 hours! I'll try it in the morning.

Thanks for at least the attempt!

@a light breeze Hey, so I tried your example, I don't think you can call result like that. Also, for anyone familiar with Sol, most of the examples use a call to script or this load where they plug in the information. I believe you use script_file and load_file equivalent for the files and I just was hoping to make sure that is correct.

Thanks for the help.

Talked to the developer and solved it. Thanks

This topic is closed to new replies.

Advertisement