對數組進行更改

*此內容是使用 AI(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() 來測試搜尋功能。傳遞到搜索的 array,以及應該搜尋的值。 執行 代碼以確認預期輸出是索引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)

移除值

如果使用尋找函數找到值,值可以被移除。檢查是否使用 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. 使用名為 playerItems 的陣列,至少包含四個值和一組複製值 **** 。


    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 可用於停止循環並將需要的資訊傳回所需位置。

完成的腳本版本可以在下面參考。

下列項目包含本教學中的所有腳本。下載 在這裡 .

請注意所有腳本都在 ServerScriptService已停用 中。要使用腳指令碼,在其屬性中,卸下「停用」欄位,然後運行 Studio。