Help with metadata and on_receive_fields
I am trying to save metadata to an ItemStack after the player presses the "Add" Button. For some reason I keep getting an empty string from get_metadata().
Has anyone run into this before? I am stuck, help :)
Has anyone run into this before? I am stuck, help :)
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
on_receive_fields = function(pos, formname, fields, sender)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local stack = inv:get_stack("input", 1)
local item_meta_str = stack:get_metadata()
local item_meta
print(item_meta_str)
if item_meta_str == "" then
item_meta = {
active = false,
access = {},
}
else
item_meta = minetest.deserialize(item_meta_str)
end
if fields["spacestation:computer_idcard_button"] == "Add" then
local perm_name = fields["spacestation:computer_idcard_text"]
if perm_name ~= "" then
table.insert(item_meta.access, perm_name)
end
end
print(minetest.serialize(item_meta))
stack:set_metadata(minetest.serialize(item_meta))
-- Build list string
local perm_list = table.concat(item_meta.access, ",")
local spec = "size[8,9]"..
"list[current_name;input;0,1;1,1;]"..
"tablecolumns[text]"..
"table[1,0;4,3;spacestation:computer_idcard_table;" .. perm_list .. ";1]"..
"button[5,1;3,1;spacestation:computer_idcard_button;Remove]"..
"field[1,4;4,1;spacestation:computer_idcard_text;New Permission;]"..
"button[5,4;2,1;spacestation:computer_idcard_button;Add]"..
"list[current_player;main;0,5;8,4;]"..
"listring[]"
meta:set_string("formspec", spec)
end,