Archived

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

csxpcm

accessing objects in main from other classes

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
Guest Anonymous Poster
Get a pointer to the camera while initializing your weaponclass.

Like this
int CWeapon::Create(CCamera *cam);

weapon.Create(&gCamera);

Share this post


Link to post
Share on other sites
thanks for the quick reply, but im not sure exactly if I have expalined it porpely.
I''m in a method of the weapon class, lets says,

void CWeapon::fireWeapon()
{
// in here i want to access the gCamera object in main
}


thanx! kind regards.

Share this post


Link to post
Share on other sites
You have several options:
  1. Make gCamera global. This will infuriate the "OOP purists", but is a perfectly valid solution.


  2. Pass gCamera as a parameter to either every function that needs it. Stupid and inefficient.


  3. Have static data and methods in client classes that need to access camera properties:

    class CWeapon
    {
    public:
    ...
    static void SetCamera( CCamera * camera ) { theCam = camera; }
    private:
    ...
    static CCamera * theCam;
    };
    CCamera CWeapon::theCam = 0;


  4. My personal favorite, refactor your design.

    If your weapons class needs to obtain camera properties, you''re not performing logical scene management. Weapons (and indeed all graphical objects) should be part of a scene, which has one or more cameras through which it is rendered. Back to the drawing board.


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

Share this post


Link to post
Share on other sites
THANKS!! Oluseyi for explaining the available options, some of them I hadn''t thought about.
I expect option 4 is the best solution, especially considering my _simple_ engine, will get progressively more advanced.
but the architecture of the program to do it this way confuses me.
I thought the simplest way to determine the position to fire the weapon from would be just in front of the present camera locaton.
But like you mentioned, if I use logical scene management and thus seperate the scene from the functionality of the camera, how do i get the position from which to fire from?
Could someone therefore please clarify a method on how this would be acheived. how I need to strucure the program better, I looked under google but its hard to find.

All help is much appreciated,
Thank-you in advance!

Share this post


Link to post
Share on other sites
Hi,
You may want to use observer-observable pattern to solve your problem. You can make your weapon class an observer class and your camera a observable class such that;


  
class Weapon : public Observer;
class Camera : public Observable;

Weapon weapon1;
Weapon weapon2;
Cemara camera;
camera.register(&weapon1);
camera.register(&weapon2);



so now whenever you change your camera position, your weapons, and all other objects that registered with the camera, will have updated camera position.

There are many good books, as well as websites, that describe this particular pattern.

Share this post


Link to post
Share on other sites