209 posts
Location
Denmark
Posted 18 September 2014 - 07:14 PM
i was sitting and wondering while i was writing my test programs for CC
is there a GOTO command
for uses like this
ABC
bla bla
if bla bla = bla bla then
bla bla
bla bla
else
bla bla bla
GOTO ABC
end
just a quick example
10 posts
Posted 18 September 2014 - 07:21 PM
I don't understand what you want given that your example is… terrible.
I think you may be looking for loops, but I don't know what you want when you say GOTO.
3790 posts
Location
Lincoln, Nebraska
Posted 18 September 2014 - 07:36 PM
There is no GOTO option available in ComputerCraft. You can implement it if you really wanted to, but there's so many more efficient ways of coding.
If you want a particular part of code to repeat a certain number of times, or infinitely, you should look into loops.
Or, if you want to be able to call a certain part of your code at any time, use functions.
756 posts
Posted 18 September 2014 - 07:39 PM
There aren't any goto instruction in Lua, plus goto is a bad habit and is not necessary to make a program in any way.
The only thing that are remotely similar to goto are functions.
The only difference being that once the function has finished executing, it will resume to where it was called.
Example
1| local function example()
2| local i = 1
3| foo.bar()
4| foo.bar() // After this line, it will resume at line 9
5| end
6|
7| foo.bar()
8| example() // This will go to line 2
9| foo.bar()
209 posts
Location
Denmark
Posted 18 September 2014 - 09:06 PM
okay so no goto functions :(/>
they are pretty handy when making "program" that arent ment to be terminated, but just keep on running..
as i understand LUA so far, it runs once, and when it gets to the last line, it terminates the program..
an examble of a program that should NOT terminate, is login programs, for doors, or other systems, where you enter a "code" and something runs for a certain period of time or X number of times, and then it returns to the start again..
used that kind of programming a lot when i made programs for industrial robots, thats why i asked if there was a GOTO function..
its really nifty if you got multiple programs running from 1 computer, and each program has its own rutines, corutines and such..
the best way to demonstrate for purpose, and if others know "how to" get around this..
Spoiler
Main program
– 3 individual programs and their assigned "code"
prog.pulverizer = "pulver"
prog.redfurnace = "furnace"
prog.magmafluid = "magma"
ABC123 – the GOTO point
–the main code, which "controls" things
print("which program to run")
input = read()
if input = prog.* then
run prog.input(*)
else
GOTO ABC123
End
GOTO ABC123
there is 3 individual programs that you can run (instead of having 1 long code in 1 file, then have it in 3 seperate files..
that "program" would never terminate.
as far as i know Lua, it would actually terminate, if you run of the 3 programs, as there is nothing that forces it to "restart"
3790 posts
Location
Lincoln, Nebraska
Posted 18 September 2014 - 09:13 PM
No, you definitely want loops to accomplish this. A while loop will continue while the specified evaluation is true.
Here's a better explanation of loops and other control structures within Lua.
209 posts
Location
Denmark
Posted 18 September 2014 - 09:18 PM
No, you definitely want loops to accomplish this. A while loop will continue while the specified evaluation is true.
Here's a better explanation of loops and other control structures within Lua.
then you might want to show an example on how to do that, because, i dont see how a while look can accomplish it.
when you have entered the code once, it should run for (lets say 5 secs) and then it should return to where you enter the code again..
might just be me, who is missing a thing there :D/> hehe
3790 posts
Location
Lincoln, Nebraska
Posted 18 September 2014 - 09:33 PM
All right. Well here's a basic structure for a general program in Lua.
--#variables are usually defined at the top of the code, so that they're called first.
local foo = "bar"
--#define functions next. Functions are chunks of code that are assigned a name that can be called back to at any time during runtime.
local function turtleSpin(num) --#the variable in the parenthesis is something you can pass to the function later to affect how it operates.
for i = 1, num do --#This is a for loop. It will repeat the actions within it the specified number of times. In this instance, starting at 1, and stopping at the defined num
turtle.turnRight()
end
end
--#You can define as many functions as you want. Basically, if it's a bit of code you plan on repeating over and over, but don't want to have to write it over and over, put it in a function
while true do --#This is what's known as a 'while' loop. it will repeat until the equation passed to it no longer returns true. Since we are giving it the equation 'true', it will always return true, and is then an infinite loop.
term.setCursorPos(1,1)
write("How much spin: "
local num = tonumber(read())
turtleSpin(num)
--#Now, calling back to the function name 'turtleSpin()', we can have it repeat that chunk of code as often as we want.
end
7083 posts
Location
Tasmania (AU)
Posted 19 September 2014 - 01:51 AM
For example, this:
ABC
bla bla (1)
if bla bla = bla bla then
bla bla (2)
bla bla (2)
else
bla bla bla (3)
GOTO ABC
end
Can be written as this:
bla bla (1)
while bla bla ~= bla bla do
bla bla bla (3)
bla bla (1)
end
bla bla (2)
bla bla (2)
If the "(1)" segment is extensive, you might stick it in a function or add a second conditional check:
while true do
bla bla (1)
if bla bla == bla bla then break end
bla bla bla (3)
end
bla bla (2)
bla bla (2)