521 posts
Location
Stockholm, Sweden
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.
160 posts
Location
Netherlands
Posted 11 October 2012 - 08:01 PM
Use functions? :P/>/>
2217 posts
Location
3232235883
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/>/>
521 posts
Location
Stockholm, Sweden
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):
- checks for servers (in-game, via rednet)
- gives you the option to select a server
- once a server is selected a certain code will execute
- now, if it didn't find any servers it gives you the option to retry
- if you say "yes" it should run from step 1 again, else it will run some other code…
2088 posts
Location
South Africa
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/>/>
2217 posts
Location
3232235883
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
2088 posts
Location
South Africa
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
521 posts
Location
Stockholm, Sweden
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.
521 posts
Location
Stockholm, Sweden
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?
1054 posts
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/>/>)
231 posts
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.
8543 posts
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.
521 posts
Location
Stockholm, Sweden
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