lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

It was thus said that the Great Littlefield, Tyler once stated:
> Hello all:
> I've stepped away from this project for a while in terms of the 
> scripting portion because I was getting a bit lost in implementation. 
> What I wish to do is expose some APIs to the scripting engine so that 
> content can be added through my game engine. This is essentially a mud, 
> so the APIs exposed are for adding and modifying rooms, etc.
> I was hoping that perhaps someone could take a look at my code and just 
> provide some pointers, or let me know if there's a better way to do what 
> exists already. Most importantly, I'm curious if there are any good 
> sandbox implementations using lua5.2, as setenv in 5.1 seems to be 
> deprecated.
> Most of my lua specific code can be found here, in scr_*.cpp:
> I'm sorry for the direct link--I did not want to provide a couple 
> thousand lines of code as an example. Any information would be greatly 
> appreciated.
> Thanks,

  I'm looking at scr_entity.cpp.  I see that you create a metatable for
entities, but you don't seem to be using it to full effect.  If you go to
the trouble, then your code can be simplified quite a bit:

	#define GAME_ENTITY	"entity"

	int SCR_NewEntity(lua_State *L)
	  Entity *entity;
	  entity = lua_newuserdata(L,1,sizeof(Entity));
	  /* initialize entity */
	  return 1;

	int SCR_GetName(lua_State *L)
	  Entity      *entity;
	  std::string  name;
	  entity = luaL_checkudata(L,1,GAME_ENTITY);
	  name   = entity->GetName();
	  return 1;

	int SCR_SetName(lua_state *L)
	  Entity     *entity = luaL_checkudata(L,1,GAME_ENTITY);
	  const char *name   = luaL_checkstring(L,2);
	  return 0;
	int SCR_MoveTo(lua_State *L)
	  Entity *src    = luaL_checkudata(L,1,GAME_ENTITY);
	  Entity *target = luaL_checkudata(L,2,GAME_ENTITY);
	  return 1;

  The luaL_check*() functions do the appropriate error checking, so you
don't have to.  

  Another thing to look out for---duplicating functions that already exist
in Lua.  I noticed SCR_TypeToStr(), which does the same thing as

  As for sandboxing, there are two ways---one way is to only include in the
global scope only those functions you want.  Sections 2.2, 3.2 and 3.3 of
the Lua 5.2 manual cover this.