Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


armitroner

Member Since 06 Jun 2012
Offline Last Active Nov 20 2012 08:40 AM

Posts I've Made

In Topic: beginning C++ game programming

12 November 2012 - 08:56 AM

IMHO, beginning with getting a firm grasp of the basics of C will help you in the long run when delving into C++. As for where to begin learning C++, I think that it's best to go for just a general knowledge of C++, then going to a specific area of programming style, such as game programming.

If you want a mixture of good knowledge of C++ as well as the basics for game programming, I'd recommend the book 'C++ Programming for the Absolute Beginner' by Mark Lee. It teaches you how C++ works just like a normal book would, but using game analogies and examples to teach you, so it's like hitting two birds with one stone.

However, it still stands that you should learn some C before C++. I started off messing around with C, in which I developed my first text-based game (which I got rid of last year). After that, I read the aforementioned book, and used the net to learn C++, and I'm in the process of making another text-based (soon to be 2D grapical! :D) game.

But still, you should just do what suits you best. All we can offer are pointers and advice. :)

In Topic: C++ - Is Goto a Good Practice?

12 November 2012 - 08:34 AM


I only used goto in one small section, since I don't have much need for it elsewhere.

You are essentially abusing goto to create a loop. Instead, use an explicit loop:
...
Note that this automatically handles the case where the user types an invalid option by looping around, though it is generally nicer to print a specific message in this case.

Another potential improvement is to dynamically remove invalid options from the menu. Something like this:
...
I'm not sure if you are familiar with std::vector or function pointers. Feel free to ask a question if you don't understand what this code is doing.


From what I can see in the second example, it's working sort of like an array. It appears to be creating a single, erm, (for lack of a better term) method that carries the other functions in it, kinda like how a class works? Then it grabs how many functions are underneath CombatChoice and displays them, dependent upon wether or not they meet the conditions given when using the push_back function. (not sure what push_back is)
Finally, when choosing the option, the reason you created that variable when creating the menu was to add an extra bumper in case the player chose the number of an option that wasn't even there.

I'll do a bit of reading on std::vector, since this is the first time I've come across the term. This was actually my first serious C++ project, so I had to hunt down tons of stuff on the net. Posted Image
Anyhow, thanks for the advice, and I'll be working on improving my code once my compiler gets fixed... Posted Image

In Topic: C++ - Is Goto a Good Practice?

11 November 2012 - 07:41 PM

You could implement your battlemenu separately and make a call to it when you need it instead of using goto. The thing about goto is that you basically get rid of every pre/post condition that a block of code should have. When you have an if else block that, besides having the two normal possible outcomes (if branch or else branch), jumps to another place with a goto in the middle, then nothing is safe anymore. You stomp over every "safety" that basic control structures should give to you when using goto to reach some place in the code. Its like having a carefully designed brake system in a car, only to go and smash it against a wall because you think you can stop the car faster by doing so.


Goto can also make spaghetti code. A lot of times what ends up happening is that your code is jumping from place to place, making it extremely hard for other people to understand what you're doing. It also makes it hard for you. (Example: Code an awesome enemy system. A month later you need to extend it. Go to look at it and it keeps on jumping to a bunch of places, and you don't understand it, so you scrap all your code.). Now, GoTo isn't always evil. There's an application for everything. However generally using Goto is frowned upon, and the problem you're solving with it can be solved by using object oriented programming.



I see. So basically, goto is really only a last-ditch alternative for when you don't need to go back and extend it all that much. The better thing to do would be to create a separate class specifically for the battle menu? Crap, another class I have to make... :P

Anyhow, thanks! I'll see how I can change it to a class, though I'm still not entirely sure how to split all the options into a class, so maybe I could get a couple pointers here?

In Topic: C++ - Is Goto a Good Practice?

11 November 2012 - 06:37 PM

Oh, yeah, sorry!
I only used goto in one small section, since I don't have much need for it elsewhere.

[source lang="cpp"]void Combat::combatChoice(Character& C) { if (C.health < 0 || C.health == 0) { cout << "----------------- You died... ------------------" << endl; cout << "Oh dear, it seems you have died... Game Over." << endl; } else { battlemenu: cout << "----------------- Battle Options ------------------" << endl; C.Display(); cout << "What do you want to do? \n" << "Type the number of the action and press enter. \n" << "[1] Melee Attack \n" << "[2] Gun Attack \n" << "[3] Health Potion" << endl; short choice; cin >> choice; switch (choice) { case 1: cout << "\n----------------- Battle Results ------------------" << endl; C.meleeAttack(M); break; case 2: cout << "\n----------------- Battle Results ------------------" << endl; if(C.ammo == 0) { cout << "You're out of ammo! \n" << endl; goto battlemenu; } else { C.gunAttack(M); } break; case 3: cout << "\n----------------- Battle Results ------------------" << endl; if(C.potions == 0) { cout << "You're out of potions! \n" << endl; goto battlemenu; } else { C.useHP©; } break; } }}[/source]

In Topic: Load of errors concerning 'console' function.

06 June 2012 - 03:24 PM

Thanks! I'll be researching ncurses and fixing my code.

Rep for you! :)

Edit: Quick thing more... how do I fix my code for ncurses..? :huh:

PARTNERS