Text Game Stack

I'm creating a text based game and inside of functions, I'm calling on other functions to go back.

Example:

I'm in the main menu.

I type shop.

It takes me to the shop function.

I do what I want and when I'm done I just call back to the main menu function.

What I'm wondering is will this type of navigation create a large stack?

If so, how can I close the current function when continuing to another?

[454 byte] By [Andyman2340a] at [2007-10-3 5:21:58]
# 1

You need to get to the end of a method call without calling a new one. Then the stack will stop growing...what you're describing could be bad.

this will cause a stack overflow quickly

public class Something

{

public static void main ( String [ ] args )

{

Something s = new Something ();

s.menu1 ();

}

public void menu1 ( )

{

menu2 ();

}

public void menu2 ( )

{

menu1 ();

}

}

Norweeda at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 2
OK, that's what I was wondering. I just have to call the function at the end of the current to clear the stack. Thanks!
Andyman2340a at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 3
I think you meant that I shouldn't include it into any loops or anything like that.
Andyman2340a at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 4
You method has to acually exit at some point. Meaning that it executed the last statement in the method and then moves on and goes back to what called it.If you always call a new method from within a method you'll get an overflow.
Norweeda at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 5

I think oyou are still a little confused. Norweed's example is bad because it will cause a stack overflow. When a method completes it will automatically go back to where it was called from.

public void mainMenu() {

shop();

// program will return from shop method to here

}

public void shop() {

// add code for shop here.

// method ends and returns to mainMenu

}

if you need the program to continue then you would place a loop in mainMenu.

public void mainMenu() {

loop until user enters exit or quit or whatever {

if user entered shop {

shop();

} else if user entered library {

library();

} else if user entered school {

school();

}

}

}

floundera at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 6

> I think oyou are still a little confused. Norweed's

> example is bad because it will cause a stack

> overflow. When a method completes it will

> automatically go back to where it was called from.

>

It's not a bad example, it's an example of what NOT to do. I said that.

Norweeda at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 7

> > I think oyou are still a little confused.

> Norweed's

> > example is bad because it will cause a stack

> > overflow. When a method completes it will

> > automatically go back to where it was called from.

> >

>

> It's not a bad example, it's an example of what NOT

> to do. I said that.

Thats what flounder meant.

CaptainMorgan08a at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 8

> > I think oyou are still a little confused.

> Norweed's

> > example is bad because it will cause a stack

> > overflow. When a method completes it will

> > automatically go back to where it was called from.

> >

>

> It's not a bad example, it's an example of what NOT

> to do. I said that.

That was the point. The OP seemed to think it was an example of what to do. I wonder if the OP comes from an FP background, what they're describing sounds like a Java version of CPS and will most assuredly cause a stack overflow eventually. Rather than specifying the next method execution should move to you should allow your method to return, either explicitly using "return" or implicitly by allowing it to reach the end of the method. Once it returns you're back to where it was invoked from. Generally a simple menu like you describe might be in a loop of some sort. When a selection is made the proper method to handle it is invoked. Once that method finishes it returns, the execution continues and the loop is run again.

kablaira at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 9
If someone thinks that it's a good example when I specifically type out this will cause a stack overflow quickly More power to them :-p
Norweeda at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 10
Let me clarify - It was a good example of what not to do. ie it was bad code!
floundera at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...
# 11
AAHHH :-)LOL, sorry bout that.
Norweeda at 2007-7-14 23:28:57 > top of Java-index,Java Essentials,Java Programming...