lua - 为什么我的列表会清除/丢失其内容?[罗布乐思]
问题描述
local sounds = {
877986525;
2734549871;
}
local PrimaryQueue = {} -- Player Chosen Songs.
local SoundObj = workspace.MusicSystem
function PlaySoundAndWait(SoundId)
SoundObj.SoundId = "rbxassetid://"..SoundId
print("Loading Sound...")
repeat wait() until SoundObj.IsLoaded
print("Loaded")
SoundObj:Play()
repeat wait() until not SoundObj.Playing -- Wait till over.
end
local PlaySecondary = sounds
while wait(0.1) do
if #PrimaryQueue ~= 0 then
print("Play primary disregard current")
-- Play primary, disregard current.
PlaySoundAndWait(PrimaryQueue[1])
table.remove(PrimaryQueue,1) -- Remove from queue (played)
else
-- Refill Secondary Queue if empty.
if #PlaySecondary == 0 then
print("REFILL")
PlaySecondary = sounds
print(#sounds)
continue
end
print(PlaySecondary[1])
PlaySoundAndWait(PlaySecondary[1])
table.remove(PlaySecondary,1)
end
end
当我提到“REFILL”时,我指的是刷新列表的第 26 行。此脚本无限期地检查 PrimaryQueue 中是否有任何内容,如果有播放则将其删除。如果没有,它会检查 SecondaryQueue 是否为空,如果是,则用“声音”重新填充它。如果不是,它会播放第一个声音,然后将其删除。因此,所有这些都应该创建一个音乐系统,但由于某种原因,在重新填充时,声音列表显示为空。即使它不应该并且只被分配了一次值。
谢谢你。
解决方案
您正在执行table.remove(PlaySecondary, 1)
的操作等于,table.remove(sounds, 1)
因为它们都指向同一个表 PlaySecondary = sounds
,因此它返回为空,因为您自己之前删除了它的所有元素!
我假设您想创建表的副本:
PlaySecondary = {unpack(sounds)}
推荐阅读
- javascript - How to control Tamper Monkey dashboard Installed script, so that user cannot disable a script
- excel - 将excel sheet的数据与neo4j中的相关数据进行对比
- python - How would I loop the triangles/improve my code? (Nested triangle turtle)
- node.js - Loopback: Creating relations between models in different microservices
- python - Finding shortest sublist with sum greater than 50
- spring-webflux - Spring Cloud Contract EXPLICIT 和 WEBTESTCLIENT 测试模式
- appium - 跳过某些关键字的“捕获页面截图”
- python - 如何在条件下从数据框中选择多列和多行?
- html - flex-wrap: nowrap 与行和列
- php - 通过 axios 请求调用 php 文件已被 VUE CLI 3 中的 CORS 策略阻止