Linked List/Entity Spawning

Started by
19 comments, last by yewbie 12 years, 9 months ago
I have been trying to make a top-down shooter just for my own experience and want to finish something. Nothing special.

But, as always, something is barring me from progressing any farther and that would be: entity spawning and dynamic memory. I have never worked with dynamic memory beyond that which is required by multiple file projects so I don't have much experience. I'm looking to replace all the static arrays I had that contained information about projectiles, mobs, and the player into a dynamic linked list. The way I have functions set up is that one function is called for entity spawning, a different one for entity moving, another one for collision and so forth. I do not know how to have one global linked list but have it be accessible in different files and in different functions.

The problem I have ran into, obviously, is the problem of how to actually implement such a list. If someone could post a tutorial or something similar on how to do these I would greatly appreciate it. I have read several on linked lists but they're not too good at explaining exactly how you do it, what each piece does, why it works, and how to implement it yourself.

As always, any reply at all is welcome =).
Prove me wrong so I can know what's right.
Advertisement
Mentioning which programming language you're using would probably be helpful. Most programming languages have linked lists either built in to the language or as part of the standard library. Ex: std::list in C++.
// your game entity
class Entity {
};

// a Node contains an Entity and a pointer to the next Node
struct Node {
Entity entity;
Node *next;
};

// a list is just a pointer to the first node;
Node *list = NULL;

// add a node to the front of the list
void AddNode(Node *n) {
if (list) {
n->next = list;
}
list = n;
}

// remove a node from the list
void RemoveNode(Node *n) {
// first we have to find the node in front of it
for (Node *i = list; i; i = i->next) {
if (i->next == n) {
// i->next points to n
i->next = n->next;
// now i->next points to the node after n. n isn't in the list any more
break;
}
}
}

// add some nodes to our list
for (int i = 0; i < 5; ++i) {
Node *n = new Node();
AddNode(n);
}

// loop through the nodes
for (Node *n = list; n; n = n->next) {
n->entity.Draw();
// this will not work. try to figure out why
if (n->entity.IsDead()) {
RemoveNode(n);
delete n;
}
}
Anthony Umfer
Your loop is broken. As soon as I delete a node, the next iteration will attempt to access n->next, which dereferences a pointer to freed memory, which is undefined behaviour. If you're exceedingly lucky, it'll crash. Otherwise, worse will happen.

This is why you shouldn't try to write your own linked lists etc. without a very deep understanding of everything you're doing. Rely instead on existing, debugged implementations like std::list in C++.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Completely disagree. A linked list is the most fundamental data structure there is, and you should be able to write one blindfolded.

You can't remove from STL data structures while in a loop either, I was just trying to show an example of looping through the list.
Anthony Umfer
The waste of time reinventing the wheel every time you need a linked list aside, that's something you're only allowed to say if you can write one correctly. Not only did you have to write four times the amount of code that using std::list would have taken, your code contains at least one major bug, requires more effort to use than std::list, and only allows one linked list in the entire program.

Edit: reponse to added text:

You can't remove from STL data structures while in a loop either

Bullshit. And the problem isn't removing in a loop, the problem is that your node removal code is broken.
As I said, you'd have the same problem with STL list.
for (MyList::iterator i = list.begin(); i != list.end(); ++i) {
if ((*i)->IsDead()) {
delete *i;
list.erase(i);
}
}
would not work either.

Second, he's not reinventing the wheel here. He's writing his first linked list! Are you proposing 1st year CS courses switch to teaching STL list?

If my hastily-written example A* implementation had an error in it, I would not be catching shit, even though there's a perfectly fine implementation in Detour.

GameDev.net, where teaching programming is frowned upon lol

EDIT: There are reasons to use STL. Not knowing how to write a linked list is not among them.
Anthony Umfer
A) Just because you don't know how to do something correctly doesn't mean it can't be done.

for (MyList::iterator i = list.begin(); i != list.end();) {
if ((*i)->IsDead()) {
delete *i;
i = list.erase(i);
} else {
++i;
}
}

B) The problem isn't that removing something in a list in a loop can't be done, the problem is your node removal code is broken. You don't know how to remove a node from a linked list. I'm not suggesting that CS programs shouldn't teach how to write a linked list, I'm saying that YOU have no business suggesting people write their own linked list. You can't even write your own correctly.

If my hastily-written example A* implementation had an error in it, I would not be catching shit
[/quote]
Yeah, and no one claims that you can write an A* implementation "blindfolded". You claimed that everyone should be able to do so with a linked list and then demonstrated that doing so is beyond your capabilities.
No, the error is NOT in the RemoveNode function, it was in deleting the node while in a loop.

And of course you CAN remove while in a loop (though I would teach using std::list::remove_if, not that mess of code you posted).

OP, sorry your thread got shat on here. GDev has a lot of inappropriate dogma that gets brought out every time someone asks about C++ functionality provided by STL or Boost. Hopefully my example code was able to teach you something about how linked lists work.
Anthony Umfer

No, the error is NOT in the RemoveNode function, it was in deleting the node while in a loop.

Sorry, I got mislead by the fact that you edited your original post so that the discussion would now be about something other than what people could actually see. Funny how that can happen when you remove information in a discussion thread.

You can't remove from STL data structures while in a loop either


As I said, you'd have the same problem with STL list.


And of course you CAN remove while in a loop

Make up your mind. This "inappropriate dogma" pops up when people like you can't even get basic facts and details right about what you call something you can do blindfolded. Is it one of the hardest topics in computer science? No, but it's still more difficult than you make it out to be.

This topic is closed to new replies.

Advertisement