July 2018
Beginner
202 pages
5h 42m
English
Let's see a simple example of how to read variables from Lua into C. Consider the use case where we create a game character. We want to store the attributes of that character in an external configuration file to allow for easy editing. The attributes file will look something like this:
class = "Warrior"attack = 56defense = 43
Reading the configuration values in C would involve creating a new Lua state, loading the Lua file, pushing each variable onto the stack, reading and storing the values, and finally popping all of the values off the stack. The following code demonstrates how to do this:
#include "lua.h"#include "lauxlib.h"#include "lualib.h"#include <string.h>int main(int argc, char** argv) { lua_State *L = luaL_newstate();Read now
Unlock full access