Page 1 of 1

idiot question :)

Posted: Mon Apr 09, 2007 8:45 pm
by Beshr
hey all!
i know this is an idiot question, How can i clear the scene to start a new level in my game?

:oops: :oops:

Posted: Mon Apr 09, 2007 9:12 pm
by Luben
Scene->clear() clears the scene.

answer

Posted: Mon Apr 09, 2007 9:44 pm
by Beshr
thx! but that's not what i ment

in c++, how can i set up the project so i can for example make a cpp file for each leve, i'm not a good c++ programmer, is there is any place for tutorials on this subject??

Re: answer

Posted: Mon Apr 09, 2007 10:17 pm
by Dr.Bunshin
Beshr wrote:thx! but that's not what i ment

in c++, how can i set up the project so i can for example make a cpp file for each leve, i'm not a good c++ programmer, is there is any place for tutorials on this subject??
Hey Beshr

In actuality, you can just make functions for each level, and of course put those functions in the said cpp files. The viability of this depends on what kind of game you are making. What kind of game are you making?

answer

Posted: Tue Apr 10, 2007 4:49 am
by Beshr
my game is about a ball that i need to move to take to some place. it's not about actully the type of the game i think. my point is when i make another funtion let's say for level 2, when i call it it tell me this:

Code: Select all

Linking...
Game_initialize.obj : error LNK2005: "class irr::video::IVideoDriver * driver" (?driver@@3PAVIVideoDriver@video@irr@@A) already defined in Game.obj
Game_initialize.obj : error LNK2005: "class irr::newton::World * p_world" (?p_world@@3PAVWorld@newton@irr@@A) already defined in Game.obj
Game_initialize.obj : error LNK2005: "class irr::scene::ISceneManager * smgr" (?smgr@@3PAVISceneManager@scene@irr@@A) already defined in Game.obj
i know that this is because i have them already defined, but if i didn't define them, it tells me that it's an undeclared identifier.
:idea: :?: :idea: :?:

Re: answer

Posted: Tue Apr 10, 2007 7:15 am
by jam
Beshr wrote: in c++, how can i set up the project so i can for example make a cpp file for each leve, i'm not a good c++ programmer, is there is any place for tutorials on this subject??
You might give Irrwizard a look. It will give you a basic framework to start off with.

Posted: Tue Apr 10, 2007 11:10 am
by hybrid
Don't define variables in .h files, but only in .cpp (and thus later on in .o) files. Because every inclusion of the .h file will create another instance of the variable, with the same name and scope.

Posted: Tue Apr 10, 2007 12:25 pm
by roxaz
i thought this prevents creating another instances of variables

Code: Select all

#ifndef _h_file_
#define _h_file_

<...some code...>

#endif

Posted: Tue Apr 10, 2007 12:33 pm
by eneru
hybrid wrote:Don't define variables in .h files, but only in .cpp (and thus later on in .o) files. Because every inclusion of the .h file will create another instance of the variable, with the same name and scope.
what ? the variable are supposed to be defined in the .h along with the functions (unless you want to "hide" them), arn't they ?

Posted: Tue Apr 10, 2007 12:47 pm
by roxaz
actually headers are ment to be class definition files so variables should be defined in class

Posted: Tue Apr 10, 2007 3:23 pm
by Luben
if headers are ment to be class definition files, how come headers are used with plain c too? ;)
headers are declaration files, but general, not restricted to classes.


Back to the subject, the problem is that if you declare a variable in a cpp-file, the compiler assigns a name to the space where the variable will be in your program, so that it can be refered to using that name. (sortof). If you have another variable of the same name in another cpp file, it will get the same name. So far so good.
But when the linker kicks in, the problem arise, because there are 2 memory areas with the same name. (sortof).
The solution is to define your variable in a way that doesn't make the compiler assign the name a new memory area, but instead just use the name, and let the linker do the linking between name and memory area.(sortof).

example:

Code: Select all

//File1.cpp
int x; //Global int x


//File2.cpp
int x; //global int x

//This produces that error, since there are 2 global "x"-objects

Code: Select all

//File1.cpp
int x; //Global int x

//File2.cpp
extern int x; //Global in x, which's object/memory is in another .cpp
EDIT: Hehe, i messed up here. The variables need to be extern in .h-files too.

Code: Select all

//file1.cpp
#include "File3.h"
int x;

//File 2.cpp
#include "File3.h"
//X was declared in file3.h

//File3.h
#ifndef _h_file_    //Protection!
#define _h_file_
extern int x;
#endif
sortof.

As i've understood it, definitions are the actual code or variable-objects, while declarations are maps to how we use the code/objects. sortof.
So multiple definitions is a problem, but multiple declarations should not be a problem.

Hope it helps a little

Posted: Tue Apr 10, 2007 4:42 pm
by hybrid
eneru wrote:
hybrid wrote:Don't define variables in .h files, but only in .cpp (and thus later on in .o) files. Because every inclusion of the .h file will create another instance of the variable, with the same name and scope.
what ? the variable are supposed to be defined in the .h along with the functions (unless you want to "hide" them), arn't they ?
They should be declared in .h files, but not defined. Even header protections won't help with definitions, as these will be put into each .o files which includes the .h file.