Jump to content

Implementing a Lua scripting interface?

So, I'm writing a simple game engine, and I'm not at the point where a scripting interface would be helpful.

I've chosen Lua since it's an established an simple language, but now I'm not sure how to bind the Lua functions to the engine's.

I can't find any guide/documentation about it, just lots and lots of guides on the language in general and its syntax.

 

I've got an entity component system working and it would definitely speed up the development having a Lua interface.

 

Current stage:

post-49433-0-94928800-1434299557_thumb.p

Link to comment
https://linustechtips.com/topic/386591-implementing-a-lua-scripting-interface/
Share on other sites

Link to post
Share on other sites

 

--snip--

 

Hi, it was a while when I was using lua last time, so I'm a bit rusty, and had to remind myslef some basics, but here those basics are. This code will implement one C function for use to lua scripts:

#include <iostream>#include <string>#include "lua.h"#include "lualib.h"#include "lauxlib.h"int lua_repeatString(lua_State *L){		if(lua_gettop(L) >= 2){ // At least one argument passed. lua_gettop(L) returns size of stack - it's like how many variables there are.		if(lua_isstring(L, 1)){ // is argument passed a string?									if(lua_isnumber(L, 2)){ // is argument passed a number?				std::string s = lua_tostring(L, 1);				int n = lua_tonumber(L, 2);								std::cout << "Repeating \"" << s << "\" " << n << " times:" << std::endl;								for(int i = 0; i < n; i++){					std::cout << s << std::endl;				}								lua_pushnumber(L, s.length()); // pushing length of string passed to this function on stack, we will tell later by return 1 that we returning one variable back to lua.			} else {				lua_pushstring(L, "Second argument passed to function needs to be a number");				lua_error(L);			}		} else {			lua_pushstring(L, "First argument passed to function needs to be a string"); 			lua_error(L);		}	} else {		lua_pushstring(L, "Function needs 2 arguments."); //You push string on stack that will be used as error message by lua_error		lua_error(L);		// Anythign after lua_error wont fire up because it is how lua_error works.	}			return 1; //We return one variable thoruh this function, the length of string passed to this function.}int main(int argc, char** argv) {		lua_State *L = luaL_newstate(); //Creating new lua state. it's lika a box for lua, anythign done in this state will stay here, you can create multiple states that won't share anything to eachother.	luaL_openlibs(L); //Initialisation of lua libraries, this does initialisate all of them (I guess) you can specify only some of them if you don't need all functionalities.		lua_register(L, "repeatString", lua_repeatString); //This one registers function that will be available to use in lua scripts		if(luaL_dofile(L, "test.lua")){ //This runs lua script file (loads it and runs)		std::cout << "Error: " << lua_tostring(L, -1) << std::endl; //if lua_dofile returns false (0) then yhere is an error message you can print in your c program. It may be some message from lua std, or our error message from function we have registred (lua_pushstring and lua_error wil make error message apear here)	}		lua_close(L);}

And lua script run with program above can look like:

local lengthOfString = repeatString("This is a test message.", 2);print("Our string passed to repeatString has " .. lengthOfString .. " characters."); 

You can also run lua functions defined in lua script file that you load a run. But it would take me to write another post this long, so if you would need that too then you know where to find me.

I explained evertything as best as I could but my english isn't perfect so there may be some weird things to explain :P

 

Edit: I have changed the example so it explains now how to return some value back to lua.

 

Output for all run together would be like:

 

Repeating "This is a test message." 2 times:

This is a test message.
This is a test message.
Our string passed to repeatString has 23 characters.
Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×