对阵列进行更改

*此内容使用人工智能(Beta)翻译,可能包含错误。若要查看英文页面,请点按 此处

阵数组的内容经常需要更改,例如当需要从玩家的道具中删除物品时。Luau有 内置函数 用于与表一起使用,以便简化这一过程。

本文将概述预制函数,用于像添加和删除这样的表,同时描述如何搜索值。

要应用这些技能,你将创建一个脚本,可以帮助管理游戏中玩家的库存。

向阵列添加值

要将新值添加到阵数组中,请使用 table.insert(array, valueToInsert)。第二个参数可以是任何值,例如字符串、数字或整个对象,例如 PlayerIntValue

要练习,你会创建一个脚本,将玩家物品存储在表中,然后添加到它。由于这个例子只是一个脚本,它可以与任何文件或项目一起使用。测试代码将使用输出窗口进行。

  1. 创建一个名为 playerItems 的空阵列。


    playerItems = {}
  2. 通过输入 table.insert() 将库存物品添加到阵列。在括号中,键入 playerItems , 要添加的表,随后是每个项目的字符串。插入至少三个值。


    playerItems = {}
    table.insert(playerItems, "Potion")
    table.insert(playerItems, "Bread")
    table.insert(playerItems, "Sleeping Bag")
    print(playerItems)
  3. 运行项目。在输出中,扩展三点 ... 以查看打印的表。

从阵列中删除值

要删除值,如果玩家使用了物品或活跃玩家列表中的人离开了体验,请使用 table.remove() 。根据提供的参数,函数可以删除表的最后值或特定索引的值。

移除最后一个值

有时需要脚本删除特定物品目。例实例,移除玩家的库存中的第一个项目,或选择列表中的第一个玩家。要删除阵数组中最后一个值,请使用 table.remove(arrayName) 。在这种使用案例中,唯一需要的参数是表本身。

  1. 使用前面的例子中的物品阵列。然后,要从表中删除最后一个值,在这里 "Sleeping Bag" , 输入 table.remove(playerItems)


    playerItems = {}
    table.insert(playerItems, "Potion")
    table.insert(playerItems, "Bread")
    table.insert(playerItems, "Sleeping bag")
    table.remove(playerItems)
    print(playerItems)
  2. 运行项目。在输出窗口中,最后一个值,"Sleeping Bag",不应打印。

按索引删除

要在数组列的特定位置移除值,在第二个参数中输入要移除的索引,例如 table.remove(arrayName, 1)

  1. 使用相同的表,键入 table.remove() 。在参数中,输入表名和 1 ,要移除的值。


    playerItems = {}
    table.insert(playerItems, "Potion")
    table.insert(playerItems, "Bread")
    table.insert(playerItems, "Sleeping bag")
    table.remove(playerItems, 1)
  2. 运行项目。检查第一个值“药水”已被移除。

在数组列中搜索值

要在阵列中找到特定值,例如赢玩家的名字,请使用 table.find() 函数。或者,你可以使用 for 循环和 if 语句编写自己的搜索功能。

找到并返回单个值

要在数组列中找到值,创建一个名为 findValue() 的函数,该函数穿过阵列并在第一次找到匹配值时停止。

一旦找到值,函数将使用 return 关键字返回该值的索引。有了索引,您可以从数组列中删除它。

  1. 复制以下名为 playerItems 的阵列。


    local playerItems = {
    "Potion",
    "Bread",
    "Bread",
    "Sleeping Bag"
    }
  2. 编写一个名为 findValue() 的新函数,带有两个参数:

    • whichArray - 要搜索的阵列。

    • itemName - 要检查的特定字符串。


      local function findValue(whichArray, itemName)
      end
  3. findValue() 中,使用 for 循环检查阵列中是否有值匹配 itemName .如果值匹配,使用关键字 return 返回在 currentIndex 中找到的值。


    local function findValue(whichArray, itemName)
    for currentIndex = 1, #whichArray do
    if whichArray[currentIndex] == itemName then
    return currentIndex
    end
    end
    end
  4. 通过创建变量 valueFound 并调用 findValue() 来测试搜索功能。将阵列传递到搜索,以及应该搜索的值。 运行 代码以确认期望的输出是索引 2。


    local function findValue(whichArray, itemName)
    for currentIndex = 1, #whichArray do
    if whichArray[currentIndex] == itemName then
    --将当前索引的值返回
    return currentIndex
    end
    end
    end
    local valueFound = findValue(playerItems, "Bread")
    print("The value is at index " .. valueFound)

移除一个值

如果使用 find 函数找到了值,可以删除它。检查是否使用了 if 语句找到了值。

  1. 检查值是否在 valueFound 内;如果是,使用 table.remove() 移除值。


    if valueFound then
    table.remove(playerItems, valueFound)
    end
  2. 使用以下代码打印阵列。


    for index = 1, #playerItems do
    local itemString = playerItems[index]
    print("Index " .. index .. ": " .. itemString)
    end
  3. 播放测试并检查第一个 "Bread" 值已从数组列中删除。尝试通过更改第二个参数来删除其他值 findValue()

    注意,由于此函数只被调用一次,只有第一个实例 "Bread" 被删除。下一节将涵盖如何找到并移除所有实例。

找到并移除特定值的所有内容

虽然前面的代码只能删除找到的值的第一个实例,但这段代码片段会找到并移除阵数组中的所有出现。例如,如果,说,一名玩家想在游戏商店中出售所有面包。

  1. 使用名为 的阵列,至少包含四个值和一组重复值。


    local playerItems = {
    "Potion",
    "Bread",
    "Bread",
    "Sleeping Bag"
    }
  2. 要通过阵数组,创建一个 for 循环,从 向后 通过 playerItems ,从 #playerItems ,结束于 1, 并增加 -1。


    for index = #playerItems, 1, -1 do
    end
  3. 在循环中,使用 if 语句检查 playerItems[index] 内的值是否等于 "Bread",如果是,请移除该物品。


    for index = #playerItems, 1, -1 do
    if playerItems[index] == "Bread" then
    table.remove(playerItems, index)
    end
    end
  4. 使用以下代码添加一个第二个循环,打印阵数组。


    for index = 1, #playerItems do
    local itemString = playerItems[index]
    print("Index " .. index .. ": " .. itemString)
    end
  5. 运行脚本并检查所有命名为“面包”的值都已删除。

摘要

值可以被移除或添加到阵数组,但要注意索引移动导致的结果。使用循环遍历表以移除所有值的实例,或仅找到第一个实例。

关键字 return 可用于停止循环,将需要的信息发送回需要的地方。

脚本的完成版本可以在下面参考。

以下项目包含本教程中的所有脚本。下载 这里 .

请注意,所有脚本都在 服务器脚本服务已禁用 中。要使用脚本,在其属性中,取消禁用字段,然后运行 Studio。