252 posts
Posted 18 October 2012 - 07:27 PM
Ok, so I am making this program. I am trying NOT to have the stack overflow error, and so this is what I am doing:
local UseProgram = true
function Exit()
UseProgram = false
term.clear()
term.setCursorPos(1,1)
shell.run("AnotherProgram")
end
function Program2()
input = read()
if input == "yay" then
Exit()
else
print("Yay!")
Exit()
end
end
end
function Program()
input = read()
if input == "yay" then
Program2()
else
Exit()
end
end
while UseProgram do
Program()
end
Basically it does that, but, the other functions that it runs only are used once when the program it running, then it goes into another program. Will this cause a stack overflow? Or avoid one? Please help, thanks in advance.
2088 posts
Location
South Africa
Posted 18 October 2012 - 08:40 PM
Yeah that should prevent stack overflows if I'm correct.
Why not just use while true do and then when you call exit make it return false?
252 posts
Posted 18 October 2012 - 08:49 PM
Yeah that should prevent stack overflows if I'm correct.
Why not just use while true do and then when you call exit make it return false?
How would I go about doing this? :3 I am not good with while loops :P/>/>
2088 posts
Location
South Africa
Posted 18 October 2012 - 08:51 PM
This should work
function Exit()
UseProgram = false
term.clear()
term.setCursorPos(1,1)
shell.run("AnotherProgram")
return false
end
function Program2()
input = read()
if input == "yay" then
Exit()
else
print("Yay!")
Exit()
end
end
end
while true do
input = read()
if input == "yay" then
Program2()
else
Exit()
end
end
252 posts
Posted 18 October 2012 - 09:00 PM
This should work
function Exit()
UseProgram = false
term.clear()
term.setCursorPos(1,1)
shell.run("AnotherProgram")
return false
end
function Program2()
input = read()
if input == "yay" then
Exit()
else
print("Yay!")
Exit()
end
end
end
while true do
input = read()
if input == "yay" then
Program2()
else
Exit()
end
end
So I could take out the "DoProgram" and the "DoProgram = false" then just do "return false"?
2088 posts
Location
South Africa
Posted 18 October 2012 - 09:39 PM
So I could take out the "DoProgram" and the "DoProgram = false" then just do "return false"?
Yep, just removes some unnecessary lines.
2447 posts
Posted 18 October 2012 - 09:44 PM
If another program is ran in another program which is ran in another program you will eventually get a stack overflow - it would just take a while.
2088 posts
Location
South Africa
Posted 18 October 2012 - 09:45 PM
If another program is ran in another program which is ran in another program you will eventually get a stack overflow - it would just take a while.
Programception.
But true true - how long exactly would it take?
252 posts
Posted 18 October 2012 - 09:45 PM
If another program is ran in another program which is ran in another program you will eventually get a stack overflow - it would just take a while.
What I am doing is having the other programs it runs have the same thing this does. With the while loop. Will this still cause a stack overflow?
2447 posts
Posted 19 October 2012 - 01:29 AM
If another program is ran in another program which is ran in another program you will eventually get a stack overflow - it would just take a while.
What I am doing is having the other programs it runs have the same thing this does. With the while loop. Will this still cause a stack overflow?
Yes. Eventually, since you'll fill up the stack with what was running before. Are you just wanting to exit the program entirely or start again?
252 posts
Posted 19 October 2012 - 01:51 AM
Well I am planning on having to exit back to a menu to execute other programs, so I guess exit it entirely until I want to reurn to it from the list.
1054 posts
Posted 19 October 2012 - 01:55 AM
Well I am planning on having to exit back to a menu to execute other programs, so I guess exit it entirely until I want to reurn to it from the list.
You can just use the 'return' statement from the other program (outside any function). It will exit and your code will resume from the line that started this other program.