Advertisement

Linked list woes....

Started by December 27, 2000 05:48 PM
3 comments, last by Pyro_256 24 years ago
In the tile based game I'm making there is a linked list that contains every entity and then each tile also contains a linked list of all the objects occupying that tile, so an object belongs to two linked lists at once. The inner-tile linked list works, but the global one does not. This code for updating all the objects causes it to crash:
  
int MapWrapper::UpdateObjs(int params){ //this updates the objects as well as doing moving type stuff

	BaseEntity *current;
	int tx, ty;
	if(LinkFirst){ // updating...

		current = LinkFirst;
		while(current){
			current->Update(params);
			if(current->moveX && current->moveY){ // this moves the object to another tile... untested

				current->RemoveDelete = LL_REMOVE;
				MapData[current->moveX][current->moveY].Add(current);
			}
			tx = (int)current->x;	// note the explicit conversions to remove warnings

			ty = (int)current->y;	// also note that this is outside the if so that garbage

			MapData[tx][ty].RemoveGarbage(); // will be removed even if nothing is moving

			current = current->nextGlobal;
		}
	}
	return(1);
}
[/source]

Specifically, the current->Update(params) line causes it to crash, but it will also crash with LinkFirst->Update(params).  I think that the objects aren't being properly added into the global linked list via this function:
[source]
int MapWrapper::FirstTimeAdd(BaseEntity *ObjToAdd, int x, int y){
	if(LinkFirst){
		LinkLast->nextGlobal = ObjToAdd;
	}
	else{
		LinkFirst = ObjToAdd;
	}
	LinkLast = ObjToAdd;

	LinkLast->nextGlobal = NULL;

	//MapData[x][y].Add(ObjToAdd);

	return(1);
}
  
Any ideas? Edited by - Pyro_256 on 12/27/00 5:49:42 PM
It seems like it is a problem with the FirstTimeAdd(). It looks like it is setting something to null when it shouldn''t. Check this out by either tracing with the debugger or write to a log file.

I think that the problem is here:

  LinkLast->nextGlobal = ObjToAdd ...LinkLast->nextGlobal = NULL  


I believe that this will end up setting ObjToAdd to null and screw it up. But check it with you debugger or if you can just print a statement to the screen saying which ones are null.

Just a thought.


-------
Andrew
Advertisement
Pyro: Out of curiosity, how are you going with saving your linked lists to disk?


"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..."
"When you are willing to do that which others are ashamed to do, therein lies an advantage."
"NPCs will be inherited from the basic Entity class. They will be fully independent, and carry out their own lives oblivious to the world around them ... that is, until you set them on fire ..." -- Merrick
It apparently has something against (non-pure) virtual functions; update (a virtual function) currently doesn''t do anything, but the shell of the function is there. If I replace update with any of the non-virtual functions within the class it doesn''t crash, but all the virtual ones cause it to crash.

About saving the linked list:
There isn''t a way to save the linked list(s) right now, only the terrain. I was planning on just saving the global one and then the game can just create the other one from the coordinates that every object has.
Off topic, but why does everyone insist on putting themselves through this? Learn how to use the STL, please!! It may be slightly annoying (I hate having to use iterators for everything, personally), but the containers are flexible and fast, and VERY easy to wrap if you don''t like using them directly (like me).

Sure, I COULD write my own linked-list class every time I needed one, but I have better things to spend my time on. And so does everyone else, I''m sure.

So please, PLEASE, learn how to use the STL. You''ll save yourself hours of coding, debugging, and breaking your keyboard.

Flame away,

Morbo

This topic is closed to new replies.

Advertisement