How do I check if minetest node/item is registered (exists)?

Post Reply
drkwv
Member
Posts: 102
Joined: Thu Jun 28, 2012 13:48
GitHub: aa6

How do I check if minetest node/item is registered (exists)?

by drkwv » Post

Hello. I need something like this:

Code: Select all

if minetest.item_is_registered("default:cobble") then
  minetest.override_item("default:cobble", ...)
end
How do I test if item is registered?
Last edited by drkwv on Sat Jul 20, 2019 11:55, edited 1 time in total.

User avatar
TalkLounge
Member
Posts: 324
Joined: Sun Mar 26, 2017 12:42
GitHub: TalkLounge
In-game: TalkLounge
Location: Germany

Re: How do I check if minetest node/item is registered (exis

by TalkLounge » Post

Code: Select all

if minetest.registered_nodes["default:dirt"] then
 --exists
end
They are all saved here

core is the same as minetest. So core.registered_items would work as well as an example.
Subgames Server: Sky World Subgames German Survival Server: Wildes Land 2 E-Mail: talklounge@yahoo.de


User avatar
Krock
Developer
Posts: 4650
Joined: Thu Oct 03, 2013 07:48
GitHub: SmallJoker
Location: Switzerland
Contact:

Re: How do I check if minetest node/item is registered (exis

by Krock » Post

To ensure this code always works as expected, depend your mod on "default". This ensures that the nodes are registered before your mod is loaded. However, that does not matter as soon you're checking the node presence from a callback because then all nodes will already be registered.
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

ThorfinnS
Member
Posts: 311
Joined: Mon Feb 25, 2019 22:05
GitHub: ThorfinnS

Re: How do I check if minetest node/item is registered (exis

by ThorfinnS » Post

Krock wrote:To ensure this code always works as expected, depend your mod on "default". This ensures that the nodes are registered before your mod is loaded. However, that does not matter as soon you're checking the node presence from a callback because then all nodes will already be registered.
Just so I'm sure I'm understanding how it works, an optional depend ("default?") is sufficient for this, right? It's what I've been doing, and haven't run into anything yet, but maybe that's just dumb luck..

User avatar
TalkLounge
Member
Posts: 324
Joined: Sun Mar 26, 2017 12:42
GitHub: TalkLounge
In-game: TalkLounge
Location: Germany

Re: How do I check if minetest node/item is registered (exis

by TalkLounge » Post

If your init.lua would be

Code: Select all

if minetest.registered_nodes["default:dirt"] then
 --exists
end
then it can happen, that your mod, with this source code in it will be loaded before the default mod and then the if statement would be false. Optional dependency would be the best here, because if there's a default mod, then it will be loaded before your mod, but if there's no default mod, your mod will run as well.

But if you do something like this in your init.lua

Code: Select all

minetest.register_on_joinplayer(function(player)
  if minetest.registered_nodes["default:dirt"] then
   --exists
  end
end)
then you don't need to set default as optional dependency, because all mods will be loaded, before this callback will be called.
Subgames Server: Sky World Subgames German Survival Server: Wildes Land 2 E-Mail: talklounge@yahoo.de

User avatar
GreenXenith
Member
Posts: 1356
Joined: Wed Oct 28, 2015 01:26
GitHub: GreenXenith
Location: UTC-8:00
Contact:

Re: How do I check if minetest node/item is registered (exis

by GreenXenith » Post

TalkLounge wrote:But if you do something like this in your init.lua

Code: Select all

minetest.register_on_joinplayer(function(player)
  if minetest.registered_nodes["default:dirt"] then
   --exists
  end
end)
then you don't need to set default as optional dependency, because all mods will be loaded, before this callback will be called.
This wont work for overriding.
Use

Code: Select all

minetest.register_on_mods_loaded(function()
    if minetest.registered_nodes["default:cobble"] then
        minetest.override_item("default:cobble", {})
    end
end)
YouTube | Mods | Patreon | Minetest Discord @greenxenith

You should not be able to read this message.

User avatar
TalkLounge
Member
Posts: 324
Joined: Sun Mar 26, 2017 12:42
GitHub: TalkLounge
In-game: TalkLounge
Location: Germany

Re: How do I check if minetest node/item is registered (exis

by TalkLounge » Post

Ohh, yeah, sure. Just want to show the system of dependencies. But overriding won't work then. Misleading example. So use optional dependencies here.

If you are using minetest.register_on_mods_loaded, your mod is only for minetest 5.0.0+. Won't run in 0.4.x then.
Subgames Server: Sky World Subgames German Survival Server: Wildes Land 2 E-Mail: talklounge@yahoo.de

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests