Nesting ModelAnimator in a Player class?

Topics: User Forum
Feb 17, 2007 at 1:44 PM
I am having problem going beyond the tutorials. I am a newbie so I guess this is more like a general problem. I want to create a Player, PlayerManager, and a Camera class. This is because I can then write methods like RenderPlayers and so on. When I am nesting the ModelAnimator inside my Player class and trying to instantiate it I get an error:

'ShooterExperiment.Game1' is a 'type', which is not valid in the given context

I don't know how to fix it. Plz help.

Cheers
Alfons
Coordinator
Feb 17, 2007 at 8:05 PM
Edited Feb 17, 2007 at 8:05 PM
Sounds like you are passing Game1 (which is a type) into the modelanimator's constructor when you create it, when you should in fact be passing an instance of type Game1 (or Game) into the constructor.
Feb 17, 2007 at 8:13 PM
and how do I do that?
Feb 17, 2007 at 8:19 PM
You'll have to show who us more code, specially the part where you make the ModelAnimator instantiation.

An example on how you could implement the Player class is as follows
public class Player
{
  ModelAnimator animator;
  ...
  public Player(Game game)
  {
    Model model = content.Load<Model>("player_model");
    animator = new ModelAnimator(game, model);
    ...
  }
  ...
}
And in your Game class
protected override void LoadGraphicsContent(bool loadAllContent)
{
  if (loadAllContent)
  {
    Player player = new Player(this);
    ...
  }
}

Feb 17, 2007 at 8:27 PM
I change Program.cs to this and it works. Is this a stupid solution?

public static Game1 game;
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
using (game = new Game1())
{
game.Run();
}
}
Coordinator
Feb 17, 2007 at 8:31 PM
Definitely not an ideal solution, but passable if you are only using one game object for everything. You'll really have to post more code to get more help.
Feb 17, 2007 at 8:41 PM
Edited Feb 17, 2007 at 8:43 PM
i uploaded my code here: www.karlfredberg.se/cgtalk/ShooterExperiment 2007-02-17.rar

plz have in mind that it's a work in progress and only a test. So there is alot of junk code all over the place.
Coordinator
Feb 17, 2007 at 9:05 PM
Edited Feb 17, 2007 at 9:11 PM
Well, since your entire code base is static anyway, it won't be too easy to implement the ideal solution. You'd want to make your Player class take a Game object as a parameter in the constructor and store it as a member variable.

But since all your managers are static classes anyway, it probably won't hurt to make the Game a public static property.

Keep in mind that this reliance on static classes and properties reduces code reusability; it will be non-trivial to use these classes in other games. They are effectively global variables, which, in general, should be used as sparingly as the top of the food pyramid.

Googling about C# and stetic methods can give a wealth of insight on when to use them, etc. Here is one link: http://msdn2.microsoft.com/en-us/library/79b3xss3.aspx
Feb 17, 2007 at 9:18 PM
thanks for the help, I never figured out what the disadvantages about static so I kept with it, but I'll try to read up on it now. I find reusability highly useful.

and dastle, I must say your engagement in the XNA community is amazing.
Coordinator
Feb 17, 2007 at 9:26 PM
Edited Feb 17, 2007 at 9:26 PM

Alfons wrote:
and dastle, I must say your engagement in the XNA community is amazing.


Heh, I just love coding (only in C# though) and software design theory. Subscribing to this site's RSS feed gives me an excuse to not do work for a few minutes whenever someone posts :)