Programming 2D Games
http://programming2dgames.com/forum/

When to use pointers for game objects??
http://programming2dgames.com/forum/viewtopic.php?f=9&t=266
Page 1 of 1

Author:  guizmo01 [ Tue Aug 05, 2014 6:28 pm ]
Post subject:  When to use pointers for game objects??

Hi,

In most of your projects, you rarely use pointers for your game objects, so my question is when and why I would use or not use pointers for my game objects??

KIND REGARDS

JC MAZZA

Author:  profkelly [ Tue Aug 05, 2014 11:03 pm ]
Post subject:  Re: When to use pointers for game objects??

I try to make the examples simple to follow. Many programmers are confused by pointers so I tend not to use them unless it is necessary. If I were going to create game items dynamically I would use pointers.

Author:  guizmo01 [ Sun Oct 19, 2014 7:31 pm ]
Post subject:  Re: When to use pointers for game objects??

Ok but WHY and WHEN would you want to create game items dynamicaly what is the difference?? Could you give us an example on when it suited to use a pointer.

if I do this for example:

player *ship;
then I have to add new and delete which i get it why we do this but its more coding

but if I do this:
player ship;
memory will be automatically allocated for me right?? which is less coding right??

what would make me decide between the two.

whichever way I do, I get the same results and everything compiles the same way at the same speed

So why and when is it suited to use a pointer or not and what is actually the purpose of it??

Author:  profkelly [ Fri Oct 24, 2014 12:26 pm ]
Post subject:  Re: When to use pointers for game objects??

Dynamic object creation occurs while the program is running. As opposed to static object creation which occurs at compile time.

Player ship;
This creates a ship object at compile time. The disadvantage of this method is that I must create every ship I will need for the entire game at compile time. That means all of the ship objects will need to reside in memory for the entire game. Maybe not a big deal with just ships, but now expand that to every object in the game. In large AAA game titles there may be thousands of unique game objects per scene with many different scenes in the total game. If we were forced to create all of those objects at compile time it would increase the memory requirements of the game to tremendously.

Player *ship;
All that is created at compile time is a pointer. We create the object when we need it.
ship = new Player();

When we are finished with it we delete it to release the memory.
delete ship;

Author:  bapowell [ Fri Nov 21, 2014 6:01 am ]
Post subject:  Re: When to use pointers for game objects??

Is it worth worrying about Stack vs Heap allocation in terms of performance? Probably a trivial distinction for simple games on modern machines, but I don't know much about it.

Author:  profkelly [ Fri Nov 21, 2014 3:18 pm ]
Post subject:  Re: When to use pointers for game objects??

I think the main difference in stack vs. heap is the allocation time required when using the heap (new) as opposed to the stack (local variable). If your code is super speed sensitive then you would want to perform the memory allocation (new) before. Perhaps during a transition from one level to another.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/