Making a program "skip" to another part.

Nov 21, 2009 at 4:21pm
I'm making a game and i was thinking about what happens when someone dies and i want to make it so they go back to a certain point like the last shop or w/e. My question is as thus: How do you write it so that the code will take you "back" to another point in your program? or is there even a way to do so. BTW i'm doing this in C++ on visual studio.
Last edited on Nov 21, 2009 at 4:27pm
Nov 21, 2009 at 4:25pm
For games a way could be saving the needed information ( eg: the location ) and then loading them when needed.
To repeat code, you have loops
Nov 23, 2009 at 1:34am
Use the "goto" keyword.

1
2
3
4
5
SomePoint:

....blahblahblahblah....

goto SomePoint; // Go back up to SomePoint ^ 
Nov 23, 2009 at 1:57am
Use the "goto" keyword.


No, don't EVER use the goto keyword when you are trying to do something like this, it's not needed and will simply make your code harder to understand/maintain.
Nov 23, 2009 at 3:30am
Good god never use goto its evil, just use loops, functions and if/else or switch.

For your particular problem just create a few flags that if the player dies he is resurrected at the last flag.
Nov 23, 2009 at 12:19pm
Good god never use goto its evil

There are cases when goto is the best option.
Nov 23, 2009 at 4:11pm
Abramus wrote:
There are cases when goto is the best option.


Agreed. But this isn't one of them.
Nov 23, 2009 at 9:13pm
In order to solve the issue of whether or not to use "goto" (I know it's shun upon but it is really useful in some cases) could you post the code your having trouble with?
Nov 23, 2009 at 10:04pm
I'm making a game and while i would love to post the game it's rather long, but in summery it's a mass of functions that simulate battles and towns and other various activities, mostly i would like to know if theres a way to make it skip back to a particular function? say the last town or w/e, thanks in advance for any advice and links that would help.
Nov 23, 2009 at 10:26pm
It's possible to goto from one function to another, but having to do that is a serious sign of bad design.
Dec 4, 2009 at 12:22am
i don't see how it's a bad sign of design, since i know of no other way of making checkpoints in a game, because if you die i don't want to just end it. but anyway how do make a checkpoint that once you past it if you "die" it sends you back to the last "checkpoint"?
Dec 4, 2009 at 1:07am
That's exactly where the bad design is. You're coding the game itself in C++. Games like, say, text adventures, are written in a special purpose language specifically designed for that game. The program that implements the language is known as the engine. For games past a certain complexity (for example, games that save state), a game engine is a must.
Dec 5, 2009 at 6:34pm
Suggestion: Make a prototype with Mark Overmar's Game Maker 7.0, then once you've implemented a checkpoint feature with that game engine, find a way to 'convert' it to C++.
Dec 7, 2009 at 5:55pm
Surely, when the player dies you should only need to change the data that holds where the player is located on the map?

So each time you start the game, you check the player's position in order to decide which function to begin calling:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

int function_start(int checkpoint)
{
	bool success;

	//do game stuff

	if(success)
	{
		return checkpoint + 1; // goto next checkpoint
	}
	return checkpoint; // repeat last checkpoint
}

int funcion_woods(int checkpoint);
int funcion_town(int checkpoint);
int funcion_house(int checkpoint);


int main()
{
	int checkpoint(0);
	bool dead(false);

	while(!dead)
	{
		switch(checkpoint)
		{
			case 0: checkpoint = funcion_start(checkpoint);
			break;
			case 1: checkpoint = funcion_woods(checkpoint);
			break;
			case 2: checkpoint = funcion_town(checkpoint);
			break;
			case 3: checkpoint = funcion_house(checkpoint);
			break;
			default: checkpoint = 0;
		}
	}
}
Dec 8, 2009 at 8:49am
So is it that depending on user input you want to go back to some particular point or just automatically you want to go back?
Topic archived. No new replies allowed.