accessing objects in main from another class

Started by
1 comment, last by csxpcm 22 years ago
hello all, I have this problem whereby i need to access the object gCamera which is specified in _main_ like so. CCamera gCamera; In main, i invoke several methods on gCamera. When i move around the game level, it updates the position attributes of the gCamera. My problem is that i have a class for the weapon system, and from this weapon class i need to access gCamera attributes in main. I need to do this since i need to know where the camera (person) is positioned, to set the location from where to fire the weapon from. how can I access gCamera and all its attributes from the weapon class? can extern be used? can the scope operator (: be used? Any help is MUCH appreciated! thanks in advance everyone for your time.
Advertisement
You can use :: to access objects declared in the outer scope, or in a class. There is no way to access objects declared in functions outside of those functions, for once because those objects are destroyed when the function exits.

You can make your camera global, or make your camera class singleton. Or you can redesign your game structure so that the object (player) that wants to fire a weapon creates that weapon giving it the position as a parameter. This assumes that all objects know where they are located.
---visit #directxdev on afternet <- not just for directx, despite the name
Don''t cross post. Thread closed.

[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

This topic is closed to new replies.

Advertisement