ending prog outside of main()

Started by
11 comments, last by Iron Eye 21 years, 6 months ago
I''m working on a lame text rpg, i can detect death, however the game continues, return 0; wont kill the app after i say game over, and then you just keep playing... Now I could write if thens into the turn code, like if my function returns some special number then quit game, but thats horribly inefficent i think. Is there an easier way to solve this such as killing the game from an outside function? -Kris
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
Advertisement
If you really want to quit the program from a function other than main(), use the exit() function.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
When i try to use exit() the compiler says: error line 325 in stdlib.h to few arguments for void exit()

what do i have to provide exit()? a number in seconds till exit?
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
Hey, I''m working on a lame text RPG too.

How far into development are you??

I designed the different classes and races, set up my main menu, designed weapons, and started my main game loop structure.

How experienced at C++ are you (I assume you''re using C++).
Check out Drunken Brawl at http://www.angelfire.com/games6/drunken_brawl!
exit() takes a single parameter, the exit status code. In general, 0 is assumed to mean normal termination, and non-zero is assumed to mean abnormal termination. So just use exit(0) or exit(1). It doesn''t matter too much unless you are actually checking if a program completed successfully (which you might do, for example, in a shell script).
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
I started this past saturday learning C++, mines really lame in comparison to yours, all im working on right now is a battle system, when you start the game, its just like monster attacking, and so on, almost done with that.


-Kris
---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
As a rule of thumb, if you''re using exit for something other than an error condition, you''re doing something wrong.
I thought of using the checkfordeath() function i use to set a variable to something, and have the games main code in main() to loop while that variable is not to set to the check for death or something, however i get that the variable was not declared, is there some way to make a variable accesible to all functions, and not isolated to their own enviroments?

If that were possible i could just trash my checkfordeath() function, and do that when the damages are calculated.

---
ConPong _//_ Google _//_ Chaos Forge - quick and easy file hosting for developers

"Games usually keep me from making my own..."
-Me
---



find your elementat mutedfaith.com.
well, i suggest you work on learning c++ in a non game programming context first, but the answer to your question is to make it global, i.e. declare it outside of any function making it accessible to all of your functions
-PoesRaven
There was an AP Computer Science case study on this a few years back (before they switched to the aquarium one). Honestly, I''ve never really looked at it, so I don''t know how good it is, but it sounds like a good example of how things can be done.

Personally, this is how I would design the game.

I''d have one Player class. If there''s only one player, that''s fine; create one player object.

I''d also make an Object class, and derive each object in the game from it.

I''d then make a Location class. Each Location would contain an array/list/vector of Objects and Players, AND, an array of pointers to other Locations (which I''ll call Doors). These would be movement options.

Each Location would have a short Name string, and a longer Description string. You could probably store all this in a text file for easy modification.

Each Player has a pointer to the Location it''s in. In order to make sure that it and the pointers in each Location correspond, I would use one member function of Player to Move; it would be a friend to the Location class.

In my main game loop, I would do something like this:


  //PseudocodePlayer you;while(you.Alive()){    int choice;    cout << "You are in the " << you->Place.Name << ".\n\n"         << you->Place.Description << "\n\n";    cout << "Where would you like to go from here?\n";    for(int i=0; i < you->Place.nDoors; i++)    {        cout << i << ".)\t" << you->Place.Doors[i]->Name << endl;    }    cout << "Enter the number of your choice: ";    cin >> choice;    you.Move(you->Place.Doors[choice]); //Pass the Move function a pointer to the next place    cout << endl;}  


Now, actually, this bit of pseudocode isn''t REALLy how you''d want to structure your code. You''d really want to delegate most of the Location description responsibilities to the Locations, and most of the description of the items within them to those items, and so on. Much of the game code would actually be within the Location class. However, I gave you this simple bit of pseudocode to help you get the idea, not REALLY to show how to do the OO design here.

I think that, with an approach like what I''ve described, you''ll be able to store most of your game within a text file, actually, and you could easily extend or change your game. Whatever you decide to do, I hope that I''ve given you some ideas.

Good luck!

This topic is closed to new replies.

Advertisement