• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.

Archived

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

da_cobra

global variables

13 posts in this topic

there is a thing that I still don't quite understand and so I keep on getting these kind of errors : say I have a boolean in my windows message loop to know where I am : in the menu, in the game, like this :
      
while (true)
		{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))			
			{
			if (msg.message==WM_QUIT) 
				// Have We Received A Quit Message?

				{
				// If So break

				break; 
				}
			else
				{
				TranslateMessage(&msg) ;	
				DispatchMessage(&msg) ;		
				} // end of else

			} // end of if

		else
			{
			MyGame.CLS() ; // clear the screen

			if (bMenu=true) Menu() ; // go to the menu function

			if (bGame=true) GameMain() ; // go to the game loop

			}
		} // end of while

	return 0 ;
	} // end of WinMain

      
BUT I have to use these booleans in my menu() function so menu option one makes bMenu to true, Menu option 2 => bGame=true so now I put these booleans into my "Globals.h" and include this file in my Winmain.cpp and in my menu.cpp and I also use the "globals.h" in another file clock.cpp but not for these booleans but for other global variables and/or pointers.... so now I get the following error => inMain.obj : error LNK2005: "bool bExit" (?bExit@@3_NA) already defined in Clock.obj WinMain.obj : error LNK2005: "bool bCredits" (?bCredits@@3_NA) already defined in Clock.obj WinMain.obj : error LNK2005: "bool bGame" (?bGame@@3_NA) already defined in Clock.obj WinMain.obj : error LNK2005: "bool bMenu" (?bMenu@@3_NA) already defined in Clock.obj menu.obj : error LNK2005: "bool bExit" (?bExit@@3_NA) already defined in Clock.obj menu.obj : error LNK2005: "bool bCredits" (?bCredits@@3_NA) already defined in Clock.obj menu.obj : error LNK2005: "bool bGame" (?bGame@@3_NA) already defined in Clock.obj menu.obj : error LNK2005: "bool bMenu" (?bMenu@@3_NA) already defined in Clock.obj I know that this error is because I define these booleans over and over, right? How can I avoid this? thanx in advance... Edited by - da_cobra on December 11, 2001 3:14:39 PM Edited by - da_cobra on December 11, 2001 3:15:44 PM
0

Share this post


Link to post
Share on other sites
In the header file, you have to declare nonlocal variables as ''extern''.

extern bool bExit; ... and so on

And there is an error in your code : if (bMenu=true) Menu() should be ==. Same on the next line.
0

Share this post


Link to post
Share on other sites
A better way to do it is instead of using 2 booleans to determine program state, use a single int (or better - an enum), that uses, say 0 for menu and 1 for game, 2 for highscore table, 3 for title screen, etc. It makes it easier to put more states in the game, and eliminates a potential problem that your code has: that it is possible to be in both a game state AND a menu state.
0

Share this post


Link to post
Share on other sites
btw how come when I try to use an enumerated type I get the following error?

enum MenuChoice { NewGame, Credits, Exit} ;


fatal error C1001: INTERNAL COMPILER ERROR
(compiler file ''msc1.cpp'', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
0

Share this post


Link to post
Share on other sites
also I still can''t fix this error it keeps on giving me these kind of errors

WinMain.obj : error LNK2005: "bool bMenuFirstRun" (?bMenuFirstRun@@3_NA) already defined in menu.obj
WinMain.obj : error LNK2005: "bool MenuKeyDown" (?MenuKeyDown@@3_NA) already defined in menu.obj
WinMain.obj : error LNK2005: "bool MenuKeyUp" (?MenuKeyUp@@3_NA) already defined in menu.obj
WinMain.obj : error LNK2005: "int iMenu" (?iMenu@@3HA) already defined in menu.obj
menu.obj : error LNK2001: unresolved external symbol "int MenuChoice" (?MenuChoice@@3HA)
WinMain.obj : error LNK2001: unresolved external symbol "int MenuChoice" (?MenuChoice@@3HA)
Debug/class project.exe : fatal error LNK1120: 1 unresolved externals
Error executing link.exe.

pls some1 help me
0

Share this post


Link to post
Share on other sites
quote:
Original post by da_cobra
btw how come when I try to use an enumerated type I get the following error?

The error lies elsewhere. Keep looking.

quote:
also I still can''t fix this error it keeps on giving me these kind of errors

a.) Did you declare the boolean extern in the other header/source file?

