Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

nicho_tedja

no global variables?

This topic is 5815 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

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."

Share this post


Link to post
Share on other sites
Advertisement
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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I wrote a 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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!