diff --git a/README.md b/README.md index 8d6887b8..c566df1f 100644 --- a/README.md +++ b/README.md @@ -15,40 +15,9 @@ It should be noted that the library itself depends on `lua.hpp` to be found by y If you want to contribute, please check CONTRIBUTING.md for details. Thank you! -## Example +## Examples -Here's an example on how to load a basic configuration struct with a Lua script. - -```cpp -#include -#include -#include - -struct test { - int foo; - std::string bar; - double baz; -}; - -test load(const sol::table& t) { - return { t.get("foo"), t.get("bar"), t.get("baz") }; -} - -int main() { - try { - sol::state lua; - lua.script("foo = 1234;\n" - "bar = \"hello world\";\n" - "baz = 1.4;"); - - test c = load(lua.global_table()); - std::cout << '(' << c.foo << ", " << c.bar << ", " << c.baz << ")\n"; - } - catch(const std::exception& e) { - std::cerr << e.what() << '\n'; - } -} -``` +Examples are provided in the examples directory. ## License diff --git a/examples/config.cpp b/examples/config.cpp new file mode 100644 index 00000000..fb92eb68 --- /dev/null +++ b/examples/config.cpp @@ -0,0 +1,27 @@ +// shows how to load basic data to a struct + +#include +#include +#include + +struct config { + std::string name; + int width; + int height; + + void print() { + std::cout << "Name: " << name << '\n' + << "Width: " << width << '\n' + << "Height: " << height << '\n'; + } +}; + +int main() { + sol::state lua; + config screen; + lua.open_file("config.lua"); + screen.name = lua.get("name"); + screen.width = lua.get("width"); + screen.height = lua.get("height"); + screen.print(); +} diff --git a/examples/config.lua b/examples/config.lua new file mode 100644 index 00000000..7d84f017 --- /dev/null +++ b/examples/config.lua @@ -0,0 +1,3 @@ +name = "Asus" +width = 1920 +height = 1080