Struggeling struggle
Started by jag, Oct 11 2012 06:59 PM
12 replies to this topic
#1
Posted 11 October 2012 - 06: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.
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.
#2
Posted 11 October 2012 - 07:01 PM
Use functions?
/>
#3
Posted 11 October 2012 - 07: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
/>
think with subroutines and or tables to acomplish what you are trying to do
#4
Posted 11 October 2012 - 07:09 PM
Snakybo, on 11 October 2012 - 07:01 PM, said:
Use functions?
/>
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...
#5
Posted 11 October 2012 - 07: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
/>
#6
Posted 11 October 2012 - 07: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
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
#7
Posted 11 October 2012 - 07:41 PM
PixelToast, on 11 October 2012 - 07:26 PM, said:
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
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
#8
Posted 11 October 2012 - 07: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.
But basically I just added some extra code to skip the other code if you reboot.
#9
Posted 11 October 2012 - 08: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
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?
#10
Posted 11 October 2012 - 08:20 PM
jag_e_nummer_ett, on 11 October 2012 - 08:14 PM, said:
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
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
#11
Posted 11 October 2012 - 08:23 PM
You can use
That's tail recursion and won't cause a stack overflow.
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.
#12
Posted 11 October 2012 - 09: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.
#13
Posted 11 October 2012 - 09:21 PM
Lyqyd, on 11 October 2012 - 09:10 PM, said:
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.
3 user(s) are reading this topic
0 members, 3 guests, 0 anonymous users











