From d33a8b69ca5054ad303950e36ab32002820b6b07 Mon Sep 17 00:00:00 2001 From: upsilon Date: Tue, 20 Jun 2017 16:42:35 +0200 Subject: [PATCH] Make the on_hit field optional --- README.md | 2 +- init.lua | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index 1c3fe5f..6ac1c0b 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ Definition: definition table, containing: * target (optional, defaulting to throwing.target_both): what the arrow is able to hit (throwing.target_node, throwing.target_object, throwing.target_both). * allow_protected (optional, defaulting to false): whether the arrow can be throw in a protected area * on_hit_sound (optional): sound played when the arrow hits a node or an object. - * on_hit(pos, last_pos, node, object, hitter, data, self) (must exist, will crash if nil): callback function: + * on_hit(pos, last_pos, node, object, hitter, data, self) (optional but very useful): callback function: - pos: the position of the hit node or object. - last_pos: the last air node where the arrow was - node and object: hit node or object. Either node or object is nil, depending diff --git a/init.lua b/init.lua index 4c6b731..6539c8b 100644 --- a/init.lua +++ b/init.lua @@ -111,16 +111,18 @@ local function arrow_step(self, dtime) return end - local ret, reason = self.on_hit(pos, self.last_pos, node, obj, player, self.data, self) - if ret == false then - if reason then - logging(": on_hit function failed for reason: "..reason) - else - logging(": on_hit function failed") - end + if self.on_hit then + local ret, reason = self.on_hit(pos, self.last_pos, node, obj, player, self.data, self) + if ret == false then + if reason then + logging(": on_hit function failed for reason: "..reason) + else + logging(": on_hit function failed") + end - hit_failed() - return + hit_failed() + return + end end if self.on_hit_sound then