Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

If I missed the answer to this question being already posted I apologize but I was unable to find it.

I'm new to C# and XNA and converting a VB game I created into them. Im finding a problem I'm not sure how to get around.

I'm using a generic list that contains information about nonplayer characters including the Graphic, Vector2, Rectangle. I'm reading this information from a file. So I have a class to receive all the information then I add it to the generic List. It then passes that information back to the main Game1.cs.

My problem is when it gets to the Draw method after loading all the information into the generic List<> the other methods in the main see the list EXCEPT the Draw and Update methods. I thought this would be because of the protected status of those methods but it will not let me change them to public.

So to sum up NPCClass.cs creates a class to accept the information. LoadLevel.cs reads from a text file and loads a instance of the NPCClass and adds it to a generic list of the NPCClass that is contained in the Game1.cs. The Draw Method in Game1.cs will not see the generic List even tho I have it publicly declared in Game1.cs main.

Thank you for any help and if i'm going at this completely incorrectly please don't hesitate to tell me.

share|improve this question
6  
Could you provide is with some code perhaps? Preferably all the relevant code and what files the code is in. –  William 'MindWorX' Mariager Dec 23 '11 at 3:14
    
What is the error you are getting when trying to make the list public? Sounds like you might be defining the list as a local variable (inside a method) instead of a member variable (inside the class). –  John McDonald Dec 23 '11 at 16:59
    
Are you sure you declared the list in the correct scope of Game1.cs? Make sure you put it right under the namespace declaration (in that scope, anyways). –  DMan Dec 24 '11 at 0:06
add comment

1 Answer

It sounds like you have built a class when what you want is a game component. A game component overloads the Game1.cs Draw and Update loops with the code contained in the component's draw and update loops. To create a component just right-click on your project and select ->Add New Item and then select game component from the XNA options.

You also may want to look at the XGPA tutorial series that shows how to build a 2D RPG from the ground up. Even if you don't want to follow all the tutorials you can download the lesson source code and see how the author built the project:

http://xnagpa.net/tutorials.php

share|improve this answer
    
Or, alternatively, take the existing class and implement the "DrawableGameComponent" interface. Just be sure to override the draw and update methods, or nothing will happen. –  Raven Dreamer Jan 4 '12 at 20:59
    
That's very much true. You would put :DrawableGameComponent after your class declaration. –  ChargerIIC Jan 4 '12 at 21:07
    
Personally I have found it easier to not implement DrawableGameComponent for every single drawable object and instead just used it for rendering systems that hold a list of the objects and operate at a broader scope. Drawing the objects is more explicit and you have more control over their drawing order. –  ChrisC Jan 5 '12 at 22:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.