Skip to main content
GameDev.net gamedev.net
🔒 Locked

Understanding a Lua File (Sol C++)

Started by LedMar Mar 14, 2021 at 9:24 AM 7 replies 5.3k views
Original Post
LedMar
LedMar

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!

a light breeze
a light breeze

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

LedMar
LedMar

Ah okay,

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

LedMar
LedMar

@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?

a light breeze
a light breeze
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.)

LedMar
LedMar

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!

LedMar
LedMar

@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.

LedMar
LedMar

Talked to the developer and solved it. Thanks

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.