I need to check if something has children in ROBLOX Lua. I know of FindFirstChild(string) which finds the first child with a name matching string, and I have been using that to see if the instance has a certain child in it, but now I want to see if it has ANY at all. I was hoping for something like:
if Instance:GetChildren() then
--Do something
end
How do I do something like that?
This method will get a table of the Instance's children and check if it is more than 0, meaning it has children.
if #Instance:GetChildren() >0 then
--It has children!
end
I suggest using the hashtag operator or table.getn
-- Hashtag
if(table.getn(Instance:GetChildren()) > 0) then
-- ...
end
if(#Instance:GetChildren() > 0) then
-- ...
end
if Object.GetChildren() then
--code here
end
here's one way you could find that out:
x = 0
for i, v in pairs(script:GetChildren()) do
x += 1
end
if x > 0 then
print("it has children")
end
it's not the most efficent but it's pretty simple and works
Related
I am trying to move my i location forward and backward if a certain string arrives kind of like assembly code, is this possible using lua?
something like this:
local array = {"Hi", "Goodbye", "Cat"}
for i in pairs(array) do
if string.find(array[i], "Hi") then
move_to(3) -- so it will basically skip over "Goodbye" but I need this on a large scale so it can jump from 3 to 234 and then jump back to 1 etc etc
elseif array[i] == "Cat" then
print("Cat")
end
end
Also I cannot just make a variable to check if a jump is in progress and just ignore the other values until I am at the descried location, because then I cannot jump backwards
Thanks!
You cannot use a numeric for loop or a generic for loop with the standard iterator functions to do this. You cannot properly control their state from inside. Just use a while loop.
while notDoneCondition do
-- do stuff that may trigger the jumpCondition
if jumpCondition then
pos = jumpTarget
end
end
I'm looking to iterate over some similarly named variables. a_1,a_2,a_3=1,2,3
So that instead of using:
if a_1>0 then a_1-=1 end
if a_2>0 then a_2-=1 end
if a_3>0 then a_3-=1 end
I can do something like:
for i=1,3 do
if a_'i'>1 then a_'i'-=1 end --syntax is wrong here
end
Not sure how to go about doing this, as stated there is no access to _G library in pico8. var-=1 is just var=var-1. Given there are functions like tostr() and tonum() was wondering if there was a tovar() kind of trick to this. Basically need a way to convert the i value to a letter in my variable name and concat it to the variable name...in the conditional statement. Or some alternative method if there is one.
In Lua, when in doubt, use a table.
In this case you could put the 3 variable on a table at the start, and unpack them at the end:
local a={a_1,a_2,a_3}
for i=1,3 do
if(a[i]>1) a[i]-=1
end
a_1,a_2,a_3=unpack(a)
Not sure which Lua version does pico 8 use but for LuaJIT, you could try using loadstring and there is load for Lua5.2+ (I know, not the best solution):
for i = 1, 3 do
local x
loadstring("x = a_" .. tostring(i))()
if x > 1 then
x = x - 1
loadstring("a_" .. tostring(i) .. " = x")()
end
end
I'm trying to find something in a loop in Lua, and when I'm done I need to use the location I found.
for j = 1,100 do
<do some stuff>
if <some test> then
break
end
end
if j >= 100 then
return
end
Unfortunately, I get an error which suggests that after the for loop exits, the value of j is nil. How do I use the value that j ended at? Obviously I could create an extra variable and assign it right before I break, but that just seems wrong, and I've never seen another language set the loop variable to nil when the loop ends, so I'm wondering if there isn't a better way to accomplish this.
The loop variable is only visible inside the for loop block. You can get around this by creating another variable as suggested in PIL 4.3.4 Numeric For.
local index
for j=1,100 do
if j == 10 then
index = j
end
end
Alternatively, if you are a doing a common operation then using a function with an early return may be best.
function find(tbl, val)
for i, v in ipairs(tbl) do
if v == val then
return i
end
end
end
I'm trying to find out a way to use a multiline comment on a batch of code, but it keeps mistaking some syntax in it as a ]] and thinking I want it to end there, which I don't!
--[[
for k,v in pairs(t) do
local d = fullToShort[k]
local col = xColours[v[1]] -- It stops here!
cecho(string.format(("<%s>%s ", col, d))
end
--]]
I thought I read somewhere it was possible to do use a different sort of combination to avoid those errors, like --[=[ or whatnot... Could someone help?
As you can see in Strings tutorial there is a special [===[ syntax for nesting square braces. You can use it in block comments too. Just note, that number of = signs must be the same in open and close sequence.
For example 5 equals will work.
--[=====[
for k,v in pairs(t) do
local d = fullToShort[k]
local col = xColours[v[1]] -- It stops here!
cecho(string.format(("<%s>%s ", col, d))
end
--]=====]
You can use the following to create multiline comments past ]]'s:
--[[
codes
]]
In my lua program, i want to stop and ask user for confirmation before proceeding with an operation. I'm not sure how to stop and wait for user input, how can it be done?
local answer
repeat
io.write("continue with this operation (y/n)? ")
io.flush()
answer=io.read()
until answer=="y" or answer=="n"
Take a look at the io library, which by default has standard-input as the default input file:
http://www.lua.org/pil/21.1.html
I've worked with code like this. I will type this in a way it will work:
io.write("continue with this operation (y/n)?")
answer=io.read()
if answer=="y" then
--(put what you want it to do if you say y here)
elseif answer=="n" then
--(put what you want to happen if you say n)
end
I use:
print("Continue (y/n)?")
re = io.read()
if re == "y" or "Y" then
(Insert stuff here)
elseif re == "n" or "N" then
print("Ok...")
end
try to use folowing code
m=io.read()
if m=="yes" then
(insert functions here)
end
print("Continue (y/n)?")
re = io.read()
if re == "y" or "Y" then
(Insert stuff here)
elseif re == "n" or "N" then
print("Ok...")
end
From the bit of lua that I've done (not a lot), I'm going to say that using both uppercase and lowercase letters is redundant if you use string.sub.
print("Continue? (y/n)")
local re = io.read()
--[[Can you get string.sub from a local var?
If so, this works. I'm unfamiliar with io(game
lua uses GUI elements and keypresses in place of the CLI.]]
if re.sub == "y" then
--do stuff
if re.sub == "n" then
--do other stuff
end
That should work.