help programming!

Started by
19 comments, last by GameDev.net 18 years, 6 months ago
Quote:Original post by BeerNutts
Quote:Original post by matthughson
Quote:Original post by Hunter_Ex
...i used arrays before but it takes much memory...


Also, an array should take up less memory than a linked list in theory.

Matt Hughson


Please explain this, because it's very wrong.


Yeah basically you have all the memory that an array would use (for storing the actual objects) plus the memory needed for the pointers in the linked list (and any other memory the linked list uses).

Matt Hughson

PS- "Please explain this, because it's very wrong." Who says stuff like this??
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
Advertisement
Quote:Original post by Hunter_Ex
u have some points but the sorting also takes time.
what u think about making a game with linked list's??


yea, sorting takes time, which is why i said keep only static collision objects in that data structure - you only sort once when the level is loading.
for example in the game of elastomania (i think thats what its called, the 2d motorcycle in a polygon world) i would try to sort the polygons somehow so i can check collision with them fairly quick, while if i had moving objects (saying boxes you can push, or moving platforms or moving enemies) i would put it in a different unsorted datastructure (either array or linked list). for collision i would then check both the sorted and the unsorted.

as for the question of array or linked list - the choice depends on how much you need to insert/remove from the structure and how much you need random access. For collision detection you dont need any random access (you loop checking for each so can do it one after the other) so linked list works fine, and the only advantage you gain from array is that its abit simpler to program. so if you dont add/remove alot you can choose array.
Linked list is my choice since in array as people suggested you might have say 500 cells and 499 empty cells but you still need to check all of them - not elegant.

but as i said first - you should start from simplest approach and fix it later if you find it too slow (i doubt you will feel slowness).

Iftah.


I believe BeerNutts, stating "Please explain this, because it's very wrong" about a linked list taking up more memory than an array, was referring to the specific scenario that Hunter_Ex brought forth -- namely, storing entities for the game in a linked list. And indeed, the linked list implementation will take up less memory on average than the array implementation, because the linked list can allocate memory on the fly and only use as much as necessary for the current number of entities, whereas the array implementation will always take up a fixed amount of memory that will on average be greater than the current number of entities.

I would advocate the linked list method over an array in this scenario not for the amount of memory it takes, but because it has
- Fast insertions and deletions, O(1)
- Equivalent searches, O(n)

An array only has the advantage in random access, which will happen much less often than any other operation in a typical game. Note that I'm not taking sorting into account for either one, because if it is for some reason necessary it can usually be done once at load time.

Cheers,
Twilight Dragon

EDIT: If you're using the standard C++ data structures such as std::vector (a better array) and std::list (a linked list), it's about as easy to program one as the other.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
If only there were a data structure that was somewhere between a linked list and a static array... [wink]

Quote:Original post by TDragon
EDIT: If you're using the standard C++ data structures such as std::vector (a better array) and std::list (a linked list), it's about as easy to program one as the other.


Bah your edit ruined my joke!

Matt Hughson
__________________________________[ Website ] [ Résumé ] [ [email=contact[at]matthughson[dot]com]Contact[/email] ][ Have I been Helpful? Hook me up! ]
Well, at least he didn't mention a std::deque too, which is more of a hybrid than std::vector.
My advice: Use the STL... It's an official part of C++ because it's good. Instead of always having to make your own, it's simply there for use. An array would be faster, but the speed difference honestly doesn't make a difference when you dealing with a few of them, but if you're dealing with a few million, it could.
We should do this the Microsoft way: "WAHOOOO!!! IT COMPILES! SHIP IT!"
thx for all replies and about the memory with arrays ^^

iknow but i didnt thought of making a bool and reuse old parts of the array
so i use to make some big arrays where the old just were left in memory ( very bad)

so now i have gotten lotsa new ideas thx all....

HunterEx
Blekinge Institute of Technology
Twitter [twitter]devmoon[/twitter]
Homepage http://devmoon.se
Stream http://twitch.tv/devmoon
Hello,

I think that a AVL tree is ideal for what you want to do. It performs as good as an array for searching and is better for inserts and deletions. Compared to a linked list it is better at searching and deleting. One neat feature of an AVL tree is that the data is ordered the same as an ordered array if you use an inorder traverse (I don't know if this has any relevence to your application).

It is basically the best of both linked lists and arrays.

regards
okay didnt knew of that :D

but i think ill stick to ARRAYS with use boolean, its not a gigantic game!!

/cheers
Blekinge Institute of Technology
Twitter [twitter]devmoon[/twitter]
Homepage http://devmoon.se
Stream http://twitch.tv/devmoon
I use a (heap-based) priority queue to update, a sorted array to render, a (array-based) stack for pool allocation and a (singly) linked list for collisions.

This topic is closed to new replies.

Advertisement