Skip to content

Example 1: Reading a Simple Config File

kamih edited this page Jul 14, 2012 · 1 revision

This is simple.lua, which contains only simple global values:

BoolVal = true
IntVal = 42
FloatVal = 23.51
StringVal = "awesome string!"

This is how you would read the values with LuaUtils in C++:

using namespace LuaUtils;

// Create a new LuaState and load the simple.lua file
LuaState state;
state.loadFile("simple.lua");

int ival;
float fval;
bool bval;
std::string sval;

// Read the globals
state.getValue("IntVal", ival);
state.getValue("BoolVal", bval);
state.getValue("StringVal", sval);
state.getValue("FloatVal", fval);

You can also load Lua code from a string with LuaState::loadString. These functions both return a success flag.

As you can see, the LuaState::getValue function is overloaded so you can conveniently use it to get any type. If the global value cannot be found in the Lua state, this function returns false.

You can also set the value of globals with LuaState::setValue.

Clone this wiki locally