check = fs.exists("test")
am I able to run check or do I have to do this with a function? I was just wondering because a 1 line function just seems odd to me.
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Lua question
Started by grand_mind1, 21 January 2013 - 12:41 PMPosted 21 January 2013 - 01:41 PM
If I have something like
Posted 21 January 2013 - 03:18 PM
Not sure what you're asking. That line would see if the file named test exists and store the result (boolean true or boolean false) in the variable named check. Is that what you're trying to do?
Posted 21 January 2013 - 03:22 PM
Not sure what you're asking. That line would see if the file named test exists and store the result (boolean true or boolean false) in the variable named check. Is that what you're trying to do?
He is most likely using it in this context:
if check == true then
--do something with the "test" file
else
--create the "test" file
end
EDIT: Would be nice to see the code for better understanding
Posted 21 January 2013 - 04:06 PM
You only need to make a function for this if your code is going to use it multiple times otherwise you just leave it like that and use the "check" variable in whatever you're doing.
Posted 21 January 2013 - 06:33 PM
Sorry. I mean like could I do this:
check = fs.exists("disk")
while check == false do
print("Insert Disk")
sleep(1)
check --would I be able to do this?
end
Or would I have to make 'check' a function and call it. I would want to use this for checking if a disk is in a disk drive.Posted 21 January 2013 - 06:44 PM
If I'm reading what you're saying correctly, you want this?
function checkDisk()
if fs.exists("disk") then
return true
else
return false
end
end
check = false
while check == false do
print("Insert Disk")
sleep(1)
check = checkDisk()
end
Posted 21 January 2013 - 06:46 PM
Yes, you have answered my question. Thanks! Sorry if I was confusing.
Posted 21 January 2013 - 06:47 PM
Not a problem. :P/>Yes, you have answered my question. Thanks! Sorry if I was confusing.
Posted 21 January 2013 - 06:49 PM
If I'm reading what you're saying correctly, you want this?function checkDisk() if fs.exists("disk") then return true else return false end end check = false while check == false do print("Insert Disk") sleep(1) check = checkDisk() end
Whoa, seriously? Why not just this:
while not fs.exists("disk") do
print("Insert Disk")
sleep(1)
end
Posted 21 January 2013 - 06:52 PM
I thought I had to do it the more complicated way because you never check for fs.exists("disk") in the loop so it wall always be false even if you put in a disk.If I'm reading what you're saying correctly, you want this?function checkDisk() if fs.exists("disk") then return true else return false end end check = false while check == false do print("Insert Disk") sleep(1) check = checkDisk() end
Whoa, seriously? Why not just this:while not fs.exists("disk") do print("Insert Disk") sleep(1) end
Posted 21 January 2013 - 07:01 PM
I do currently program inefficiently, but I'm usually just happy it gets the job done..If I'm reading what you're saying correctly, you want this?function checkDisk() if fs.exists("disk") then return true else return false end end check = false while check == false do print("Insert Disk") sleep(1) check = checkDisk() end
Whoa, seriously? Why not just this:while not fs.exists("disk") do print("Insert Disk") sleep(1) end
Posted 21 January 2013 - 07:05 PM
I thought I had to do it the more complicated way because you never check for fs.exists("disk") in the loop so it wall always be false even if you put in a disk.
While loops check the conditional before the loop begins each time it loops, so whenever "disk" came into existence, the conditional would change and the loop would exit at that time.
Posted 21 January 2013 - 07:12 PM
Oh, ok! I guess I was just confused on how loops work. Thanks for your help! :D/>I thought I had to do it the more complicated way because you never check for fs.exists("disk") in the loop so it wall always be false even if you put in a disk.
While loops check the conditional before the loop begins each time it loops, so whenever "disk" came into existence, the conditional would change and the loop would exit at that time.
Posted 21 January 2013 - 07:16 PM
Oh, ok! I guess I was just confused on how loops work. Thanks for your help! :D/>/>
As stated the while loop checks the conditional before running the instructions. A for loop does the same. It checks the conditional before executing the instructions.
However a repeat loop is different. A repeat loop will always performs the instructions at least once before checking the conditional. Repeat loop syntax:
repeat
-- code to repeat
until <some condition>