Variable outside of scope

Post your questions, suggestions and experiences regarding game design, integration of external libraries here. For irrEdit, irrXML and irrKlang, see the
ambiera forums
Post Reply
coderaider249
Posts: 9
Joined: Fri Nov 04, 2011 4:38 pm

Variable outside of scope

Post by coderaider249 »

Hi, I have my scene initialized in an if statement. but my main loop is outside the if statement, so I can't access the scene node pointers(or any other variable) in that if statement from the main loop. Is there any way in C++ to use variables that are created inside an if statement out of it?
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: Variable outside of scope

Post by Radikalizm »

You might want to review your design instead of trying to look for ugly workarounds
All variables created within a scope get deleted as soon as that scope is no longer valid, unless they were allocated dynamically on the heap, so such a workaround does not exist
CuteAlien
Admin
Posts: 9646
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Re: Variable outside of scope

Post by CuteAlien »

You should probably work with classes. Put variables in classes and you can access them anywhere within that class.
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Re: Variable outside of scope

Post by hybrid »

Well, wouldn't call it workaround, but common practice: Create the pointer before and init to zero. Then just assign the pointers in your if statement. But as CuteAlien said, it's probably better to give everything a little more structure.
coderaider249
Posts: 9
Joined: Fri Nov 04, 2011 4:38 pm

Re: Variable outside of scope

Post by coderaider249 »

yeah, I tried with creating pointers in the header file although it took the red underlines off but got errors in compile. maybe I want to review the code again.
@CuteAlien: I think you meant the same thing, right?
thanks!
Radikalizm
Posts: 1215
Joined: Tue Jan 09, 2007 7:03 pm
Location: Leuven, Belgium

Re: Variable outside of scope

Post by Radikalizm »

coderaider249 wrote:yeah, I tried with creating pointers in the header file although it took the red underlines off but got errors in compile. maybe I want to review the code again.
@CuteAlien: I think you meant the same thing, right?
thanks!
Using classes is different from using globals ;) Maybe you should review your C++ skills before you go on?
coderaider249
Posts: 9
Joined: Fri Nov 04, 2011 4:38 pm

Re: Variable outside of scope

Post by coderaider249 »

Radikalizm wrote:
coderaider249 wrote:yeah, I tried with creating pointers in the header file although it took the red underlines off but got errors in compile. maybe I want to review the code again.
@CuteAlien: I think you meant the same thing, right?
thanks!
Maybe you should review your C++ skills before you go on?
definitely! :oops:
Post Reply