(lua) controlling multiple ai's through multiple scripts

Started by
0 comments, last by corysama 19 years ago
hi, i'm using lua to do some scripting and have a simple question. first, i have entities that behave differently, each has their own corrosponding lua script. now, my question... do i load each script with it's own lua_State *, or do I use lua_dofile(script) every frame for each differenty entity, using the same lua_State *? My inclination is to have multiple lua_State *'s, but then they each need their own ctor/dtor code. So I was wondering what is the best approach for running multiple entities (each with own script) from lua. thanks
Advertisement
I would recommend having one lua_State holding all of your entities. Have each entity store its own state in a table. Implement the different behaviors as Lua functions that take an entity state table as a parameter. Doing it all in one state will be a lot smaller, faster and better organized than having lots of separate states. Also, with all of your entities in one state it is a lot easier for them to interact with each other.

You definitely don't want to lua_dofile() every frame. lua_dofile() reads a file, parses the script text, compiles it to bytecode, makes a function out of it and calls the function. Instead, just do lua_loadfile() once at the beginning of your program. Instead of calling the compiler every frame, just store the functions in Lua variables and call them whenever you need them.

Here's an example to get you started:

extern "C"{#include "lua.h"#include "lualib.h"#include "lauxlib.h"}const char luaScript[] = "entities = \n""{\n""	dog = \n""	{\n""		stamina = 10,\n""		update = function(self)\n""			if self.stamina > 0 then\n""				print 'bark'\n""				self.stamina = self.stamina-1\n""			end\n""		end\n""	},\n""	cat =\n""	{\n""		stamina = 5,\n""		update = function(self)\n""			if self.stamina > 0 then\n""				print 'meow'\n""				self.stamina = self.stamina-1\n""			end\n""		end\n""	}\n""}\n""updateEntities = function()\n""	for name, entity in pairs(entities) do\n""		entity.update(entity)\n""	end\n""end\n";int main(int argc, char *argv[]){	lua_State *luaState;	int result, frame;		luaState = lua_open();	luaopen_base(luaState);	/* Compile the script */	if((result = luaL_loadbuffer(luaState, luaScript, sizeof(luaScript)-1, "luaScript")) != 0)	{		fprintf(stderr, lua_tostring(luaState, -1));		return result;	}	/* Run the script to create the tables and the functions */	if((result = lua_pcall(luaState, 0, 0, 0)) != 0)	{		fprintf(stderr, lua_tostring(luaState, -1));		return result;	}	for(frame = 0; frame < 60; frame++)	{		/* Get the updateEntities() function */		lua_pushstring(luaState, "updateEntities");		lua_gettable(luaState, LUA_GLOBALSINDEX);		/* Call updateEntities() */		if((result = lua_pcall(luaState, 0, 0, 0)) != 0)		{			fprintf(stderr, lua_tostring(luaState, -1));			return result;		}	}	lua_close(luaState);	return 0;}


[Edited by - corysama on April 10, 2005 12:55:38 AM]

This topic is closed to new replies.

Advertisement