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

how can i pass info between 2 classes/game components

Last post 9/30/2008 7:56 PM by Meme83. 3 replies.
  • 9/30/2008 6:21 PM

    how can i pass info between 2 classes/game components

    ok, I’m going to try explain this clearly, but let me know if I’m not.

     

    Ok, so i have a class called "active scene" this is shown when you’re playing the game (e.g. moving the character around). within this class I have 2 game components iv made, 1. Quest manager 2. Item manager.

    What I want to know is how do I get info from the quest component to the item component.

    So when a quest equals complete in my quest manager I want to add a set amount of money to the money variable in my item manager.

     

    thanks in advance

  • 9/30/2008 6:53 PM In reply to

    Re: how can i pass info between 2 classes/game components

    The easiest way: make your item manager a global. This works as long as there is only ever one item manager.

    The harder way: define interfaces, and ask for them. Define an IMoneyManager interface:

    public interface IMoneyManager {
      int Balance { get; }
      void ChangeBalance(int delta);
    }

    Implement this in your Player class. Whenever something interacts with a quest, pass in the object that is interacting. The quest can then try to cast the object as IMoneyManager (obj as IMoneyManager), and if it's not NULL, interact with the interface.

     


    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 9/30/2008 7:28 PM In reply to

    Re: how can i pass info between 2 classes/game components

    Thanks for the reply!

    i should have known to do it globally.

    and thanks for the other method, its always good to learn new things.

  • 9/30/2008 7:56 PM In reply to

    Re: how can i pass info between 2 classes/game components

    I guess that you don't want the two components to know about each other. 

    I suggest you to have an other class (let's call it glue) with references to both the quest and the item managers. This class will be observer of the quest manager (google for the observer pattern *), and will be responsible to call the required method of the item manager.

     

    * If you look here you see a C# example using the events.

    In your case you first define a QuestCompletedEvent and add a field of that type in the Quest class. Then you subscribe the Glue class to it, by creating a method OnQuestCompleted which takes the senter and the arguments (as shown in the example) and subscribing that method with the += operator (see example). Secondly, inside the logic of the Quest class you add a .Notify() when the quest is completed, this way the glue Class gets notified, i.e. the OnQuestCompleted method is called. Inside that method you will run the code necessary to update the item manager.

    I hope this clarifies the thing. The only thing you need then is to find a proper name for the Glue class ;)

Page 1 of 1 (4 items) Previous Next