This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
grand_mind1's profile picture

Lua question

Started by grand_mind1, 21 January 2013 - 12:41 PM
grand_mind1 #1
Posted 21 January 2013 - 01:41 PM
If I have something like

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.
Lyqyd #2
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?
SuicidalSTDz #3
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
Axolotl #4
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.
grand_mind1 #5
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.
brett122798 #6
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
grand_mind1 #7
Posted 21 January 2013 - 06:46 PM
Yes, you have answered my question. Thanks! Sorry if I was confusing.
brett122798 #8
Posted 21 January 2013 - 06:47 PM
Yes, you have answered my question. Thanks! Sorry if I was confusing.
Not a problem. :P/>
Lyqyd #9
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
grand_mind1 #10
Posted 21 January 2013 - 06:52 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
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.
brett122798 #11
Posted 21 January 2013 - 07:01 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
I do currently program inefficiently, but I'm usually just happy it gets the job done..
Lyqyd #12
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.
grand_mind1 #13
Posted 21 January 2013 - 07:12 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.
Oh, ok! I guess I was just confused on how loops work. Thanks for your help! :D/>
theoriginalbit #14
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>