XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

question regarding accessing methods from components

Last post 6/27/2009 1:56 PM by Craig Martin. 5 replies.
  • 6/27/2009 6:27 AM

    question regarding accessing methods from components

    First of all here is how I have my project set up:

    protected override void Initialize() 
            { 
                 
                graphics.IsFullScreen = true
                graphics.PreferredBackBufferWidth = 500; 
                graphics.PreferredBackBufferHeight = 500; 
     
                graphics.ApplyChanges(); 
     
                // create components 
                fpsComp = new FPScounter(this); 
                freeCameraComp = new FreeCameraComponent(this); 
                terrainComp = new TerrainComponent(this); 
                sky = new SkyDomeComponent(this); 
     
                // register components 
                Components.Add(fpsComp); 
                Components.Add(freeCameraComp); 
                Components.Add(terrainComp); 
                Components.Add(sky); 
     
                // register services 
                Services.AddService(typeof(Camera), freeCameraComp.Camera); 
     
                base.Initialize(); 
            } 

    So each part is its own component. Here is my question. For instance lets say I want to have water that reflects the surrounding terrain. In the tutorial provided at http://www.riemers.net/eng/Tutorials/XNA/Csharp/Series4/Refraction_map.php in order to create the Refraction map you need to access TerrainDraw how would I access that from my Ocean Component which is out side my Terrain Component? With the camera you can access it from any component by registering it as a service is that how I would have to do it in order to access the terrain stuff and if so how would I register it as a service?
    See my games currently in development and more at my website:
    http://empiregames.x10hosting.com
  • 6/27/2009 7:50 AM In reply to

    Re: question regarding accessing methods from components

    To register the terrainComp as a service just do exactly what you have done with the freeCameraComp.Camera.

    Services.AddService(typeof(TerrainComponent), terrainComp);

    Game hobbyist hell-bent on coding a diabolical Matrix
  • 6/27/2009 11:08 AM In reply to

    Re: question regarding accessing methods from components

    Thanks! I was a little confused about that. Thanks for clearing it up! One thing though I thought the Camera would be able to be accessed from any method in any of the namespaces however I am only able to access it from its own namespace and the Engine.Environment Namespace any ideas why?
    See my games currently in development and more at my website:
    http://empiregames.x10hosting.com
  • 6/27/2009 11:39 AM In reply to

    Re: question regarding accessing methods from components

    To use any type you either have to have a using statement for the types namespace or fully qualify the type.
    Game hobbyist hell-bent on coding a diabolical Matrix
  • 6/27/2009 12:02 PM In reply to

    Re: question regarding accessing methods from components

    I have a using statement. Here is what I have. The Camera is a FPS camera so I have it in the namespace Engine.GameObjects.Camera.FPS

    I then load it in my main game class and load it as a service:
    Services.AddService(typeof(Camera), camera.Camera);

    Then in my Terrain Component (Namespace Engine.Environment) I have:
    using Engine.GameObjects.Camera.FPS;

    private Camera camera;

    public override void Initialize()
            {
                // get camera that is a registered service.
                camera = Game.Services.GetService(typeof(Camera)) as Camera;
                if (camera == null)
                {
                    throw new InvalidOperationException("Cameraservice not found.");
                }

                base.Initialize();
            }

    Everything compiles just fine and works the way it should.

    I then went to make a Model loading class I copied and paste all the camera stuff from the terrain component including the using statement. Now whenever I try to compile I get a error:

    Error    1    'Engine.GameObjects.Camera' is a 'namespace' but is used like a 'type'    C:\Users\James\Desktop\Game Engine\Engine\GameObjects\Loader\Loader.cs    15    17    Engine

    Everything is the same so I don't understand why it is not working!

    EDIT - Never mind I changed the camera namespace to Engine.GameObjects.Cameras.FPS and now it is working fine.


    See my games currently in development and more at my website:
    http://empiregames.x10hosting.com
  • 6/27/2009 1:56 PM In reply to

    Re: question regarding accessing methods from components

    Seems like you had a little confusion between the FreeCameraComponent class and the Camera class.

    Putting them both into the same namespace as you have done should make things easier for you.

    I would suggest registering the FreeCameraComponent as a service rather than the Camera, because you may need both, and you can always get the Camera from the FreeCameraComponent.
    Game hobbyist hell-bent on coding a diabolical Matrix
Page 1 of 1 (6 items) Previous Next