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

Best way to get a reference to a game component

Last post 6/11/2008 9:39 PM by mtnPhil. 2 replies.
  • 6/11/2008 9:27 PM

    Best way to get a reference to a game component

    I have based my game on the NetworkStateManagement sample. I am writing a Sound Engine that handles background music and sound effects. Since this engine needed an Update method to be called at all times, I decided to implement it as a GameComponent.

    I know that for game service there is a

    Services.GetService(typeof(XXX))

    But for game components I have not found an elegant way of doing it. Since I had to reference it from the GamePlay screen, I had to do this:

    soundEngine = (SoundEngine)ScreenManager.Game.Components[3];

    Which I don't like.

    So my question is, is there a nice way of getting a reference to a GameComponent without passing the reference all the way across different screens? I would have used a Game Service but I really need the Update(GameTime) method to be constantly called.

     

  • 6/11/2008 9:38 PM In reply to

    Re: Best way to get a reference to a game component

    The correct answer is to use a GameService. I don't understand what using a service has to do with having Update called.
    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 6/11/2008 9:39 PM In reply to

    Re: Best way to get a reference to a game component

    The fact that it is a GameComponent doesn't preclude it from being a service too.

    Just make an interface ISoundEngine (that has the methods you want to call from various places), have it implement that interface, and register it as a service under that interface.

     

Page 1 of 1 (3 items) Previous Next