Improve formatting of table example

This commit is contained in:
Rapptz 2015-01-15 11:53:15 -05:00
parent c83d1941db
commit f6fb0fd7f0

View File

@ -2,35 +2,35 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
/// this example shows how to read data in from a lua table // this example shows how to read data in from a lua table
using namespace std;
//compiles on linux with: g++ -std=c++11 -I.. table.cpp -o tables -llua int main() {
sol::state lua;
// table used as an array
lua.script("table1 = {\"hello\", \"table\"}");
// table with a nested table and the key value syntax
lua.script("table2 = {"
"[\"nestedTable\"] = {"
"[\"key1\"] = \"value1\","
"[\"key2\"]= \"value2\""
"},"
"[\"name\"]= \"table2\""
"}");
int main(int argc, char * argv[])
{ // using the values stored in table1
sol::state lua; std::cout << lua.get<sol::table>("table1").get<std::string>(1) << " "
//table used as an array << lua.get<sol::table>("table1").get<std::string>(2) << '\n';
lua.script("table1 = {\"hello\", \"table\"}");
//table with a nested table and the key value syntax auto t2 = lua.get<sol::table>("table2");
lua.script("table2 = {[\"nestedTable\"]={[\"key1\"]=\"value1\", [\"key2\"]=\"value2\"}," auto nestedTable = t2.get<sol::table>("nestedTable");
" [\"name\"]=\"table2\"}");
// some retrieval of values from the nested table
// the cleaner way of doing things
//using the values stored in table1 std::cout << "nested table: key1 : " << nestedTable.get<std::string>("key1") << ", key2: "
cout << lua.get<sol::table>("table1").get<string>(1)<< " " << // yes you can chain the get<>() results
lua.get<sol::table>("table1").get<string>(2) << endl; << lua.get<sol::table>("table2").get<sol::table>("nestedTable").get<std::string>("key2")
<< '\n';
auto t2 = lua.get<sol::table>("table2");
auto nestedTable = t2.get<sol::table>("nestedTable"); std::cout << "name of t2: " << t2.get<std::string>("name") << '\n';
//some retrival of values from the nested table }
//the cleaner way of doing things
cout << "nested table: key1 : " << nestedTable.get<string>("key1") << ", key2: "
//yes you can chain the get<>() results
<< lua.get<sol::table>("table2").get<sol::table>("nestedTable").get<string>("key2")
<< endl;
cout << "name of t2: " << t2.get<string>("name") << endl;
return 0;
}