problems with on_punch in registering a node

Post Reply
oftox
New member
Posts: 2
Joined: Mon Oct 25, 2021 17:27
GitHub: Oftox
In-game: Oftox
Location: Finland

problems with on_punch in registering a node

by oftox » Post

Problems with on_punch in registering a node

Hi! I'm trying to make a node that when hit kills the player but when i hit the node i get an error:

AsyncErr: Lua: Runtime error from mod 'mod1' in callback node_on_punch(): /minetest/bin/../mods/mod1/init.lua:11: attempt to call field 'set_hp' (a nil value)
stack traceback:
/minetest/bin/../mods/mod1/init.lua:11: in function </minetest/bin/../mods/mod1/init.lua:10>


Here's the code:
local name = minetest.localplayer
local player = minetest.get_player_by_name(name)


minetest.register_node("mod1:block", {

description = "Block",
tiles = {"default_cobble.png"},
groups = {oddly_breakable_by_hand=2},
on_punch = function(player)
player:set_hp(0)
end,
})

I tried replacing the player:set_hp(0) with other functions but i get the same error. Am i missing something here or am i just a newbie?

User avatar
Linuxdirk
Member
Posts: 3217
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: problems with on_punch in registering a node

by Linuxdirk » Post

You need to use the player object from within the on_punch function. The on_punch function is called with several arguments without you having to care about them manually.

You should also make sure that the puncher of a node is an actual player.

Code: Select all

minetest.register_node("mod1:block", {
    description = "Block",
    tiles = {"default_cobble.png"},
    groups = {oddly_breakable_by_hand=2},
    on_punch = function(pos, node, puncher, pointed_thing)
        if puncher:is_player() then puncher:set_hp(0) end
    end,
})
The on_punch function: https://github.com/minetest/minetest/bl ... 7938-L7941

is_player(): https://github.com/minetest/minetest/bl ... .txt#L6529

oftox
New member
Posts: 2
Joined: Mon Oct 25, 2021 17:27
GitHub: Oftox
In-game: Oftox
Location: Finland

Re: problems with on_punch in registering a node

by oftox » Post

Linuxdirk wrote:
Tue Oct 26, 2021 11:47
You need to use the player object from within the on_punch function. The on_punch function is called with several arguments without you having to care about them manually.

You should also make sure that the puncher of a node is an actual player.

Code: Select all

minetest.register_node("mod1:block", {
    description = "Block",
    tiles = {"default_cobble.png"},
    groups = {oddly_breakable_by_hand=2},
    on_punch = function(pos, node, puncher, pointed_thing)
        if puncher:is_player() then puncher:set_hp(0) end
    end,
})
The on_punch function: https://github.com/minetest/minetest/bl ... 7938-L7941

is_player(): https://github.com/minetest/minetest/bl ... .txt#L6529
Oh. Didn't know that. Thanks a lot Linuxdirk! I thought that you need to pass your own variables as arguments to the function.

User avatar
Linuxdirk
Member
Posts: 3217
Joined: Wed Sep 17, 2014 11:21
In-game: Linuxdirk
Location: Germany
Contact:

Re: problems with on_punch in registering a node

by Linuxdirk » Post

oftox wrote:
Tue Oct 26, 2021 12:30
Oh. Didn't know that. Thanks a lot Linuxdirk! I thought that you need to pass your own variables as arguments to the function.
You're welcome :) In the function you can only use things that are provided by the API (like the parameters in this specific order) or that you create or get from within this function.

I suggest only using the Lua API documentation file as reference on how to use API functions. The dev wiki's documentation on API calls is commonly seen as just outdated at best. All other sources are just 3rd-party sources not to be used as only reference.

Good luck and have fun!

Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests