Sign in to follow this  

how to return to a certant line

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

two questions. first, how do I return to a specific line of code. I need to simply return to an earlier line of code, but dont know how. I need this becuase I am making a battle system for an rpg. I need it to go through the battle phase, and restart it, so it will go on forever until either the player or monster dies. second, any good site for rpg developmetn? I really have no idea what makes a good rpg.

Share this post


Link to post
Share on other sites
Ummm, this would involve the use of goto's bue the goto command is a sin to programming. You should be able to do what u want without it. If you employ some sort of Finite State Machine u can stay in the same battle state and loop.

So do something like


bool gameOver = false;
int currentState = 1; // supposing this is a battle state

while (!gameOver)
{
switch(currentState)
{
case 1:
{
//do battle
break;
}
}
}


Hope this helps a bit.

ace


[/source]

Share this post


Link to post
Share on other sites
Basically what you are looking for is a while loop

Basically

While (condition)
{
CODE HERE
}

it will end when the condition is changed. For instance

While (condition == true)
{
CODE HERE
}

will go until condition equals false

Share this post


Link to post
Share on other sites
First question: This is called a loop. Assuming C++: there are while, do... while and for. Choose the one that suits you best.

Second question: Learn to program first, seeing as you did not hear of loops, you aren't quite ready for a RPG yet.

Share this post


Link to post
Share on other sites
you can use a do while loop.

you code everything in the do { } statement, then at the end, you put the conditions in the while statement
like the following pseudocode:

do
{
fight monster
}
while(either you or the monster are alive);

it'll loop while the condition in the while command is true.

I've always been told to refrain from using GOTO-like commands, as they tend to be far less memory efficient than loops, and they look ugly too.

your do while loops can be nested as well, if you need.

Share this post


Link to post
Share on other sites
Nobody is going to tell you how to use goto because goto is a last resort hack that should be avoided if at all possible. Yes, there are uses for it, but you don't want to use it unless you really haven't got a better solution. 9 times out of 10, if goto is being used, it's because of poor design, not because it was a good place to use it.

Share this post


Link to post
Share on other sites
Quote:
Original post by jakpandora
I know about loops, I just didnt think of using them. thought somebody would finally tell me how to use goto...........
anyways thanks.



void PerformBattle()
{
battlestart: // goto label

... all your code

goto battlestart;

.. finished fighting

return;
}


As others have said, using the goto call is not the right sollution to this, but it is still useful. Go and use it here, and then you will realize the limitations. Right now you probably want to see results; programming shouldn't always be about 'doing it right' when you are trying to learn. Get something working, and keep adding to it, and eventually you will see you probably want a state machine or a loop:

void PerformBattle()
{
bool bKeepFighting = true;

while (bKeepFighting)
{
.. all your battle code
.. if you want to keep fighting, set bKeepFighting to true
.. if not, set it to false
}

return;
}


You will want to end up breaking your fighting code into a different function, eventually, so call out of your loop into a different function then.

Share this post


Link to post
Share on other sites
Quote:
Original post by jakpandora
I know about loops, I just didnt think of using them. thought somebody would finally tell me how to use goto...........
anyways thanks.


Don't use goto for looping, use loops for looping. 99% of the evils of goto can be avoided if you follow one simple rule:

Always jump forward

There are two places that I've seen the most agreement that goto is ok (though there is still some argument):

1) Clean up after an error in C (in C++ you should learn about RAII)

2) Breaking out of multiple loops (some say that if you have to do this you should put the loop in another function, but I personally (your mileage may vary) find this solution worse than the problem)

Share this post


Link to post
Share on other sites

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this