I have a system that essentially is this:
while true do
print("Example code")
wait(2)
exit
That or I press any key to exit the loop.
I am new to lua and Computercraft so help is much appreciated.
while true do
print("Example code")
wait(2)
exit
local shouldStop = false
while not shouldStop do
print("Not stopping yet!")
local event, keyCode = os.pullEvent("key")
if keyCode == keys.space then
shouldStop = true
end
end
In the above example, the program will continue writing "Not stopping yet!" until you hit the correct key (in this case the spacebar).
local sleepTime = 0
while true do
print("Not stopping yet!")
sleep(1)
sleepTime = sleepTime + 1
if sleepTime > 5 then
break
end
end
2013-08-03 02:38:01 [INFO] [STDOUT] ComputerCraft: Error running task.
2013-08-03 02:38:01 [INFO] [STDERR] java.lang.NoClassDefFoundError: dan200/computer/core/IMountedFileNormal
2013-08-03 02:38:01 [INFO] [STDERR] at dan200.computer.core.FileSystem.<init>(FileSystem.java:25)
2013-08-03 02:38:01 [INFO] [STDERR] at dan200.computer.core.Computer.initFileSystem(Computer.java:626)
2013-08-03 02:38:01 [INFO] [STDERR] at dan200.computer.core.Computer.access$1000(Computer.java:33)
2013-08-03 02:38:01 [INFO] [STDERR] at dan200.computer.core.Computer$1.execute(Computer.java:865)
2013-08-03 02:38:01 [INFO] [STDERR] at dan200.computer.core.ComputerThread$1$1.run(ComputerThread.java:117)
2013-08-03 02:38:01 [INFO] [STDERR] at java.lang.Thread.run(Thread.java:680)
2013-08-03 02:38:01 [INFO] [STDERR] Caused by: java.lang.ClassNotFoundException: dan200.computer.core.IMountedFileNormal
2013-08-03 02:38:01 [INFO] [STDERR] at cpw.mods.fml.relauncher.RelaunchClassLoader.findClass(RelaunchClassLoader.java:139)
2013-08-03 02:38:01 [INFO] [STDERR] at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
2013-08-03 02:38:01 [INFO] [STDERR] at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
2013-08-03 02:38:01 [INFO] [STDERR] ... 6 more
Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9
for i=1,5 do
turtle.dig()
turtle.forward()
turtle.digUp()
end
Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9
How exactly do you want this to behave? The code is valid and should have the turtle do the thing endlessly - however, you have to hit a key every time you want the turtle to do something. If you want to the turtle to just continuously do something until you hit a key, you will need to use the parallel API or coroutines. These are somewhat more advanced topics, but if you'd like I can go into more detail.
Another alternative is to have the turtle just do something a certain number of times. For this we would use a for loop.
Example:for i=1,5 do turtle.dig() turtle.forward() turtle.digUp() end
for i=1,5 do
turtle.dig()
turtle.forward()
turtle.digUp()
end
local event, keyCode = os.pullEvent("key")
if keyCode == keys.space then
print("Stopping")
sleep(2)
exit()
end
mine 3
and it would mine 3 in that direction?Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9
How exactly do you want this to behave? The code is valid and should have the turtle do the thing endlessly - however, you have to hit a key every time you want the turtle to do something. If you want to the turtle to just continuously do something until you hit a key, you will need to use the parallel API or coroutines. These are somewhat more advanced topics, but if you'd like I can go into more detail.
Another alternative is to have the turtle just do something a certain number of times. For this we would use a for loop.
Example:for i=1,5 do turtle.dig() turtle.forward() turtle.digUp() end
Okay so I used example 1 and it would not loop, Code can be found here: http://pastebin.com/8CAkZYG9
How exactly do you want this to behave? The code is valid and should have the turtle do the thing endlessly - however, you have to hit a key every time you want the turtle to do something. If you want to the turtle to just continuously do something until you hit a key, you will need to use the parallel API or coroutines. These are somewhat more advanced topics, but if you'd like I can go into more detail.
Another alternative is to have the turtle just do something a certain number of times. For this we would use a for loop.
Example:for i=1,5 do turtle.dig() turtle.forward() turtle.digUp() end
So… Could i put a "for loop" inside for loop?