diff --git a/examples/tables.cpp b/examples/tables.cpp new file mode 100644 index 00000000..be2dfde9 --- /dev/null +++ b/examples/tables.cpp @@ -0,0 +1,36 @@ +#include +#include +#include + +// this example shows how to read data in from a lua table + +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\"" + "}"); + + + // using the values stored in table1 + std::cout << lua.get("table1").get(1) << " " + << lua.get("table1").get(2) << '\n'; + + auto t2 = lua.get("table2"); + auto nestedTable = t2.get("nestedTable"); + + // some retrieval of values from the nested table + // the cleaner way of doing things + std::cout << "nested table: key1 : " << nestedTable.get("key1") << ", key2: " + // yes you can chain the get<>() results + << lua.get("table2").get("nestedTable").get("key2") + << '\n'; + + std::cout << "name of t2: " << t2.get("name") << '\n'; +} diff --git a/sol/function_types.hpp b/sol/function_types.hpp index dd7af8fb..0fd4f7d6 100644 --- a/sol/function_types.hpp +++ b/sol/function_types.hpp @@ -564,4 +564,4 @@ struct usertype_indexing_function : public usertype_function_core } // sol -#endif // SOL_FUNCTION_TYPES_HPP +#endif // SOL_FUNCTION_TYPES_HPP \ No newline at end of file