Modding with inventories

Post Reply
User avatar
MisterE
Member
Posts: 694
Joined: Sun Feb 16, 2020 21:06
GitHub: MisterE123
IRC: MisterE
In-game: MisterE

Modding with inventories

by MisterE » Post

I have looked at the documentation for how to make nodes with inventories that do things, I looked at the api stuff and the minetest videos episode on it, but I still don't really get how to manipulate inventories... Could someone give me some basic pointers that explain how the inventories work, along with some simple examples that I could try out to illustrate the point? I would like to be able to make machines that accept items and output items. I need to be able to understand what the code does, in order to manipulate it.
Thanks

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

Re: Modding with inventories

by Krock » Post

Untested sample:

Code: Select all

-- Register your machine node

minetest.register_node("mymod:mynodename", {
	-- node appearance stuff here

	on_construct = function(pos)
		-- Set up node inventory (contained in NodeMetaRef)
		local meta = minetest.get_meta(pos)
		local inv = meta:get_inventory()
		-- The inventory is empty at this point.
		-- Create inventory lists which can contain "n" stacks
		inv:set_size("input", 1) -- 1 stacks
		inv:set_size("output", 4) -- 4 stacks

		-- Fill "output" with some pre-given items (goodies?)
		inv:add_item("output", "default:stick") -- just add it somewhere
		inv:set_stack("output", 2, "default:mese") -- override empty stack

		-- Set a formspec so that players can rightclick on it
		-- "context" is a reference to the current node
		-- "current_player" is the player who opened this formspec
		meta:set_string("formspec", [[
			size[8,5]
			list[context;input;1,2;1,1;]
			list[context;output;4,1;2,2;]
			list[current_player;main;0,3;8,2;]
		]])
	end,
})
See also: https://rubenwardy.com/minetest_modding ... ories.html
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
MisterE
Member
Posts: 694
Joined: Sun Feb 16, 2020 21:06
GitHub: MisterE123
IRC: MisterE
In-game: MisterE

Re: Modding with inventories

by MisterE » Post

Ok, thanks, Ill test this out and ask about stuff I dont understand!

User avatar
MisterE
Member
Posts: 694
Joined: Sun Feb 16, 2020 21:06
GitHub: MisterE123
IRC: MisterE
In-game: MisterE

Re: Modding with inventories

by MisterE » Post

Ok, so here is my analysis of this code, please correct me if I get it wrong...

1) when we place a node, on_construct is called, which passes the position of the node to the on_construct function.

2) First we create a local variable that holds the metadata of the node at that position. The metadata is actually tied to the position, not to the node. At creation of the local variable, and when we initialize it, it holds an empty inventory table, which can be accessed through

Code: Select all

meta:get_inventory()
we use that command to get the inventory table and name it as "inv", now a local variable.
----
Question (a): Ok, here we have "named" the vars "inv" and "meta". Are they only local variables within the function, or are they actually "tied" to minetest's meta data at that location? In other words, will changing the local var "inv" from the function actually change the position's inventory, or will we have to pass the vars "meta" and/or "inv" back to minetest somehow? How does that work? (I am kinda new to object-oriented programming)
----

3) so the next 2 commands create two different inventory slot types and set their sizes. They are named "input", with one input slot for one itemstack, and "output" with four slots for four itemstacks.

4) the next two commands illustrate how we can put item stacks into inventory slots or overwrite whatever might be in a certain slot.
The first command just puts a stack of 1 stick somewhere into the output inventory
----
Question (b): Ok, so it puts it "somewhere" into the inventory. No location is specified, so I assume that it would try to put it in an empty slot...What if the inventory (which is currently 4 slots long) already has a itemstack in each of its slots? what would happen?
----
The second command overwrites the second Itemstack location of the output inventory with an itemstack of 1 mese block.

5) "Set a formspec so that players can rightclick on it"
----
Question (c): You mean that if no formspec was specified, you could not rightclick on the machine and get a user interface to interact with the node's inventories, right?
----
Question (d): What is happening here, when we are running a "set_string" method on "meta"?
----
Its obvious that this defines what the user interface looks like...
----
Question (e): What does the "size[8,5]" do? What does the "list[ ]" mean?
I can see that list makes the inventories visible... but what does "list" mean in this case?
----


Ok, I ran the code and saw what it does...
It makes and displays the two inventories as expected... This will definately get me started with this type of modding! Thx for your help!
If someone can answer these questions, I can get a better understanding of how this all works, bit this has been really helpful already.

User avatar
MisterE
Member
Posts: 694
Joined: Sun Feb 16, 2020 21:06
GitHub: MisterE123
IRC: MisterE
In-game: MisterE

Re: Modding with inventories

by MisterE » Post

Oh, one other thing,
----
Question (f): What do these numbers mean:

Code: Select all

			list[context;input;1,2;1,1;]
			list[context;output;4,1;2,2;]
			list[current_player;main;0,3;8,2;]
(the ones after the inventory name)

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

Re: Modding with inventories

by Krock » Post

MisterE wrote:
Fri Jun 12, 2020 18:33
The metadata is actually tied to the position, not to the node. At creation of the local variable, and when we initialize it, it holds an empty inventory table
[...]
In other words, will changing the local var "inv" from the function actually change the position's inventory
Every position in the map equals one node. For "empty" areas this node is called "air" (no mod name prefix). Metadata is tied to the node, and the node is tied to the position.

What we're doing there is getting a reference of the inventory. All changes that you do to "meta" and "inv" are passed to the C++ engine and applied in-game. Comparable to C++ variable references or C pointers.
MisterE wrote:
Fri Jun 12, 2020 18:33
I assume that it would try to put it in an empty slot...
It places the stack (correct name is ItemStack) into the first stack which has space for it. It can be empty or the same type. "add_item" returns an ItemStack which indicates how much leftover there is (i.e. stuff that didn't fit into the inventory list).
https://github.com/minetest/minetest/bl ... 5726-L5727
MisterE wrote:
Fri Jun 12, 2020 18:33
You mean that if no formspec was specified, you could not rightclick
Yes (kinda). As an alternative you can specify an on_rightclick callback function, but for now this here is the simplest approach.
MisterE wrote:
Fri Jun 12, 2020 18:33
Question (d): What is happening here, when we are running a "set_string" method on "meta"?
"formspec" is a special field like "infotext" (top-left text on hover). "meta" is a string key/value storage bound to the node (plus inventory).

Regarding all other formspec questions, please look up the syntax:
https://github.com/minetest/minetest/bl ... 2157-L2162

EDIT: Also take some of the default shipped code as an example. Here's the furnace from minetest_game:
https://github.com/minetest/minetest_ga ... e.lua#L275
Look, I programmed a bug for you. >> Mod Search Engine << - Mods by Krock - DuckDuckGo mod search bang: !mtmod <keyword here>

User avatar
MisterE
Member
Posts: 694
Joined: Sun Feb 16, 2020 21:06
GitHub: MisterE123
IRC: MisterE
In-game: MisterE

Re: Modding with inventories

by MisterE » Post

Ok, thanks for your help!

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests