Help with metadata and on_receive_fields

deaf_fish
Member
 
Posts: 16
Joined: Sun Oct 21, 2012 00:02
GitHub: hansonry
IRC: hansonry
In-game: hansonry

Help with metadata and on_receive_fields

by deaf_fish » Sat Jul 02, 2016 15:57

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 :)


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,
   
 

User avatar
kaeza
Member
 
Posts: 2141
Joined: Thu Oct 18, 2012 05:00
GitHub: kaeza
IRC: kaeza diemartin blaaaaargh
In-game: kaeza

Re: Help with metadata and on_receive_fields

by kaeza » Sat Jul 02, 2016 23:39

You are modifying the ItemStack's metadata, but not setting it back in the inventory.

You probably want to `inv:set_stack("input", 1, stack)` somewhere.
Your signature is not the place for a blog post. Please keep it as concise as possible. Thank you!

Check out my stuff! | Donations greatly appreciated! PayPal | BTC: 1DFZAa5VtNG7Levux4oP6BuUzr1e83pJK2
 

deaf_fish
Member
 
Posts: 16
Joined: Sun Oct 21, 2012 00:02
GitHub: hansonry
IRC: hansonry
In-game: hansonry

Re: Help with metadata and on_receive_fields

by deaf_fish » Sun Jul 03, 2016 02:08

Yup, that was it. Thanks sooo much :)
 


Return to Modding Discussion

Who is online

Users browsing this forum: No registered users and 7 guests

cron