Rafael Mores

Hi,

I'll begin saying I'm developing a 2D side-scroller game with XNA, and I'm new to game programming as well as OOP, so it's been quite challenging for a while now.

I have a maybe stupid question but I can't seem to understand this:

I'm storing my Sprites in a List i.e. Players or Enemies, then storing those lists onto another List(or maybe i should go with a List[ , ] , I dont know yet)

That final List is then passed onto my Scene Manager, which handles rendering and updating the scene. It works good, but in the Main class, if I change something in the sprite list directly, i.e. List<T>.Clear() that will affect all elements inside all lists, and I want to actually dispose of the elements after passing them to the Scene Manager so I can free resources.

Here's my code :

List<Sprite> Players = new List<Sprite>();

List<List<Sprite>> Entities = new List<List<Sprite>>();

Entities.Add(Players);

node = new Scene2DNode(Entities, Vector2.Zero);

Players.Clear();

(...)

public class Scene2DNode

{

public Scene2DNode(List<List<Sprite>>entities, Vector2 scenepos)

{

Entities = new List<List<Sprite>>(entities);

(...)

In that example, all players will not be rendered onto the screen.



Re: XNA Game Studio Express List Implementation Question

Jim Perry

Why are you adding the Sprites list to another list




Re: XNA Game Studio Express List Implementation Question

GroZZleR

List<> (and all classes) are what's called a reference type. This means when you pass them to another method, you're not passing the actual value, but a "link" to the original data.

Take for example this:
List<int> list;
int number = 9;

...

void SomeMethod(List<int> list, int number)
{
list.Add(5);
number = 6;
}

...

SomeMethod(list, number);

// When this method returns, list will have increased in .Count by 1, adding the value 5. However, number (a value type) will still be set to 9. This is because it's not a reference to the original information, just a copy.

So when you're calling players.Clear(), it's affecting anything that had a reference to it, ie: your Entities list.

Now, on to part two. Why do you need a list of lists exactly All it looks like you want to do is copy the list of Players into the list of Entities. Is that correct Just do something like this:

foreach(Player player in Players)
Entities.Add(player);

Though I might not fully understand what you're trying to accomplish.




Re: XNA Game Studio Express List Implementation Question

Rafael Mores

Hum, I was still unclear about that being just a pointer to the value or had I copied the value like a new struct, that clarifies, thanks.

My programming experience doesn't go very far from ANSI C I'm afraid.

 

My idea is to keep a list of all players, another will all objects and so on, and pass them onto a list of entities, so the parameters for my functions will be as small as one list, and not expansible to the number of types of entities I have in the game. If this were C, I would just pass a pointer to the adress of the Lists, but then I read that needs to use unsafe mode and I'm not confortable with trying C# like that, plus, I'm sure there's got to be a more clever way to solve this.

List<Sprite> Players = new (...)

Sprite PlayerOne = new (..)

List<Sprite> Enemies = new (...)

Sprite BadGuy = new(..)

Players.Add(PlayerOne);

Enemies.Add(BadGuy);

List<List<Sprite>> Entities = new (...)

Entities.Add(Players);

Entities.Add(Enemies);

 

Still, with this kind of implementation, I'm having some trouble trying to do collision detection, because I have to iterate through all objects and check them with all objects on the other lists. My code looks something like this:

for (int i = 0; i< Players.Count ; i++)

{

for (int j  = 0; j<Enemies.Count; j++)

{
if (Collision(Players[ i ],Enemies[ j ])

return true;

}

}





Re: XNA Game Studio Express List Implementation Question

GroZZleR

Once again - I fail to see the point of the List<> of List<>s.

List<Sprite> Players = new ( ... )
List<Sprite> Enemies = new ( ... )
List<Sprite> Entities = new ( ... )

Sprite PlayerOne = new ( ... )
Sprite BadBuy = new ( ... )

Players.Add(PlayerOne);
Enemies.Add(BadGuy);

Entities.Add(PlayerOne);
Entities.Add(BadGuy);

Since Sprite is a reference type (I'm assuming it's a class not a struct on your end), any time you make any changes to PlayerOne, those changes will be changed for all lists.

In all honesty though, I don't see the reason for all the separate lists to begin with. I store all of my game objects in one big list and it's never been a problem. You just iterate through the list, updating their position, checking for collisions and rendering them. It doesn't really matter if it's a player, or an enemy, or a bullet, or a wall.




Re: XNA Game Studio Express List Implementation Question

Rafael Mores

In my case it's actually better to sort them in separate lists, because i have code specific for each like player input which dont apply to enemies and so on. I've though at best use a dictionary of lists where the key is an enum type, it's working fine now.

But my question regarding that was about copying a sprite onto a list, passing the list as an argument to my scene to draw and update, however, I draw/update/etc with another private list created using the new list<T>(listnametocopy) which copies them over to this new list object. I thought they were separate lists, not separate pointers to he same objects. I guess if I wanted to actually copy the elements to another one to delete the old one and still have the files I'd have to hard-code it, right Not that it's all that important to me now, I'll be implementing a level sctructure and reading it directly onto the scene list, but for experimentations with all other aspects of the game I'm defining some entities to play around a little bit first, since it's all been a challenge to learn step by step. :)

I guess I still could simply use one big list and make an enum type inside my sprite class and add it to the contructor for what i need, but then again, I think this way looks easier to me.

 

Thanks for your thoughts on it!