no global variables?
Hi there,
I''m wondering how do you make a big program (games) without using global variables at all. I heard you can do this with classes, but I still don''t get it how.
The only way I can think of is using one single main source file:
main.cpp // everything in here
level.cpp // level class
level.h
monster.cpp // monster class
monster.h
etc...
in main.cpp, I include all necessary .h files. But still, one source file? Isn''t that gonna make 20000 lines long in one file, and that''s very confusing and annoying looking at the tiny scroll thumb.
Is there any other way to do this?
My compiler generates one error message: "does not compile."
the first 2 games I wrote used globals (way too many of them in fact). Even though I''d just spent 3 years at uni learning the right way to code, for some reason I coded my first two games ala LaMothe style. (probably cos I was reading his books at that time).
The code was bloody awful but I did learn alot.
My third game was coded better (but still not great).
I code much better when not writing game projects (don''t know why though).
anyway the point is that I''d get in there and start doing it at least in the beginning.
You''ll get cleaner as you go along, however keep in mind that its not the best way to do things and you''ll be right.
Toby
Gobsmacked - by Toby Murray
The code was bloody awful but I did learn alot.
My third game was coded better (but still not great).
I code much better when not writing game projects (don''t know why though).
anyway the point is that I''d get in there and start doing it at least in the beginning.
You''ll get cleaner as you go along, however keep in mind that its not the best way to do things and you''ll be right.
Toby
Gobsmacked - by Toby Murray
You could have a game class in one file to begin with. And it''s not forbidden to not put everything in one place. you could split it. Graphics, Sound, keyboard utilities doesn''t have to be in the same place. The Game class could then put things together...
happy coding!
happy coding!
It''s actually pretty simple. Within your main()/WinMain()/etc:
{
CGame cApp;
cApp.Run(); //hehe hopefully you''ll see it run
return cApp.GetOSReturnCode();
}
CGame is then the class that takes care of it all. Within that, there may be several member class variables that call other classes to do work. I''ve actually done this several times before and it works quite well. Keeps main really small and lets the majority of your code be object oriented. I still like to use global variabes for some things, however.
{
CGame cApp;
cApp.Run(); //hehe hopefully you''ll see it run
return cApp.GetOSReturnCode();
}
CGame is then the class that takes care of it all. Within that, there may be several member class variables that call other classes to do work. I''ve actually done this several times before and it works quite well. Keeps main really small and lets the majority of your code be object oriented. I still like to use global variabes for some things, however.
I wrote a
"It is precisely because it is fashionable for Americans to know no science, even though they may be well educated otherwise, that they so easily fall prey to nonsense. They thus become part of the armies of the night, the purveyors of nitwittery, the retailers of intellectual junk food, the feeders on mental cardboard, for their ignorance keeps them from distinguishing nectar from sewage."
Isaac Asimov
goto
the other day in memory of E W Dijkstra "It is precisely because it is fashionable for Americans to know no science, even though they may be well educated otherwise, that they so easily fall prey to nonsense. They thus become part of the armies of the night, the purveyors of nitwittery, the retailers of intellectual junk food, the feeders on mental cardboard, for their ignorance keeps them from distinguishing nectar from sewage."
Isaac Asimov
To break out of several nested
for
loops? Otherwise I''d have to smack you with a newspaper :D
quote:Original post by SilentReaper
It''s actually pretty simple. Within your main()/WinMain()/etc:
{
CGame cApp;
cApp.Run(); //hehe hopefully you''ll see it run
return cApp.GetOSReturnCode();
}
I hope that''s not how you do it
The "correct" way to do it would be to use a Singleton. Make a Google search.
Cédric
Cool I will have to check into that. But, yeah, believe it or not that is how I had been doing it.
quote:Original post by cedricl
I hope that''s not how you do it
The "correct" way to do it would be to use a Singleton. Make a Google search.
Ergh... what would be the point of a singleton here? Without knowing anything more about CGame''s interface, it isn''t given that it needs to be globally accessible.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement