Trying to build a Groups Table from multiple Variables

User avatar
SegFault22
Member
 
Posts: 870
Joined: Mon May 21, 2012 03:17

Trying to build a Groups Table from multiple Variables

by SegFault22 » Sun Oct 02, 2016 07:52

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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
Code: Select all
a = {metal = 1}
b = {ingot = 1}
c = {silver = 1}

the resulting groups table would be this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: Trying to build a Groups Table from multiple Variables

by Byakuren » Sun Oct 02, 2016 09:34

Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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: 870
Joined: Mon May 21, 2012 03:17

Re: Trying to build a Groups Table from multiple Variables

by SegFault22 » Sun Oct 02, 2016 14:48

It was a syntax error. I was trying to assign a value to an index that wasn't in a table, something like this:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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
Member
 
Posts: 4500
Joined: Tue Jun 12, 2012 18:11
GitHub: rubenwardy
IRC: rubenwardy
In-game: rubenwardy

Re: Trying to build a Groups Table from multiple Variables

by rubenwardy » Sun Oct 02, 2016 14:53

itemtype = {ingot}

Is equivalent to

Itemtype = {}
Itemtype[1] = ingot

Ingot is an undefined variable. That's why it didn't work.
 

Byakuren
Member
 
Posts: 441
Joined: Tue Apr 14, 2015 01:59
GitHub: raymoo
IRC: Hijiri

Re: Trying to build a Groups Table from multiple Variables

by Byakuren » Sun Oct 02, 2016 18:20

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:
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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):
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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: 870
Joined: Mon May 21, 2012 03:17

Re: Trying to build a Groups Table from multiple Variables

by SegFault22 » Sun Oct 02, 2016 23:01

Byakuren wrote:You can use the keys in a table literal too, by doing something like
Your phone or window isn't wide enough to display the code box. If it's a phone, try rotating it to landscape mode.
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.
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 14 guests

cron