Archived

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

csxpcm

accessing objects in main from another class

Recommended Posts

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.

Share this post


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

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.