Where would you use linked lists in a game?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
Pickzell
Chaos Rift Junior
Chaos Rift Junior
Posts: 233
Joined: Sat May 16, 2009 10:21 am

Where would you use linked lists in a game?

Post by Pickzell »

I don't know they just seem kind of useless to me.
I'm an altogether bad-natured Cupid.
User avatar
Bakkon
Chaos Rift Junior
Chaos Rift Junior
Posts: 384
Joined: Wed May 20, 2009 2:38 pm
Programming Language of Choice: C++
Location: Indiana

Re: Where would you use linked lists in a game?

Post by Bakkon »

If they seem useless to you, then you obviously haven't read enough about them. Vectors and Lists from the C++ STL are both linked lists. My recent game stored everything in a vector to update and render each individual object. Then special lists were created on the side to keep track of each object's collision list. Go read up on them. They're the absolute polar opposite of useless.

Sorry, this needs to be done:
GyroVorbis wrote:
Pickzell wrote:I don't know they just seem kind of useless to me.
LOLWUT?
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

Re: Where would you use linked lists in a game?

Post by programmerinprogress »

They're very useful, now buy a book! :lol:

I find them useful for storing pointers to objects, there's a hell of a lot of things you can do with pointers in games, and other applications, having a means of storing objects dynamically can save a lot of headaches compared to say, a dynamic array, which can be difficult to manage, especially when you need to add more elements on the fly, it just can't be done, you have to reallocate the memory again, but STL containers or just gold 'ol homemade linked lists allow you to simply 'snap on' or remove elements you don't need anymore and you can just delete them from memory.

starting to see why linked lists could be used in a game?
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
wacko
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 12
Joined: Sat Oct 25, 2008 1:36 am

Re: Where would you use linked lists in a game?

Post by wacko »

So I think everything said so far is valid in some cases but overall I think the argument for and against a linked list really depends on its use. if your going to index into your list a lot then I would not use a linked list its slower than a normal array, a lot of Vector Impl actually use arrays because they index a lot. If insertion time matters then a linked list could possible be used as it will insert in many cases much quicker than an array. If deleting items in order or reading in order matters than linked lists are a possible option. I think many cases I find myself using them when order matters right, or f you want to insert into the middle of a list then I would use a linked list, but if otherwise use an array. Also linked lists take more mem in many cases as they have to keep around extra data.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Where would you use linked lists in a game?

Post by Falco Girgis »

No offense, but if a linked list seems useless then you have a looooot to learn, child.
wacko
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 12
Joined: Sat Oct 25, 2008 1:36 am

Re: Where would you use linked lists in a game?

Post by wacko »

GyroVorbis wrote:No offense, but if a linked list seems useless then you have a looooot to learn, child.
I do not know how often its really needed, I think there are specific cases where order matters and it should be used. Also I would like to comment that the STL Vector is not a linked list. It internally uses an Array to track its members it does not work the same way a linked list does.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Where would you use linked lists in a game?

Post by avansc »

linked list are the base of all complex data structures. trees and what not.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
internetfx
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 11
Joined: Mon Sep 28, 2009 1:11 am

Re: Where would you use linked lists in a game?

Post by internetfx »

Pickzell wrote:I don't know they just seem kind of useless to me.
Most objects in a typical game will be in a linked list when the number of that type of object is not fixed or limited.

When the number of those objects changes often, or there's no fixed limit to how many would be generated, you may not want to use an array/vector. Unused objects in a vector may waste too much memory.

If you find yourself allocating objects and storing pointers to those in a vector to save memory, and position in the array is not important, you may benefit from a linked list instead. Linked lists also work great for LIFO (last-in-first-out) stacks.

Examples for linked lists:

Objects and geometry
Enemies
Weapon fire
Most particles and temporary objects

Examples for arrays/vectors:

Players
Tiled maps
Inventory

Of course this could vary greatly.

Performance can be a problem for a linked list if you have to find an object in the list, even if it's sorted. For this, you'll probably need to step up to a sorted list such as a tree or associative array.
Post Reply