lua_getfield(L, LUA_GLOBALSINDEX, "f"); /* function to be called */
lua_pushstring(L, "how"); /* 1st argument */
lua_getfield(L, LUA_GLOBALSINDEX, "t"); /* table to be indexed */
lua_getfield(L, -1, "x"); /* push result of t.x (2nd arg) */
lua_remove(L, -2); /* remove 't' from the stack */
lua_pushinteger(L, 14); /* 3rd argument */
lua_call(L, 3, 1); /* call 'f' with 3 arguments and 1 result */
lua_setfield(L, LUA_GLOBALSINDEX, "a"); /* set global 'a' */
1. Get function "f" and push to the stack (stack pos 1: f)
2. Push "how" onto the stack (stack pos 2: "how")
3. Get table "t" and push to the stack (stack pos 3: t)
4. Get member x of "t" and push to stack. Our current iterator is (stack pos 4: t.x)
4, so -1 is 3, which is table "t"
5. Remove "t" from the stack (stack pos 3: t.x)
6. Push "14" onto the stack (stack pos 4: 14)
7. Call "f" using stack pos 1, with 3 arguments (next in stack) (stack pos 5: result of "f")
8. Set "a" to last item on stack: result of "f"
// stack: …
lua_getfield(L, LUA_GLOBALSINDEX, "f"); // stack: f, …
lua_pushstring(L, "how"); // stack: "how", f, …
lua_getfield(L, LUA_GLOBALSINDEX, "t"); // stack: t, "how", f, …
lua_getfield(L, -1, "x"); // stack: t.x, t, "how", f, …
lua_remove(L, -2); // stack: t.x, "how", f, …
lua_pushinteger(L, 14); // stack: 14, t.x, "how", f, …
lua_call(L, 3, 1); // stack: f("how", t.x, 14"), …
lua_setfield(L, LUA_GLOBALSINDEX, "a"); // stack: …
Stack:
|[ 5][-1]|
|[ 4][-2]|
|[ 3][-3]|
|[ 2][-4]|
|[ 1][-5]|
– script
ch.send("testing 1, 2, 3")
– end
– you could do
ch.load_obj('bronze-sword', 15)
– or
o = obj.new('bronze-sword', 15)
o.addaff('fire') – add an affect for fun
ch.room.add_obj(o)
code:
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
#include "mud.h"
void load_lua(void)
{
lua_State *L = lua_open(L);
lua_close(L);
}
OUTPUT:
$ make
g++ -Wall -O -g -DNOCRYPT -c -o obj/lua.test.o lua.test.c
lua.test.c: In function `void load_lua()':
lua.test.c:6: error: expected primary-expression before '*' token
lua.test.c:7: error: invalid conversion from `int' to `lua_State*'
lua.test.c:7: error: initializing argument 1 of `void lua_close(lua_State*)'
make: *** [obj/lua.test.o] Error 1
static int send(lua_State *L)
{
CHAR_DATA *ch;
for (ch = char_list; ch != NULL; ch = ch->next)
{
if(!IS_NPC(ch))
send_to_char(luaL_checkstring(L, 1), ch);
}
return 0;
}
static const struct luaL_reg mudlib [] = {
{"send", send},
{NULL, NULL}
};
void load_lua(CHAR_DATA *ch)
{
lua_State *L = luaL_newstate();
luaL_openlibs(L);
luaL_register(L, "mud", mudlib);
luaL_loadfile(L, "test.lua");
lua_close(L);
send_to_char("Lua closed.", ch);
}
TileCollection* TileCollection::FromLua(const std::string& filename)
{
lua_State* state = LastBastion::luaState();
int startSize = lua_gettop(state);
TileCollection* tc = new TileCollection();
int err = luaL_loadfile(state, filename.c_str()); // state: loader, …
if (err) {
const char* msg = lua_tostring(state, -1);
LastBastion::getSingleton().logError(msg);
lua_pop(state, 1); // pop the error message
return NULL;
}
lua_newtable(state); // state: env_table, loader, …
BIND_FUNCTION(luatc_Name, "Name"); // state: env_table, loader, …
BIND_FUNCTION(luatc_Image, "Image"); // state: env_table, loader, …
BIND_FUNCTION(luatc_Tile, "Tile"); // state: env_table, loader, …
lua_setfenv(state, -2); // state: loader, …
err = lua_pcall(state, 0, 0, 0);
if (err) {
const char* msg = lua_tostring(state, -1);
LastBastion::getSingleton().logError(msg);
lua_pop(state, 1); // pop the error message
}
// state: …
assert(lua_gettop(state) == startSize);
return tc;
}
room resets mobs:
static count is at 28
load_char(1000) // ch->id = fat_orc29
load_char(1000) // ch->id = fat_orc30
load_char(1001) // ch->id = slimorc31
The following code is from the Lua 5.1 ref guide:
On this line: lua_getfield(L, -1, "x");, you index the stack with -1, is this because you push a value on top, that it moves everything down,
thus resulting in a negative number? Then the next line: lua_remove(L, -2);
I don't really grasp the idea.