Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
illuna-minetest
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Illuna-Minetest
illuna-minetest
Commits
f8c9b703
Commit
f8c9b703
authored
13 years ago
by
Perttu Ahola
Browse files
Options
Downloads
Patches
Plain Diff
Scripting: Allow multiple global step callbacks and improve documentation
parent
7b802c54
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
data/scripts/default.lua
+43
-9
43 additions, 9 deletions
data/scripts/default.lua
src/scriptapi.cpp
+46
-6
46 additions, 6 deletions
src/scriptapi.cpp
with
89 additions
and
15 deletions
data/scripts/default.lua
+
43
−
9
View file @
f8c9b703
...
...
@@ -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, {
...
...
This diff is collapsed.
Click to expand it.
src/scriptapi.cpp
+
46
−
6
View file @
f8c9b703
...
...
@@ -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"
<<
std
::
endl
;
lua_getglobal
(
L
,
"table"
);
lua_getfield
(
L
,
-
1
,
"insert"
);
int
table_insert
=
lua_gettop
(
L
);
// Get minetest.registered_globalsteps
lua_getglobal
(
L
,
"minetest"
);
lua_getfield
(
L
,
-
1
,
"registered_globalsteps"
);
luaL_checktype
(
L
,
-
1
,
LUA_TTABLE
);
int
registered_globalsteps
=
lua_gettop
(
L
);
// table.insert(registered_globalsteps, func)
lua_pushvalue
(
L
,
table_insert
);
lua_pushvalue
(
L
,
registered_globalsteps
);
lua_pushvalue
(
L
,
1
);
// push function from argument 1
// Call insert
if
(
lua_pcall
(
L
,
2
,
0
,
0
))
script_error
(
L
,
"error: %s
\n
"
,
lua_tostring
(
L
,
-
1
));
return
0
;
/* number of results */
}
static
const
struct
luaL_Reg
minetest_f
[]
=
{
{
"register_entity"
,
l_register_entity
},
{
"register_globalstep"
,
l_register_globalstep
},
{
NULL
,
NULL
}
};
...
...
@@ -566,6 +593,9 @@ void scriptapi_export(lua_State *L, Server *server)
lua_newtable
(
L
);
lua_setfield
(
L
,
-
2
,
"registered_entities"
);
lua_newtable
(
L
);
lua_setfield
(
L
,
-
2
,
"registered_globalsteps"
);
lua_newtable
(
L
);
lua_setfield
(
L
,
-
2
,
"object_refs"
);
...
...
@@ -684,12 +714,22 @@ void scriptapi_environment_step(lua_State *L, float dtime)
//infostream<<"scriptapi_luaentity_step: id="<<id<<std::endl;
StackUnroller
stack_unroller
(
L
);
lua_getglobal
(
L
,
"on_step"
);
if
(
lua_type
(
L
,
-
1
)
!=
LUA_TFUNCTION
)
return
;
// If no on_step function exist, do nothing
lua_pushnumber
(
L
,
dtime
);
if
(
lua_pcall
(
L
,
1
,
0
,
0
))
script_error
(
L
,
"error: %s
\n
"
,
lua_tostring
(
L
,
-
1
));
// Get minetest.registered_globalsteps
lua_getglobal
(
L
,
"minetest"
);
lua_getfield
(
L
,
-
1
,
"registered_globalsteps"
);
luaL_checktype
(
L
,
-
1
,
LUA_TTABLE
);
int
table
=
lua_gettop
(
L
);
// Foreach
lua_pushnil
(
L
);
while
(
lua_next
(
L
,
table
)
!=
0
){
// key at index -2 and value at index -1
luaL_checktype
(
L
,
-
1
,
LUA_TFUNCTION
);
// Call function
lua_pushnumber
(
L
,
dtime
);
if
(
lua_pcall
(
L
,
1
,
0
,
0
))
script_error
(
L
,
"error: %s
\n
"
,
lua_tostring
(
L
,
-
1
));
// value removed, keep key for next iteration
}
}
/*
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment