Linked List/Entity Spawning

Started by
19 comments, last by yewbie 12 years, 10 months ago
Ok, I added the error back in, with a comment so readers won't get confused. I'm not in the habit of leaving bugs in example code. But yes, the error is in the technique of using the linked list, not implementing it, so you would have the same bug when using most implementations, including STL. Oddly enough, C macro based linked lists are the ones most likely to not have this issue!

Number of helpful posts in this thread: 1
Number of reputation points gained: -3

That's what I mean by inappropriate dogma.
Anthony Umfer
Advertisement
I like to just use a vector to store information, generally for entities when you don't really care about the order, vectors are very nice.
And when one is to be removed I mark it for delete with a bool, then before my loop for iterating through my vector I just remove that entity.

To go into more detail I use a class to make my entity struct, then I create a vector with that class as the vector type.
Then I make another function to make a new instance of that class, fill in the data members, then push_back to the vector.

edit: I also put all of the adding / removing of npcs in another big class that is my entity management class that handles adding, removing, loading, saving, etc.


class NPC
{
public:
int IDNum; //this is the ID number (this should be unique)
int LocX; //X location
int LocY; //Y location
string Name; //Name of NPC
bool MarkForDelete; //If we need to delete it before next loop (or skip it during the current loop)
}

vector <NPC> NpcList;



Also looping through a vector and getting a pointer is pretty easy

int NumNpcs = NpcList.size();
for(int npc=0;npc < NumNpcs; npc++) //loop through all npcs
{
NPC * ThisNpc = &NpcList.at(npc); //get a pointer to this npc so its more friendly to use
ThisNpc->LocX = 0;
}


edit: to all the above information, I think teaching people is great if they are willing to learn, but if he just wants to use something like std::list that has a lot of built in protections without knowing the in's and out's of everything its doing behind the scenes I don't really see a problem with that.

Ok, I added the error back in, with a comment so readers won't get confused. I'm not in the habit of leaving bugs in example code. But yes, the error is in the technique of using the linked list, not implementing it, so you would have the same bug when using most implementations, including STL. Oddly enough, C macro based linked lists are the ones most likely to not have this issue!
[/quote]
Essentially, the std::list leaks the underlying linked list behaviour, which is that deleting while iterating is tricky. If you can't do it correctly using the interface std::list provides, you almost certainly won't be doing it right if you do it by yourself. All you are doing is potentially adding additional bugs elsewhere.

Incidentally, your RemoveNode() doesn't appear to work for the head node.

@OP What they really want is a container. The first line of their post is:

I have been trying to make a top-down shooter just for my own experience and want to finish something.
[/quote]
Hand rolling a linked list does not add significantly to that end goal, and in fact will become a hindrance to it.

If you are serious about becoming a computer scientist, I believe you should be able to understand and write a linked list. For finishing your game, it is less of a priority.

If you are serious about becoming a computer scientist, I believe you should be able to understand and write a linked list. For finishing your game, it is less of a priority.


I guess I just cannot imagine a situation in which "I am using C++" and "I don't know how to implement a linked list" should both be true.

If you want to use C++, you have to know how to use pointers. If you want to know how to use pointers, you have to know how to write a linked list. I'm pretty sure it's physically impossible to understand pointers without understanding the linked list implementation.
Anthony Umfer

I'm pretty sure it's physically impossible to understand pointers without understanding the linked list implementation.


For years when I first started as a hobbyist game developer I was using a linked list class that someone else had written and it worked great.
I was using a linked list implementation and pointers without really understanding either.

If you want to use C++, you have to know how to use pointers.
[/quote]
I wrote a lovely little C++ program in this thread. I'm afraid I didn't use any pointers though.

I agree with the principle, a competent C++ programmer should understand pointers. I also agree that a good litmus test of understanding pointers is implementing a linked list. But we're in For Beginners here, so we're not dealing with experienced C++ programmers. If the OP hadn't emphasised the goal as "getting their game done", I would agree with you. I think people should be capable of writing their own basic data structures before they start using a pre-written one.
First off, sorry about this being super late. Hope you guys don't mind the giant wall of replies.

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++.[/quote]

Sorry, I really need to put something in my signature or something that says I'm using C++ 'cause I always forget to mention that =/.

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?[/quote]

If you are assuming that I'm in a CS course, then I'm not =P. I will be in a couple years but for now my only source of teaching will be the internet and myself. I guess it makes threads like these all the more important =P. ( I just wanted to specify that I'm not doing this for a grade but for my own knowledge).

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.[/quote]

To the former: won't really affect myself any but if something was wrong with something I'm glad it was pointed out if it means I won't have to spend 2 hours later wondering what's going on. To the latter: It will most definitely help, thanks a lot for your help =D.

edit: to all the above information, I think teaching people is great if they are willing to learn, but if he just wants to use something like std::list that has a lot of built in protections without knowing the in's and out's of everything its doing behind the scenes I don't really see a problem with that. [/quote]

I don't really know what I want to use but I'll side with whatever I think is most efficient and I can get working in a test program. Thanks for your extra input, it's always welcome to see another take on a subject.

Hand rolling a linked list does not add significantly to that end goal, and in fact will become a hindrance to it.

If you are serious about becoming a computer scientist, I believe you should be able to understand and write a linked list. For finishing your game, it is less of a priority.[/quote]

Those two things (experience and finishing something) should go hand-in-hand ideally. I want to work on something and finish it to figure out and implement everything I need to start a game and finish it. I would like to know exactly how everything is working, why everything is working, and what things need to be there in order for it to work. In this way, I would like to implement the linked list in my own way first (not copy pasting someone else's code but I would like to use some sorta template to understand what's going on faster). If std::list works I'll probably mess around with that as well as implementing it myself in different ways to see what's better for me.


I guess I just cannot imagine a situation in which "I am using C++" and "I don't know how to implement a linked list" should both be true.

If you want to use C++, you have to know how to use pointers. If you want to know how to use pointers, you have to know how to write a linked list. I'm pretty sure it's physically impossible to understand pointers without understanding the linked list implementation.[/quote]

Someone else already commented on this but I think I should add my own two cents to the table. I know how pointers work (perhaps not completely, but I know all the basics about them) but there are some logistical issues that crop up for (at least) myself sometimes when I try to view tutorials. I think that any, really experienced programmer should be able to look at some code and work out some of the basic workings at least but since I have never really implemented anything that requires pointers like this and with new things like dynamic memory that I hadn't really dealt with before.

I wrote a lovely little C++ program in this thread. I'm afraid I didn't use any pointers though.

I agree with the principle, a competent C++ programmer should understand pointers. I also agree that a good litmus test of understanding pointers is implementing a linked list. But we're in For Beginners here, so we're not dealing with experienced C++ programmers. If the OP hadn't emphasised the goal as "getting their game done", I would agree with you. I think people should be capable of writing their own basic data structures before they start using a pre-written one.[/quote]

I agree, this is why I asked for some tutorials and templates.
Prove me wrong so I can know what's right.
Okay so I have pseudo-successfully implemented a linked list to handle all the entities within my game (as in it compiles correctly and executes without a crash at the start, the player spawns and can move around) but I seem to have run into a problem with junk values being placed into the next entity in the list's address even after I set it to 0.

Initialization:
void InitGame()
{
srand(GetTickCount());

// Create a root for the player list
playerRoot = new sEntity;
playerRoot->ID = ROOT_DEFAULT_ID;
playerRoot->next = 0;


// Spawn a player struct inside the root list
playerRoot->SpawnEntity(PLAYER, gwx->Width / 2, gwx->Height + 32, 100, 100, 32, 32);

// Point the list to player
player = playerRoot->next;

player->invulnerable = false;

// Initialize mob's list root
entityListMobRoot = new sEntity;
entityListMobRoot->ID = ROOT_DEFAULT_ID;
entityListMobRoot->next = 0;

// Initialize projectile's list root
entityListProjRoot = new sEntity;
entityListProjRoot->ID = ROOT_DEFAULT_ID;
entityListProjRoot->next = 0;
}


The value immediately after it's supposed to be set to 0:
v43ozs.jpg

I dunno why it's doing this since the ID is set to 0 and not a junk value correctly. I read that 0xcdcdcdcd is a value specified for released addresses but it hasn't been released yet by my program as it hasn't even been initialized.

As always, any reply and all help is greatly appreciated.
Prove me wrong so I can know what's right.
0xcdcdcdcd is for uninitialised memory. The value -842150451 is also 0xcdcdcdcd, and I'd bet the various float/double values are also 0xcdcdcdcd. Something fishy is going on, from the looks of your code it should be impossible for this situation to occur. Have you tried a clean rebuild?

Your Entity constructor should initialise "next" to 0 however, you shouldn't have to set this yourself. The ID, if required, should be a constructor parameter.
[font="Consolas"][font="Consolas"][/font]

[/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][font="Consolas"] DestroyObject([/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]long[/font][/font][/font][font="Consolas"][font="Consolas"] idx)[/font][/font]

[font="Consolas"][font="Consolas"] {[/font][/font]

[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]delete[/font][/font][/font][font="Consolas"][font="Consolas"] Buffer[idx];[/font][/font]

[font="Consolas"][font="Consolas"] Buffer.erase(Buffer.begin() + idx);[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]return[/font][/font][/font][font="Consolas"][font="Consolas"] ;[/font][/font]

[font="Consolas"][font="Consolas"] }

[/font][/font]












This is in my game engine. I use std::vectors for Enitity buffer.But you can get the idea.

[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"] [/font][/font][/font]

[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"] [/font][/font][/font]

This topic is closed to new replies.

Advertisement