Trying to build a Groups Table from multiple Variables

Post Reply
User avatar
SegFault22
Member
Posts: 872
Joined: Mon May 21, 2012 03:17
Location: NaN

Trying to build a Groups Table from multiple Variables

by SegFault22 » Post

I'm working on a mod which automatically determines the groups for an item based on some data in a table, and passes it to the minetest.register_craftitem() function to register an item with the groups. Basically, it uses a variable in place of the group definitions in the table, said variable containing all of the group definitions.

I have noticed that group definitions are within a table, and the group "name" is an index in the table, and the number "level" for that group is stored "at" the index. The index is not a string in any examples I have found. And, when I try to test whether strings are acceptable as the index, an error is thrown: "'}' expected near '='". So, it is not currently possible for groups table indexes to be strings.

I have not found any way to insert group definitions into a table, said table being the groups table passed to the minetest.register_craftitem() function call. The only way I have found to "insert" group definitions is for them to be written out directly into a table, like this:

Code: Select all

groups = {metal = 1, ingot = 1, silver = 1}
...
groups = groups,
...
It is essential to my mod that the group definitions be stored in a table within a table at a certain index, because they are retrieved from separate tables full of data, such as a table of "item types" which defines "ingot", a table of "materials" which defines "silver", and a table of "material types" which defines "metal". When we need to put the group definitions to the groups table, the information has to be fetched from some index in a sub-table (like the numerical index 4), which is located at some other known index in the data table (such as "ingot"). Is there any way to have each group definition stored at some variable, then put the values from those variables together into the groups table? It should be such that if I had these variables (or similar):

Code: Select all

a = {metal = 1}
b = {ingot = 1}
c = {silver = 1}
the resulting groups table would be this:

Code: Select all

groups = {metal = 1, ingot = 1, silver = 1}
Better yet, is there a way to convert strings like "ingot", "metal", etc. to the undeclared variables for the group names, and then put them into the groups table?

I have tried everything that I know of, such as table index tricks and table.concat() (which returns a useless string, remember we get that error if the group name is a string and not an unassigned variable)...
Is this even possible? If so, how?

If this is possible, I suggest that information regarding how to do this should be added to the developer wiki page at http://dev.minetest.net/Groups, because that is where I first went to try and find out how to do this, before searching the Lua documentation to no avail.

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: Trying to build a Groups Table from multiple Variables

by Byakuren » Post

Code: Select all

local tab = {
  foo = bar,
  baz = quux,
}
tab.blah = 1
-- is syntax sugar for
local tab = {
  ["foo"] = bar,
  ["baz"] = quux,
}
tab["blah"] = 1
You should post the code that gave you the error. It's a syntax error, so you probably typed something incorrectly. Group table keys are always strings.
Every time a mod API is left undocumented, a koala dies.

User avatar
SegFault22
Member
Posts: 872
Joined: Mon May 21, 2012 03:17
Location: NaN

Re: Trying to build a Groups Table from multiple Variables

by SegFault22 » Post

It was a syntax error. I was trying to assign a value to an index that wasn't in a table, something like this:

Code: Select all

itemtype = {ingot}
materialtype = {metal}
material = {silver}

groups = {itemtype[1] = 1, materialtype[1] = 1, material[1] = 1}

minetest.register_craftitem("test:ingot_silver", {
	description = "Silver Ingot",
	inventory_image = "default_steel_ingot.png",
	groups = groups,
})
It was crashing when the table "groups" was declared, while trying to assign the number value "1" to one of the entries (the first one), which is not itself an index.

I should have been trying to do this (which actually works):

Code: Select all

itemtype = "ingot"
materialtype = "metal"
material = "silver"

groups = {}

groups[(itemtype)] = 1
groups[(materialtype)] = 1
groups[(material)] = 1

minetest.register_craftitem("test:ingot_silver", {
	description = "Silver Ingot",
	inventory_image = "default_steel_ingot.png",
	groups = groups,
})
I just didn't realize where it was breaking. Now that I think about it, I'm not sure why I didn't think about this before, while I was trying to get it to work.

Thank you for helping me with this; now it isn't a problem any more, and I can continue working on that mod.

User avatar
rubenwardy
Moderator
Posts: 6978
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy
Location: Bristol, United Kingdom
Contact:

Re: Trying to build a Groups Table from multiple Variables

by rubenwardy » Post

itemtype = {ingot}

Is equivalent to

Itemtype = {}
Itemtype[1] = ingot

Ingot is an undefined variable. That's why it didn't work.
Renewed Tab (my browser add-on) | Donate | Mods | Minetest Modding Book

Hello profile reader

Byakuren
Member
Posts: 818
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri
In-game: Raymoo + Clownpiece

Re: Trying to build a Groups Table from multiple Variables

by Byakuren » Post

SegFault22 wrote:It was a syntax error. I was trying to assign a value to an index that wasn't in a table, something like this:

Code: Select all

itemtype = {ingot}
materialtype = {metal}
material = {silver}

groups = {itemtype[1] = 1, materialtype[1] = 1, material[1] = 1}

minetest.register_craftitem("test:ingot_silver", {
	description = "Silver Ingot",
	inventory_image = "default_steel_ingot.png",
	groups = groups,
})
It was crashing when the table "groups" was declared, while trying to assign the number value "1" to one of the entries (the first one), which is not itself an index.

I should have been trying to do this (which actually works):

Code: Select all

itemtype = "ingot"
materialtype = "metal"
material = "silver"

groups = {}

groups[(itemtype)] = 1
groups[(materialtype)] = 1
groups[(material)] = 1

minetest.register_craftitem("test:ingot_silver", {
	description = "Silver Ingot",
	inventory_image = "default_steel_ingot.png",
	groups = groups,
})
I just didn't realize where it was breaking. Now that I think about it, I'm not sure why I didn't think about this before, while I was trying to get it to work.

Thank you for helping me with this; now it isn't a problem any more, and I can continue working on that mod.
You can use the keys in a table literal too, by doing something like

Code: Select all

local blahs = { "blah1" }
local millipede = "beef"

local tab = {
  [blahs[1]] = 42,
  [millipede] = 666,
}
If you wrap an expression with [], you can use the value it evaluates to as a key.
Every time a mod API is left undocumented, a koala dies.

User avatar
SegFault22
Member
Posts: 872
Joined: Mon May 21, 2012 03:17
Location: NaN

Re: Trying to build a Groups Table from multiple Variables

by SegFault22 » Post

Byakuren wrote: You can use the keys in a table literal too, by doing something like

Code: Select all

local blahs = { "blah1" }
local millipede = "beef"

local tab = {
  [blahs[1]] = 42,
  [millipede] = 666,
}
If you wrap an expression with [], you can use the value it evaluates to as a key.
I have been using something like this extensively; It's an important part of "fetching" data, such as the name of a material or its strength/density, using the "id" as the key in the table of all materials or whatever. I just didn't know that way to put the data into the groups table properly.

I would probably still be stuck trying to figure out what I was doing wrong, if it were not for your advice. Thank you.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests