【问题标题】:Trying to insert table into table without creating a new table when saving to a file保存到文件时尝试将表插入表而不创建新表
【发布时间】:2021-09-20 03:46:16
【问题描述】:

我正在尝试将“食谱”(表格)添加到文件中以供将来查找。 我的问题是,当我将数据上传到文件中时,它会创建一个新表,并且不会作为嵌套表插入到“主”表中。当尝试在主“食谱书”中搜索“食谱”时,这会导致问题。 以下是项目这一部分的代码。我认为的问题是将数据上传到文件附近,但我很难过。谁能解释一下我到底做错了什么?

当我第二次运行代码时,它会将信息添加到主表中而没有问题,但它会将其添加到原始表下。 当我再次运行代码时,它只会添加到一个新表中。

代码:

--recursive search in--recursive search into tables
  local Deep = require "DeepPrint"
  
  --Working from the following info
  --http://www.computercraft.info/forums2/index.php?/topic/23076-crafting-api-learn-recipes/
  --the solution found: https://stackoverflow.com/questions/19299162/lua-table-expected-got-nilo
  
  --variables--
  --data holding place for recipes
  Recipes = {}
  --Crafting Grid
  grid = {1,2,3,5,6,7,9,10,11};
  
  --check for recipe book, if exists then read the data 
  local readRecipe = fs.open("recipeBook.lua","r")
  if readRecipe == nil then
      print("recipe book not found")
  else
      recipeList = readRecipe.readAll()
      readRecipe.close()
      ok, Recipes = pcall(textutils.unserialize, recipeList)
      if not ok then
          return false, recipeList
      end 
      if type(Recipes) ~= "table" then
          print("Main table not found, creating base table")
          Recipes = {}
      end 
  end
if readRecipe == nil then
      print("recipe book not found")
  else
      recipeList = readRecipe.readAll()
      readRecipe.close()
      ok, Recipes = pcall(textutils.unserialize, recipeList)
      if not ok then
          return false, recipeList
      end 
      if type(Recipes) ~= "table" then
          print("Main table not found, creating base table")
          Recipes = {}
      end 
  end
  
  --get recipe location and details to push into recipeBook.lua
  itemLayout = {}
  for key,value in pairs(grid) do
      turtle.select(value)
      if turtle.getItemDetail() then
       details = turtle.getItemDetail()
       table.insert(itemLayout,{
       name = details.name,
       count = details.count,
       location = value
       })
       end
  end
--craft item to get the name of crafted item
  turtle.select(16)
  turtle.craft()
  itemMade = turtle.getItemDetail()
  
  --check if table exists and if not creates a table
  if not Recipes[itemMade.name] then
      print("didnt find the table for item, creating new table")
      Recipes[itemMade.name] = {}
  else
      print("found recipe")
  end
  
  --add in info into main product table
  Recipes[itemMade.name]["components"] = itemLayout
  Recipes[itemMade.name]["quantityMade"] = itemMade.count
  
  --add recipe to list
  local ok, str = pcall(textutils.serialize, Recipes)
  if not ok then
      return false, str
  else
  book = fs.open("recipeBook.lua","a")
  book.writeLine(str)
  book.close()
  end

生成以下类型的表格

{
    [ "minecraft:crafting_table" ] = {
      components = {
        {
          location = 1,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 2,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 5,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 6,
          name = "minecraft:oak_planks",
          count = 1,
        },
      },
      quantityMade = 1,
    },
  }
{
    [ "minecraft:oak_button" ] = {
      components = {
        {
          location = 1,
          name = "minecraft:oak_planks",
          count = 1,
        },
      },
      quantityMade = 1,
    },
    [ "minecraft:crafting_table" ] = {
      components = {
        {
          location = 1,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 2,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 5,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 6,
          name = "minecraft:oak_planks",
          count = 1,
        },
      },
      quantityMade = 1,
    },
  }
  {
    [ "minecraft:oak_button" ] = {
      components = {
        {
          location = 1,
          name = "minecraft:oak_planks",
          count = 1,
        },
      },
      quantityMade = 1,
    },
  }
{ 
    [ "minecraft:oak_pressure_plate" ] = {
      components = {
        { 
          location = 1,
          name = "minecraft:oak_planks",
          count = 1,
        },
        { 
          location = 2,
          name = "minecraft:oak_planks",
          count = 1,
        },
      },
      quantityMade = 1,
    },
  }
{
    [ "minecraft:crafting_table" ] = {
      components = {
        {
          location = 1,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 2,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 5,
          name = "minecraft:oak_planks",
          count = 1,
        },
        {
          location = 6,
          name = "minecraft:oak_planks",
          count = 1,
        },
      },
      quantityMade = 1,
    },
  }

【问题讨论】:

    标签: lua minecraft computercraft


    【解决方案1】:

    我发现了这个问题。

    1. 是因为我将它附加到文件中,而不是在将信息上传回文件时写入。

    2)第二个是变量Recipes开头的无意义数组,甚至在开始从文件中收集信息之前。这导致上传有另一个不需要的巢

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-18
      • 2014-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-01-03
      • 1970-01-01
      • 2013-12-01
      相关资源
      最近更新 更多