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

Cannot access the player's position from the enemy class.

Last post 06/11/2009 20:18 by jwatte. 4 replies.
  • 06/11/2009 16:22

    Cannot access the player's position from the enemy class.

    My project has three major classes: Player, Enemy and EnemyManager. Each is instantiated in the main game class - the Player as "player" and EnemyManager as "enemyManager". All enemies are stored in a list in EnemyManager.

    All three classes have an Update method. The Enemy's Update method is called by the EnemyManager's Update method. Both the Player and EnemyManager Update methods are called by the main game loop.

    The problem is that I want to access the Player's position (a vector2 stored in the Player class, and accessible via a public Position property) in the Enemy class, however I recieve an error which says:

    "The name 'player' does not exist in the current context"


    Any ideas?
  • 06/11/2009 16:57 In reply to

    Re: Cannot access the player's position from the enemy class.

    The 'player' instance that you created is only available to your game class.  The enemy class isn't going to have a reference to that instance and therefore you can't make access calls to it. 

    One way around it is to pass a reference of the player class into the enemy class through the update method.  Another way, might be to pass the reference in via the enemy constructor and then store the reference for later use in the update method.

    What I do, which may not be the most recomended method, is that I store everything I create inside the respective classes.  So, for player, there is a static reference to a player instance.  In my 'enemies' type class there is a static reference to a list of enemies.  Then any part of my game can access those values and do stuff with them at any given time.
  • 06/11/2009 16:59 In reply to

    Re: Cannot access the player's position from the enemy class.

    You need to go through the Game object since that's where the object is.
    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.
  • 06/11/2009 17:09 In reply to

    Re: Cannot access the player's position from the enemy class.

    I'll give that a try. Thanks
  • 06/11/2009 20:18 In reply to

    Re: Cannot access the player's position from the enemy class.

    Answer
    Reply Quote
    There are two ways to solve this.

    If your game will only ever be single-player, then you will only ever have one Player. At that point, you can create a static, public field of Player called something like "Instance", and assign to that in the Player constructor:

    public class Player
    {
      public static Player Instance;
      public Player() {
        Instance = this;
        /* stuff */
      }
    }

    At this point, you can always get to the current player by going through "Player.Instance"

    If your game may be multi-player, or your enemies need to react to "things in the world" (whether they be player or not), then it's probably better to create a World object. Each object in the World registers itself (typically by implementing IWorldObject). An entity can then query the World for "things close enough to me to care about," and make whatever decision they want.

    For example:

    enum Alignment {
      PlayerFriendly,
      PlayerHostile
    }

    public interface IWorldObject {
      Alignment Alignment { get; }
      Vector3 Position { get; }
    }

    public class World {
      public static World Instance = new World();
      World() {}
      List<IWorldObject> objects = new List<IWorldObject>();
      public void AddObject(IWorldObject wo) ...
      public void RemoveObject(IWorldObject wo) ...
      public List<IWorldObject> GetObjectsNear(Vector3 pos, float radius) ...
    }

    The Player would then implement IWorldObject, set Alignment to PlayerFriendly, and add itself to the world.
    The enemies would implement IWorldObject, set Alignment to PlayerHostile, and add themselves to the world.
    When the enemies need to "react" to something in the world, they call World.Instance.GetObjectsNear(Position, radius), and iterate through those objects to see if there's any objects that are PlayerFriendly; if so, they know they are enemies thereof.
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
Page 1 of 1 (5 items) Previous Next