我需要检查ROBLOX Lua中是否有孩子。我知道FindFirstChild(string)
找到名字匹配string
的第一个孩子,我一直在用它来查看实例中是否有某个孩子,但现在我想知道它是否有什么都没有。我希望有类似的东西:
if Instance:GetChildren() then
--Do something
end
我该怎么做?
答案 0 :(得分:2)
此方法将获取Instance子项的表,并检查它是否大于0,表示它有子项。
if #Instance:GetChildren() >0 then
--It has children!
end
答案 1 :(得分:0)
我建议使用井号运算符或table.getn
-- Hashtag
if(table.getn(Instance:GetChildren()) > 0) then
-- ...
end
if(#Instance:GetChildren() > 0) then
-- ...
end
答案 2 :(得分:0)
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
//...
static async Task<string> ReadAllTextAsync(string path)
{
switch (path)
{
case "": throw new ArgumentException("Empty path name is not legal.", nameof(path));
case null: throw new ArgumentNullException(nameof(path));
}
using var sourceStream = new FileStream(path, FileMode.Open,
FileAccess.Read, FileShare.Read,
bufferSize: 4096,
useAsync: true);
using var streamReader = new StreamReader(sourceStream, Encoding.UTF8,
detectEncodingFromByteOrderMarks: true);
// detectEncodingFromByteOrderMarks allows you to handle files with BOM correctly.
// Otherwise you may get chinese characters even when your text does not contain any
return await streamReader.ReadToEndAsync();
}
// ...
答案 3 :(得分:0)
这里有一种方法可以让您找到答案:
x = 0
for i, v in pairs(script:GetChildren()) do
x += 1
end
if x > 0 then
print("it has children")
end
它不是最有效的,但它非常简单且有效