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

Model Loading problem

Last post 7/5/2008 8:32 PM by mtnPhil. 10 replies.
  • 7/4/2008 6:42 AM

    Model Loading problem

    Hi, I am writing my own little engine that sorts and manages my game content for me.

    Its built up of functions and arrays that allow me to add up to 50 models (I can change the number) etc.

    For some odd reason when I run the program (with my test program to test the engine) it seems as though the model doesn't load.  Can any one help?

    The error message is: "Object reference not set to an instance of an object."

     

    Here is the AddModel() code in my engine:

    public int AddModel(string _address)

    {

    if (_modelindex != MODEL_LIMIT)

    {

    string new_address = "\\..\\" + _address;

    _models[_modelindex] = content.Load<Model>(new_address);

    _modelindex++;

    return (_modelindex - 1);

    }

    else

    {

    return -1;

    }

    }

    The code is red is the line that the error is linked to.  As the link to the file is back one file I need to add the "file://..//" to it.  I have tried most combinations.

     

    Here is where I initialise the model array:

    public Model[ _models = new Model[MODEL_LIMIT];

    And MODEL_LIMIT is 50 (static).

     

    And when I add the model in my game here it is:

    car = g.AddModel("Objects\\tank");

    Now this above, car is an integer that g.addmodel returns to which will allow the user to link straight to the model if they wish to do something with it.

     

    Can anyone help?

    Cheers!

  • 7/4/2008 8:15 AM In reply to

    Re: Model Loading problem

    Hi!

    Did you add your models to the project?

    Do you use the 'content' directory to store resources?

    In my project when i set from where to load the models, im using this :

    myModel = content.Load<Model>("Content\\Models\\" + ModelName); //myModel is a 'Model' type variable. I added a 'Models' directory in Content directory, and 'ModelName' is string, like 'tank', 'airplane', etc...

  • 7/4/2008 8:39 AM In reply to

    Re: Model Loading problem

    kuzanth:
    Hi!

    Did you add your models to the project?

    Do you use the 'content' directory to store resources?

    In my project when i set from where to load the models, im using this :

    myModel = content.Load<Model>("Content\\Models\\" + ModelName); //myModel is a 'Model' type variable. I added a 'Models' directory in Content directory, and 'ModelName' is string, like 'tank', 'airplane', etc...

    Yes, I have my tank and its other resources inside an objects folder within the main project folder.

    I think its something to do with something in my project is equal to NULL but can't seem to find what, and why it is happening.

    Thanks for the reply.

  • 7/4/2008 9:04 AM In reply to

    Re: Model Loading problem

    I may be wrong, but you may have to use the Content directory for storing resources (ok, you can use other subdirectories here, but the main dir "must be" the Content dir).

    Do you use BasicEffect, or your own effect? Maybe the effect that is missing, because you havent set in its property window the option "copy if newer". Maybe.

    Edit :

    try this one :

    car = g.AddModel(".\\Objects\\tank"); //and comment out 'newaddress'


  • 7/4/2008 10:07 AM In reply to

    Re: Model Loading problem

    Nobby149:

    The code is red is the line that the error is linked to.  As the link to the file is back one file I need to add the "file://..//" to it.  I have tried most combinations.

     

    To get the the parent directory you would need "..\\" not "\\..\\".  Your code will be giving you an address like "\..\Objects\Tank", the first \ in the address tells it to address the root of the disk rather than the current working directory, just as you would if you typed "cd \" in a command prompt.

  • 7/4/2008 1:21 PM In reply to

    Re: Model Loading problem

    I have now got rid of the new_address, and just using this:

    _models[_modelindex] = content.Load<Model>("..\\Content\\" + _address);

     

    The address is the name of the model.

    I am still getting the same error message: "Object reference not set to an instance of an object."

    Very confusing issue :s

    Thanks for all your suggestions so far!  I have learnt and got some great tips and revision.

  • 7/4/2008 2:14 PM In reply to

    Re: Model Loading problem

    Using the ""..\\Content\\" + ..." would put the content you are looking for under the "Debug\\x86\\Content" (or Debug\\Xbox360\\Content" depending what platform you are using) folder which is not what you want I don't think. Try "..\\" + _address
  • 7/4/2008 2:40 PM In reply to

    Re: Model Loading problem

    Hi,

    The AddModel function was in a class, which also had the other variables used inside it such as _modelindex, _models etc.

    The _address comes from the main game code in the loadgraphics functions.

     

    This is where I am now, I have moved the function to the main code module, and it works (obviously the address is now different).

    Anyway, even though it works, I need it to work inside the class, as I want this to be re-usable with every project I make.  It would be very handy.

     

    Okay, heres where my debugging comes in:

    I have distinguished that in the class, the "Content" (the part with .load) is equal to NULL, but in the main module (the one that works) it doesn't equal NULL.

     

    So, any ideas on that emphasis?

     

    Cheers!

  • 7/4/2008 5:56 PM In reply to

    Re: Model Loading problem

    Well, clearly you need to initialize the "content" member of your class.  Are you not doing that?
  • 7/4/2008 8:20 PM In reply to

    Re: Model Loading problem

    mtnPhil:
    Well, clearly you need to initialize the "content" member of your class.  Are you not doing that?

     

    Yes, thats the problem.  I will try to resolve it, but where the class is initiated should I put the type, for example:

    class THECLASS : xna.framework

    etc.

    ?

     

    Cheers

  • 7/5/2008 8:32 PM In reply to

    Re: Model Loading problem

    I usually pass my Game object's Content property into the constructor of whatever class I need it in, and then store it as a member variable.

    Or if your class inherits from GameComponent, then you can access the content manager via the Game property

    http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.gamecomponent.game.aspx

    http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.game.content.aspx

     

Page 1 of 1 (11 items) Previous Next