+ 3

Why are pointers useful in games?

So I have completed C++ (on another account) and I want to try creating games but I don't know where ponters would be useful. I know what they do. Please write what pointers in games can do.

2nd Jul 2016, 5:39 AM
Armandas Bolevičius
Armandas Bolevičius - avatar
4 odpowiedzi
+ 5
Imagine you are in a third person rpg (like wow, i think) and a couple of enemies are coming towards you, so you focus on one and attack him. When attacking, it is possible that the Player class (which is the class of the player, or you) may have an attribute called focusedEnemy. Lets say that that attribute is not a pointer. Ok, so we have: class Player { ... Enemy focusedEnemy; ... } When I attack him, i dont want to assign that enemy to my focusedEnemy variable, make changes to its life and then reassign the values of focusedEnemy to the original enemy. Thats when pointers come into place. If we rewrite the player class as: class Player { ... Enemy *focusedEnemy; ... } Now we can say: Give focusedEnemy the address of the current enemy, so that when we change the focusedEnemy variable, what we are really doing is change the current enemy itself. We could say something like: currentPlayer.focusedEnemy = &enemiesInPlay[someIndex]; This is a real example because the enemies could be stored in arrays.
2nd Jul 2016, 7:21 PM
Garme Kain
Garme Kain - avatar
+ 1
Another example, but not too related to games, is for graphs. A graph has nodes, and some nodes are connected to eachother. So the class Node could have two pointers going and coming that refer to the nodes connected to it. Basically, pointers are used when you dont want the copy of an object, but the object itself you could make similar things with references.
2nd Jul 2016, 7:26 PM
Garme Kain
Garme Kain - avatar
+ 1
To get texture or window pointers. To modify some local variables from a function.
4th Jul 2016, 10:04 PM
Mihai Dancaescu
Mihai Dancaescu - avatar