Thread: object interaction

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    23

    Unhappy object interaction

    I want to declare two classes, say Cat and Dog, then when they meet in my game I want them to be able to question each other.

    My problem is that if you declare Dog first then it can't ask Cat any questions and its the same the other way.

    Is there a way the instances can interrogate each other to find out what methods and data they have?

    Is there a way to avoid this problem? Would advance declaration do it?

    This must happen all the time, I'm just new.

    Can anyone help?

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    well, fundamentally they way you describe the nature of the problem is such that it is recursive. if you define two instances of them beforehand, perhaps they can interact...
    hasafraggin shizigishin oppashigger...

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    From a design point of view, you want to minimize dependencies, and especially reduce mutual dependencies. There is no reason a Dog should depend on a Cat, nor any reason a Cat should depend on a Dog.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    Registered User Coder's Avatar
    Join Date
    Aug 2001
    Location
    Cairo, Egypt
    Posts
    128
    Well, this is more of a C++ problem, not a game programming problem. Anyway :

    I can think of a couple of ways to do this ( I have no idea if they'll work! ):

    The 1st method
    When defining the interface for Dog, use references of and pointers to Cat. And forward declare cat, before Dog's interface.
    When implementing Dog, include the Cat interface.

    The 2nd method
    Make a base class that has methods for messaging and interaction. Pass Dog to Cat as an instance of the base class, pass cat to dog as an instance of the base class.
    For example :

    Code:
    // object.h
    class T_Object
    {
    public:
    	virtual void Action( int action,void *pParameters ) = 0;
    
    public:
    	enum
    	{
    		ACT_MOVE,	// cat and dog
    		ACT_ATTACK,	// cat and dog
    		ACT_BARK,	// dog only
    		ACT_BITE	// cat and dog
    	};
    };
    
    // Dog.h
    #include "object.h"
    
    class T_Dog : public T_Object
    {
    private:
    	virtual void Action( int action,void *pParameters )
    	{
    		switch( action )
    		{
    		case ACT_BARK:
    			Bark( pParameters );
    			break;
    
    		case ACT_BITE:
    			Bite( pParameters );
    			break;
    
    		case ACT_MOVE:
    			Move( pParameters );
    			break;
    		.
    		.
    		.
    		}
    	}
    	void Bark( void *pParameters )
    	{
    		// Cast parameter to whatever you want
    		struct T_BarkParam
    		{
    			int volume,length;
    		};
    		T_BarkParam *pBarkParam = (T_BarkParam*)pParameters;
    		// Do whatever you want with the bark parameters and return
    		Snd_PlaySound( "bark.wav",pBarkParam->volume,pBarkParam->length );
    	}
    	.
    	.
    	.
    };
    
    // Cat.h
    
    #include "object.h"
    
    // Same as dog.h
    Muhammad Haggag

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    194
    do this

    class Dog;
    class Cat;

    at the top of your code. Then later when you are implimenting Dog or Cat it will know that the other is a class.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using this as synchronization object
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 07:49 AM
  2. synchronization object choosing
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 03-22-2008, 04:33 AM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 08:38 PM
  4. Question on l-values.
    By Hulag in forum C++ Programming
    Replies: 6
    Last Post: 10-13-2005, 04:33 PM
  5. A question about constructors...
    By Wolve in forum C++ Programming
    Replies: 9
    Last Post: 05-04-2005, 04:24 PM