callbacks.lua 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. -- Entities that test their callbacks
  2. local message = function(msg)
  3. minetest.log("action", msg)
  4. minetest.chat_send_all(msg)
  5. end
  6. local get_object_name = function(obj)
  7. local name = "<nil>"
  8. if obj then
  9. if obj:is_player() then
  10. name = obj:get_player_name()
  11. else
  12. name = "<entity>"
  13. end
  14. end
  15. return name
  16. end
  17. local spos = function(self)
  18. return minetest.pos_to_string(vector.round(self.object:get_pos()))
  19. end
  20. -- Callback test entity (all callbacks except on_step)
  21. minetest.register_entity("testentities:callback", {
  22. initial_properties = {
  23. visual = "upright_sprite",
  24. textures = { "testentities_callback.png" },
  25. },
  26. on_activate = function(self, staticdata, dtime_s)
  27. message("Callback entity: on_activate! pos="..spos(self).."; dtime_s="..dtime_s)
  28. end,
  29. on_deactivate = function(self)
  30. message("Callback entity: on_deactivate! pos="..spos(self))
  31. end,
  32. on_punch = function(self, puncher, time_from_last_punch, tool_capabilities, dir, damage)
  33. local name = get_object_name(puncher)
  34. message(
  35. "Callback entity: on_punch! "..
  36. "pos="..spos(self).."; puncher="..name.."; "..
  37. "time_from_last_punch="..time_from_last_punch.."; "..
  38. "tool_capabilities="..tostring(dump(tool_capabilities)).."; "..
  39. "dir="..tostring(dump(dir)).."; damage="..damage)
  40. end,
  41. on_rightclick = function(self, clicker)
  42. local name = get_object_name(clicker)
  43. message("Callback entity: on_rightclick! pos="..spos(self).."; clicker="..name)
  44. end,
  45. on_death = function(self, killer)
  46. local name = get_object_name(killer)
  47. message("Callback entity: on_death! pos="..spos(self).."; killer="..name)
  48. end,
  49. on_attach_child = function(self, child)
  50. local name = get_object_name(child)
  51. message("Callback entity: on_attach_child! pos="..spos(self).."; child="..name)
  52. end,
  53. on_detach_child = function(self, child)
  54. local name = get_object_name(child)
  55. message("Callback entity: on_detach_child! pos="..spos(self).."; child="..name)
  56. end,
  57. on_detach = function(self, parent)
  58. local name = get_object_name(parent)
  59. message("Callback entity: on_detach! pos="..spos(self).."; parent="..name)
  60. end,
  61. get_staticdata = function(self)
  62. message("Callback entity: get_staticdata! pos="..spos(self))
  63. end,
  64. })
  65. -- Only test on_step callback
  66. minetest.register_entity("testentities:callback_step", {
  67. visual = "upright_sprite",
  68. textures = { "testentities_callback_step.png" },
  69. on_step = function(self, dtime)
  70. message("on_step callback entity: on_step! pos="..spos(self).."; dtime="..dtime)
  71. end,
  72. })