I have a large code file (1151 lines) for my little console game. I've tried to do everything in separate files at first but then it got too confusing.
Now before asking for help, I wanted to ask if I should post the whole code or should I not even bother asking for someone to split my code?
It wouldn't fit into one post, I'd have to post like 10 posts to fit the whole code.
I recommend you try splitting the code, and if you run into problems post the relevant code and error messages. Hopefully your code isn't full of global variables and other bad practices.
Well first what is your question that you have? Is it about where to split the code at? Or something else?
1151 LOC is a pretty large amount of code to post on the forum (You would have to have a large amount of posts probably since of the cap on characters). So most likely people wouldn't bother reading through all of it.
So my advice if your question is about where to split the code into multiple files, we can give you some advice on where to do it and when to do it. But we won't just take your code and do it for you.
But if its about a specific problem you are having just post a detailed report about the problem and some snippets of code you believe it is happening in and we can try and work it out.
#include "globals.hpp"
#include <vector>
#include <windows.h>
Entity player;
std::vector<Entity> enemies;
void ClearScreen()
{
HANDLE hStdOut;
CONSOLE_SCREEN_BUFFER_INFO csbi;
DWORD count;
DWORD cellCount;
COORD homeCoords = { 0, 0 };
hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
if (hStdOut == INVALID_HANDLE_VALUE) return;
/* Get the number of cells in the current buffer */
if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
cellCount = csbi.dwSize.X *csbi.dwSize.Y;
/* Fill the entire buffer with spaces */
if (!FillConsoleOutputCharacter(
hStdOut,
(TCHAR) ' ',
cellCount,
homeCoords,
&count
)) return;
/* Fill the entire buffer with the current colors and attributes */
if (!FillConsoleOutputAttribute(
hStdOut,
csbi.wAttributes,
cellCount,
homeCoords,
&count
)) return;
/* Move the cursor home */
SetConsoleCursorPosition( hStdOut, homeCoords );
}
main.cpp:92:46: error: 'class Hero' has no member named 'name'
I don't exactly understand why I get this error, because Hero does have the name which is associated with player.
And since I don't get it, I couldn't think of a good example to post instead of the whole code...
Thank you AbstractionAnon, I see what I missed out there. And jlb, you mean I should pass these parameters by reference?
Basically I should make pointers to all parameters, e.g. Health and _Health or something like that?
What's the difference, when you have global variables and global pointers to those variables?
you mean I should pass these parameters by reference?
Yes, sometimes. Sometimes you will pass parameters by value or by pointer it depends on how you defined the functions.
Basically I should make pointers to all parameters
What? Why would you make pointers to all parameters?
What's the difference, when you have global variables and global pointers to those variables?
I really don't understand what you mean here. What I mean is that you shouldn't have any global variables, period. Create your variables inside your functions and pass these variables to and from your functions using parameters.
It might be worth checking out how functions work again and also more about how classes and concepts like abstraction work. You seem to have some knowledge on them but don't seem to fully grasp how to use them effectively.
So I suggest maybe looking at some tutorials for the following subjects.
1. Functions, mainly about parameters and passing. Basically go over everything about functions again for a quick refresher.
2. Look into classes a bit more. The reason I say this is because you have all your data in a public slot. So that tells me you aren't familiar with interfaces and abstraction and could use some refreshers on how to make effective classes.
Thanks Zereo and jlb,
I'll try to look more into classes, as I always found them really confusing and I'll read the tutorial on functions again.
Actually I only wrote about 400 lines of the code myself and I copied bits from here and there while slightly modifying them (of course, I first find out what it does). When I run into bugs - then I'm in trouble :D. It's just that I read, that writing a game can help you learn programming very actively (and it does! :))
Now since I posted the shorter code above, can someone, please, rewrite a good example of how the same code should be written properly without global variables? Although I changed it slightly to fix the error, I don't know if my solution is right.
I wrote:
1 2 3 4 5 6 7 8 9 10
class Hero : public Entity
{
public:
Hero(std::string _name) : Entity()
{
name = _name;
Health = 20;
MaxHealth = 20;
}
};
I promise I'll take note of whatever you do there :D
I remembered this thread from awhile ago that talks about class design. They cover a lot about class design actually and could be helpful for you. Otherwise there is plenty of other resources on class design.
Use the information from the multiple links that were given. We can't tell you how to do something without knowing the full scope of the program. Since we have only seen a very minor bit of your code it would be very hard to re write your code correctly.
So instead of asking for someone else to do it, I suggest you keep on doing some research (Which is what programming is all about) and figure out the best way to restructure it yourself.
You will have to learn to do it soon or later, so why not start now.