Could someone please give me a good example of how pointers and arrays are used effectively in a game? I just don't get it.. it's hard to learn when you think something is pointless.
Could someone please give me a good example of how pointers and arrays are used effectively in a game? I just don't get it.. it's hard to learn when you think something is pointless.
Be inspired.
Here's an example. A quick one.
Now, without arrays, if you wanted to declare 100 players, you'd have to do something like:Code:class cPlayer { public: cPlayer(); virtual ~cPlayer(); //Positions float pX, pY, pZ; //Rotations float rX, rY, rZ; //Velocities float vX, vY, vZ; //Accelerations float aX, aY, aZ; //Player Name char pName[20] }; //A class for one player
Or, why not just use:Code:cPlayer PlayerOne; cPlayer PlayerTwo; cPlayer PlayerThree; ... //Insert Four to NinetyEight in here cPlayer PlayerNinetyNine; cPlayer PlayerOneHundred
And, an even better choice would probably be Linked Lists (your question didn't ask about these, so you'll have to search up on your own. But don't worry, there are alot of resources out there on Linked Lists). The reason why I would recommend Linked Lists is because you're not bound by a specific number of players.Code:cPlayer Players[100]; //Note, that goes from Players[0] to Players[99]
Say you made an array of 100 players, what if you wanted 101 players?
Or, to be safe, you declare 1,000,000 players in an array, now you're most likely just wasting a lot of memory. And you're still stuck in that bad case of "What if 1,000,001 players want to play?".
With a Linked List (or the C++ Vector, though I prefer Linked Lists cause I like writing as much as I can myself, it helps me know what does what) you can add/delete players on the fly, so you'll always be using just the right amount of memory. Though I hear Vectors do some crazy thing where they use twice as much memory as necessary? Maybe somebody could tell me I'm wrong here...or clarify?
As for pointers, say you had a function that updates a player structure, such as:
When calling UpdateXYZ(), the function has to COPY all of Player's values into Temp, which is not only SLOW, but wastes memory. And then, once it's all updated, it has to COPY all that information back into Player in main().Code:cPlayer UpdateXYZ(cPlayer Temp) { //Update our velocities Temp.vX += Temp.aX; Temp.vY += Temp.aY; Temp.vZ += Temp.aZ; //Update our positions Temp.pX += Temp.vX; Temp.pY += Temp.vY; Temp.pZ += Temp.vz; return Temp; } //And it would be used as follows: int main(void) { cPlayer Player; //INITIALIZE ALL PLAYER VALUES HERE Player = UpdateXYZ(Player); return 0; }
Instead, something like:
Now we're not copying, but instead, we're accessing the values of Player directly, and changing them directly. No extra memory needed (except to store the pointer to our cPlayer type), and much faster since there's no copying.Code:void UpdateXYZ(cPlayer *Temp) { //Update our velocities Temp->vX += Temp->aX; //Same as: (*Temp).vX += (*Temp).aX; Temp->vY += Temp->aY; Temp->vZ += Temp->aZ; //Update our positions Temp->pX += Temp->vX; Temp->pY += Temp->vY; Temp->pZ += Temp->vz; } //And it would be used as follows: int main(void) { cPlayer Player; //INITIALIZE ALL PLAYER VALUES HERE UpdateXYZ(&Player); return 0; }
Now, there are of course better ways to do any of these functions. But there it is, and hopefully, it's accurate.
Last edited by Epo; 07-16-2005 at 08:24 PM. Reason: Prettying it up
Pentium 4 - 2.0GHz, 512MB RAM
NVIDIA GeForce4 MX 440
WinXP
Visual Studio .Net 2003
DX9 October 2004 Update (R.I.P. VC++ 6.0 Compatability)
Contested how? isn't it just the same?Originally Posted by Dave_Sinkula
>Contested how?
The statement was too general for Dave's taste. It's been changed to reflect the difference between naive changes based on unfounded performance differences between pointers and indices, and careful, advanced changes with the intent of improving performance.
What I'm surprised about is that there aren't more contested parts of that tutorial.
My best code is written with the delete key.
Thats a possibility of vector, since it doubles the size of the array when you go over the max, I believe (I assume its an array). Vector uses some sort of array (I believe thats part of what I read), and its resize function is the part that doubles its elements when you go over its max. Lets say you created your own array type class, that acted how a vector does. When you go over the amount of elements in your array (if you want to add player[3001] when the max of your array is player[3000]), a vector will resize your array to 2x the size of the array, leaving you with a 6000 in size array. Whereas if you specify the resize function yourself you could only add say 50 more allocated spots for players, leaving you with player[3050] as max, which makes more sense.Code:Though I hear Vectors do some crazy thing where they use twice as much memory as necessary? Maybe somebody could tell me I'm wrong here...or clarify?
Well.. the point of pointers is to save processing time, and temp. memory? and arrays can be passed as parameters of course, so add the two together. Lets say you have an array of players, like Epo's example of there, and you want to search the array for players whoes level is 10-50. To search the array you simply pass the pointer of the array to a function, and check the say GetHealth() function from each array element (player[1], etc.), assuming its a class that has the GetHealth function. Also if you wanted to change all of the players health to 0, that would be a better example of why you would have to use a pointer, and not copy the array over, because you have to be editing the memory address of the actual players. Thats all assuming the player array isnt globa, which it probably wouldnt be in a real game. Agh not such good examples.Could someone please give me a good example of how pointers and arrays are used effectively in a game? I just don't get it.. it's hard to learn when you think something is pointless.
Warning: Have doubt in anything I post.
GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101