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

Struggeling struggle

Started by jag, 11 October 2012 - 05:59 PM
jag #1
Posted 11 October 2012 - 07:59 PM
Simple and small question:
So in a program that I'm making I want to be able to jump back to a certain line of the code.
Is this possible? Or do I have to redo my script to make this work?

EDIT: Btw, the file I'm working on is the startup file, so I can do a os.reboot()…

EDIT 2: Yeah well I went with the os.reboot(), just because it was the simplest way of handeling it.
nolongerexistant #2
Posted 11 October 2012 - 08:01 PM
Use functions? :P/>/>
PixelToast #3
Posted 11 October 2012 - 08:03 PM
not possible because lua isnt inturpreted directly from a file, it compiles individual chunks, loads them, and runs them
think with subroutines and or tables to acomplish what you are trying to do :P/>/>
jag #4
Posted 11 October 2012 - 08:09 PM
Use functions? :P/>/>
Well yeah, but I don't think that would work that great in the code.
You see, the code checks the answer for the menu, and then goes with options from there. But in one point in the code I got so that if the method falies then retry.

So for a clearer view of the code (step by step):
  1. checks for servers (in-game, via rednet)
  2. gives you the option to select a server
  3. once a server is selected a certain code will execute
  4. now, if it didn't find any servers it gives you the option to retry
  5. if you say "yes" it should run from step 1 again, else it will run some other code…
remiX #5
Posted 11 October 2012 - 08:17 PM
I saw a thread about a week ago where someone asked this, I remember that no one gave him an answer. So I doubt you can ;)/>/> Gna have to use functions :P/>/>
PixelToast #6
Posted 11 October 2012 - 08:26 PM
it might be harder than you think, if you pile functions you will get a stack overflow
im working on an api to make this easier to do, basically it will allow you to use a sort of goto command that will run functions but not use up the stack
also a os modification that will essentially make the stack go on forever
remiX #7
Posted 11 October 2012 - 08:41 PM
it might be harder than you think, if you pile functions you will get a stack overflow
im working on an api to make this easier to do, basically it will allow you to use a sort of goto command that will run functions but not use up the stack
also a os modification that will essentially make the stack go on forever

That would be awesome :3
jag #8
Posted 11 October 2012 - 08:49 PM
I got it working as I wanted, but maybe not the same way as I wanted…
But basically I just added some extra code to skip the other code if you reboot.
jag #9
Posted 11 October 2012 - 09:14 PM
Wait, I was wondering, can you run a function within a function?
That would actually do as I want (kind of)!
So for example if you got

local input = ""
function password()
	if input == "MyPassword" then
		return true
	else
		input = read("*")
		password()
	end
end

if password() then
    print("Correct password!")
end
Will this ^ work?
Orwell #10
Posted 11 October 2012 - 09:20 PM
Wait, I was wondering, can you run a function within a function?
That would actually do as I want (kind of)!
So for example if you got

local input = ""
function password()
	if input == "MyPassword" then
		return true
	else
		input = read("*")
		password()
	end
end

if password() then
	print("Correct password!")
end
Will this ^ work?

Calling 'password' from within 'password' is a bad idea. Recursion might overflow the stack. (Though you'd have to give input to read() a lot then :P/>/>)
faubiguy #11
Posted 11 October 2012 - 09:23 PM
You can use
local input = ""
function password()
    if input == "MyPassword" then
        return true
    else
        input = read("*")
        return password()
    end
end

if password() then
    print("Correct password!")
end

That's tail recursion and won't cause a stack overflow.
Lyqyd #12
Posted 11 October 2012 - 10:10 PM
Guys, this is exactly what loops are for.


local serverFound, serverStuff = false, nil
while not serverFound do
  print("Server?")
  serverStuff = attemptToFindServer(read())
  if serverStuff then
    serverFound = true
  else
    print("Try again?")
    local input = read()
    if string.lower(input) ~= "yes" then break end
  end
end
--Here, serverFound can be tested. It will be true if we found one and false if we gave up.
jag #13
Posted 11 October 2012 - 10:21 PM
Guys, this is exactly what loops are for.


local serverFound, serverStuff = false, nil
while not serverFound do
  print("Server?")
  serverStuff = attemptToFindServer(read())
  if serverStuff then
	serverFound = true
  else
	print("Try again?")
	local input = read()
	if string.lower(input) ~= "yes" then break end
  end
end
--Here, serverFound can be tested. It will be true if we found one and false if we gave up.
I didn't want to use a loop, but I guess it's the most compact option