Help correctly using "override_item" function.

Post Reply
Erato
New member
Posts: 2
Joined: Wed Aug 14, 2024 10:51

Help correctly using "override_item" function.

by Erato » Post

Hello everyone,
I am new to the forum, to Minetest and Lua programming language in general.
I am trying to create a mod and wanted to use the function "minetest:override_item()" but it does not seem to work.

The following example does work as expected (the stone becomes transparent):

Code: Select all

local node_name = "default:stone"
local node_def = minetest.registered_nodes[node_name]
if node_def then
    local new_def = table.copy(node_def)
    new_def.drawtype = "airlike"
    minetest.register_node(":"..node_name, new_def)
end
but if I want to change the definition of an already existing node using override_item it does not work:

Code: Select all

local node_name = "default:stone"
local node_def = minetest.registered_nodes[node_name]
if node_def then
    local new_def = table.copy(node_def)
    new_def.drawtype = "airlike"
    minetest.override_item(node_name, new_def)
end
returning the following error:

Code: Select all

ERROR[Main]: .../minetest/mods/test/init.lua:6: Attempt to redefine name of default:stone to "default:stone"
How can I fix this issue?

Many thanks for your replyes

User avatar
TenPlus1
Member
Posts: 4022
Joined: Mon Jul 29, 2013 13:38
In-game: TenPlus1
Contact:

Re: Help correctly using "override_item" function.

by TenPlus1 » Post

You are trying to override the complete node definition with itself when you only need to change one thing e.g.

Code: Select all

minetest.override_item("default:stone", {
    drawtype = "airlike",
})

Josselin2
Member
Posts: 130
Joined: Thu May 28, 2020 15:32
GitHub: axcore
In-game: Josselin
Location: Tunnelers' Abyss

Re: Help correctly using "override_item" function.

by Josselin2 » Post

table.copy() will have unexpected results in many situations, so it's a bad idea to use it on any table you didn't create yourself, such as Minetest node definitions.

User avatar
Blockhead
Moderator
Posts: 2269
Joined: Wed Jul 17, 2019 10:14
GitHub: Montandalar
IRC: Blockhead256
In-game: Blockhead Blockhead256
Location: Land Down Under
Contact:

Re: Help correctly using "override_item" function.

by Blockhead » Post

When using override item, you should only provide table entries for fields you intend to redefine. There's no need to copy the table. Including the name in the table will trip the error you encountered, since renaming items doesn't work in the engine, only aliases.

References

override_item - docs.

override_item - source code.

Aliases - docs.
/˳˳_˳˳]_[˳˳_˳˳]_[˳˳_˳˳\ Advtrains enthusiast | My map: Noah's Railyard | My Content on ContentDB ✝️♂

Erato
New member
Posts: 2
Joined: Wed Aug 14, 2024 10:51

Re: Help correctly using "override_item" function.

by Erato » Post

Hello Everyone,
thank you for the replies!

@TenPlus1's answer did the job: I was actually so desperate to find the solution that I could not see it was this obvious.
Thank you @Josselin2 for the advice which I will follow.
Thank you @Blockhead, the documentation I already found, I guess in the future I will also try to read through the source code... :)

Have a great time to everyone!

Post Reply