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

Error 1 The name 'Content' does not exist in the current context

Last post 6/11/2008 3:03 PM by Jim Perry. 7 replies.
  • 6/11/2008 10:11 AM

    Error 1 The name 'Content' does not exist in the current context

    Hi all,

    I'm imagining this is a very simple problem, but I seems to be unable to solve it despite many hours trawling the net and various tutorials.

    Basically, I have a class file sepearate from Game1 but in the same namspace and I want to load some content, but all I get is the error above.

    I've tried various ways of referencing the content, contentManager.Load, Game.Content.Load etc... etc... but they all just give errors.

    Could someone please tell me what I am doing wrong and how I can load content outside of the Game1 class??

    Thanks in advance for the help. The code for the class is currently as follows:

    using System;

    using System.IO;

    using System.Text;

    using System.Collections.Generic;

    using Microsoft.Xna.Framework;

    using Microsoft.Xna.Framework.Audio;

    using Microsoft.Xna.Framework.Content;

    using Microsoft.Xna.Framework.GamerServices;

    using Microsoft.Xna.Framework.Graphics;

    using Microsoft.Xna.Framework.Input;

    using Microsoft.Xna.Framework.Net;

    using Microsoft.Xna.Framework.Storage;

    namespace SinsoftheFounders

    {

    class WorldGenerator

    {

    private landScape[ landScapes;

    public dungeon[ dungeons;

     public WorldGenerator(string lsfFile)

    {

    parseLSFfile(lsfFile);

    }

    public struct landScape

    {

    private int width;

    private int height;

    private Texture2D heightMap;

    private string name;

    private string textureSet;

    public landScape(int height, int width, string heightMap, string name, string textureSet)

    {

    this.width = width;

    this.height = height;

    this.heightMap = Content.Load<Texture2D>(heightMap);  Here is the problem line!!!

    this.name = name;

    this.textureSet = textureSet;

    }

    }

    public struct dungeon

    {

    private int width;

    private int height;

    public dungeon(int height, int width)

    {

    this.width = width;

    this.height = height;

    }

    }

    private void parseLSFfile(string lsfFile)

    {

    string[ fileLines = FileReaderUtil.SplitFileByLine(lsfFile);

    int areaNumber = fileLines.Length;

    for(int index = 0; index < fileLines.Length; index++)

    {

    string[ items = fileLines[index].Trim().Split(',');

    landScapes = new landScape[fileLines.Length];

    landScapes[index] = new landScape(Convert.ToInt32(items[2]), Convert.ToInt32(items[3]), items[4], items[0], items[1]);

    }

    }

    }

    }

  • 6/11/2008 1:07 PM In reply to

    Re: Error 1 The name 'Content' does not exist in the current context

    Depending on what is creating your WorldGenerator instance, you could pass a reference to the Game object to it and do:

    class WorldGenerator

    {

         public content As ContentManager;

        public WorldGenerator(Game game, ....
     

    content = new ContentManager(game.Services, "Content");

     

    and either pass the game object reference on to the other objects that need it or the content manager reference.

    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 1:40 PM In reply to

    Re: Error 1 The name 'Content' does not exist in the current context

    Thanks for the speedy reply,

    I see what you are saying,

    WorldGenerator is currently called in the Game1 Initialise method with the line,

    world = new WorldGenerator("C:\\Visual Studio 2005\\Projects\\SinsoftheFounders\\SinsoftheFounders\\Content\\World lsf\\Path1.lsf");

    I've added the Game game to the worldGenerator constructor but am having trouble passing the reference to the Game object in this line - any advice??

  • 6/11/2008 1:46 PM In reply to

    Re: Error 1 The name 'Content' does not exist in the current context

    ok I have that sorted, but now I can only reference content inside the contructor...
  • 6/11/2008 2:06 PM In reply to

    Re: Error 1 The name 'Content' does not exist in the current context

    no worries I have it fixed - I think... thanks for the advice!
  • 6/11/2008 2:19 PM In reply to

    Re: Error 1 The name 'Content' does not exist in the current context

    Kilanirax:

    WorldGenerator is currently called in the Game1 Initialise method with the line,

    world = new WorldGenerator("C:\\Visual Studio 2005\\Projects\\SinsoftheFounders\\SinsoftheFounders\\Content\\World lsf\\Path1.lsf");

    I hope that's not how you're planning on keeping that. Hardcoding a path is a no-no. :)

    Using Application.StartupPath is probably better

    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 2:52 PM In reply to

    Re: Error 1 The name 'Content' does not exist in the current context


    Jim Perry:

    I hope that's not how you're planning on keeping that. Hardcoding a path is a no-no. :)

    Using Application.StartupPath is probably better

    Or StorageContainer.TitleLocation, which will work on Xbox and Zune as well.

    XNA Framework Developer - blog - homepage
  • 6/11/2008 3:03 PM In reply to

    Re: Error 1 The name 'Content' does not exist in the current context

    Yeah, I thought about that after I'd posted it. I gotta get in the habit of using that. :)
    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.
Page 1 of 1 (8 items) Previous Next