b.) Did you actually declare that integer?

An extern declaration tells the current compilation unit (such as a file) that another unit will actually declare and define that variable. One (and only one) of your source files must then declare the variable without the extern keyword.

Useful links:
cplusplus.com
MSDN

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
0

Share this post


Link to post
Share on other sites
thanx for the quick reply

1) no the error isn''t somewhere else, when I delete that line

enum MenuChoice {...........

the error is gone

2) if I use extern, then I have to declare that variable in the H-file and in the source-file?!?

I don''t understand this, isn''t there any tutorials or such on the web about this topic?
0

Share this post


Link to post
Share on other sites
quote:
Original post by da_cobra
enum MenuChoice {...........

C++ Enumeration Declarations
User-defined data types: typedef, union and extern.

quote:
I don''t understand this, isn''t there any tutorials or such on the web about this topic?

The extern Storage-Class Specifier

Read the documentation, young Jedi.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
0

Share this post


Link to post
Share on other sites
it works like this: every variable and function must be defined in exactly one cpp file. They must also be declared in every single cpp file that uses them. When dealing with functions what you do is you put the function definition in one cpp file and the declaration (the signature) in all the other cpp files that use it. Using .h files to put the declarations where you need them is just a convenience. Header files aren''t compiled, they just get copied into every cpp file that includes them.

So you have to do the same thing with variables too. However when you declare a variable use have to use extern. When you define a variable you leave off the extern. So if you have a variable in multiple files all but one of the files has to have an extern in front of the variable. The easy way to do this is to include the variable in a header and put the extern there. Oh and here''s something I didn''t find out for quite a while: static member variables (which have no relation to static global variables that I know of) need to be initialized in their definitions, and you don''t put static on the definition.
0

Share this post


Link to post
Share on other sites
quote:
Header files ...get copied into every cpp file that includes them.

Not exactly or necessarily. It''s pretty close though. A header file should be parsed only once; at compile time all declarations become global, allowing for resolution of all references. This is why we use inclusion guards (#ifndef MY_HEADER_H...) or platform-specific instructions (#pragma once).

Which is more information than most people want or need.


[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
0

Share this post


Link to post
Share on other sites
If you use those inclusion guards, you do not have to declare the variables as extern.

---
Make it work.
Make it right.
Make it fast.
0

Share this post


Link to post
Share on other sites
If you define a variable in a cpp file, use extern in other files to tell the compiler that it exists.

If you define a variable in a header file, make it include safe using this pattern:

#ifndef HEADERFILENAME_DEFINED
#define HEADERFILENAME_DEFINED

//here goes the usual header stuff

#endif

This way it will be included only once when compiling the whole project.
0

Share this post


Link to post
Share on other sites
Don''t think anyone else posted about this.
if (bMenu=true) Menu() ; // go to the menu function
if (bGame=true) GameMain() ; // go to the game loop

should be:
if (bMenu==true) Menu() ; // go to the menu function
if (bGame==true) GameMain() ; // go to the game loop

or to prevent these problems from compiling:
if (bMenu) Menu() ; // go to the menu function
if (bGame) GameMain() ; // go to the game loop

or :
if (true == bMenu) Menu() ; // go to the menu function
if (true == bGame) GameMain() ; // go to the game loop

if you don''t understand this, you need to read more about how operators work in C/C++. There a lot more complex then what they look at times.


Code comment of the week:
bool hack = false; // DO NOT CHANGE THIS EVER
0

Share this post


Link to post
Share on other sites