From: Perttu Ahola Date: Sat, 12 Nov 2011 17:19:58 +0000 (+0200) Subject: Scripting: Allow multiple global step callbacks and improve documentation X-Git-Tag: 0.4.dev-20111201-0~189 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=f8c9b703798873c6e958560341105069dca8f86c;p=oweals%2Fminetest.git Scripting: Allow multiple global step callbacks and improve documentation --- diff --git a/data/scripts/default.lua b/data/scripts/default.lua index 0b9f8b913..eda7cd25e 100644 --- a/data/scripts/default.lua +++ b/data/scripts/default.lua @@ -72,13 +72,47 @@ function dump(o, dumped) end end +-- Global functions: +-- minetest.register_entity(name, prototype_table) +-- minetest.register_globalstep(func) +-- +-- Global objects: +-- minetest.env - environment reference +-- +-- Global tables: +-- minetest.registered_entities +-- ^ List of registered entity prototypes, indexed by name +-- minetest.object_refs +-- ^ List of object references, indexed by active object id +-- minetest.luaentities +-- ^ List of lua entities, indexed by active object id +-- +-- EnvRef methods: +-- - add_node(pos, content); pos={x=num, y=num, z=num} +-- +-- ObjectRef methods: +-- - remove(): remove object (after returning from Lua) +-- - getpos(): returns {x=num, y=num, z=num} +-- - setpos(pos); pos={x=num, y=num, z=num} +-- - moveto(pos, continuous=false): interpolated move +-- - add_to_inventory(itemstring): add an item to object inventory +-- +-- Registered entities: +-- - Functions receive a "luaentity" as self: +-- - It has the member .object, which is an ObjectRef pointing to the object +-- - The original prototype stuff is visible directly via a metatable +-- + print("omg lol") print("minetest dump: "..dump(minetest)) -- Global environment step function function on_step(dtime) + -- print("on_step") end +minetest.register_globalstep(on_step) + local TNT = { -- Maybe handle gravity and collision this way? dunno physical = true, @@ -112,6 +146,15 @@ function TNT:on_rightclick(clicker) pos = {x=pos.x, y=pos.y+0.1, z=pos.z} self.object:moveto(pos, false) end + +print("TNT dump: "..dump(TNT)) + +print("Registering TNT"); +minetest.register_entity("TNT", TNT) + +print("minetest.registered_entities:") +dump2(minetest.registered_entities) + --[[ function TNT:on_rightclick(clicker) print("TNT:on_rightclick()") @@ -125,15 +168,6 @@ function TNT:on_rightclick(clicker) end --]] -print("TNT dump: "..dump(TNT)) - -print("Registering TNT"); -minetest.register_entity("TNT", TNT) - ---print("minetest.registered_entities: "..dump(minetest.registered_entities)) -print("minetest.registered_entities:") -dump2(minetest.registered_entities) - --[=[ register_block(0, { diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp index 6d0522000..27f46a8ac 100644 --- a/src/scriptapi.cpp +++ b/src/scriptapi.cpp @@ -170,8 +170,35 @@ static int l_register_entity(lua_State *L) return 0; /* number of results */ } +// Register a global step function +// register_globalstep(function) +static int l_register_globalstep(lua_State *L) +{ + luaL_checktype(L, 1, LUA_TFUNCTION); + infostream<<"register_globalstep"<