- wdata.ser_inv=advtrains.serialize_inventory(inv)
+ if inv then -- inventory is not initialized when wagon was never loaded
+ -- TOOD: What happens with unloading rails when they don't find the inventory?
+ wdata.ser_inv=advtrains.serialize_inventory(inv)
+ end
end
-- TODO apply save-keys here too
end
@@ -77,23 +77,27 @@ function wagon:set_id(wid)
if self.has_inventory then
--to be used later
- local inv=minetest.create_detached_inventory("advtrains_wgn_"..self.id, {
- allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
- return invcallback(wid, player:get_player_name(), count, 0)
- end,
- allow_put = function(inv, listname, index, stack, player)
- return invcallback(wid, player:get_player_name(), stack:get_count(), 0)
- end,
- allow_take = function(inv, listname, index, stack, player)
- return invcallback(wid, player:get_player_name(), stack:get_count(), 0)
+ local inv=minetest.get_inventory({type="detached", name="advtrains_wgn_"..self.id})
+ -- create inventory, if not yet created
+ if not inv then
+ inv=minetest.create_detached_inventory("advtrains_wgn_"..self.id, {
+ allow_move = function(inv, from_list, from_index, to_list, to_index, count, player)
+ return invcallback(wid, player:get_player_name(), count, 0)
+ end,
+ allow_put = function(inv, listname, index, stack, player)
+ return invcallback(wid, player:get_player_name(), stack:get_count(), 0)
+ end,
+ allow_take = function(inv, listname, index, stack, player)