Multiple objects of one type, and memory.

Understanding the language, error messages, etc.

Multiple objects of one type, and memory.

Postby DFX2KX » Fri Sep 12, 2014 3:10 pm

Okay, so. I'm making a 'Missile Command' like Game, and Myndale was a massive help with breaking down object motion for me. While I was reworking code to take this into account, I came upon another thing for which I'm unsure as to the best-practice approach of.

But considering there are going to be lots of missiles, I am curious as to what a good approach is for storing the values for each one (I'm going to be using structs here.) Should I do something like:
Code: Select all
//define all possible missiles
missile MissileOne
missile MissileTwo
...

And just use the variables as needed (pre allocating the RAM for them, as it where), or is there a more elegant approach? I know Crabator has many crabs (all of whom are the same type of thing, obviously), but it's a bit hard to read through.

Apparently arrays work, too, or should.
Code: Select all
//Array of missiles
missile missileArray[15];
(somwhere later)
missileArray[5].start_x = 10;

The above seems valid, not any better on memory, but certainly easier to read.
DFX2KX
 
Posts: 250
Joined: Mon Apr 14, 2014 3:48 am

Re: Multiple objects of one type, and memory.

Postby rodot » Fri Sep 12, 2014 6:30 pm

DFX2KX wrote:
Code: Select all
//Array of missiles
missile missileArray[15];
(somwhere later)
missileArray[5].start_x = 10;


Yup it's the way to go :)
When I made Crabator I avoided the use of object oriented because I read somewhere that it was less efficient... but you're right, it's not readable, so arrays of object is the best way.
User avatar
rodot
Site Admin
 
Posts: 1290
Joined: Mon Nov 19, 2012 11:54 pm
Location: France

Re: Multiple objects of one type, and memory.

Postby DFX2KX » Sat Sep 13, 2014 1:09 am

Thanks Rodot! I learned about how primitive memory is in C/C++ when I was learning about multidimensional matrices. Still haven't gotten my head around having to predict memory usage. There is the nice perk of being able to scroll through all possible projectiles and do collision checks and updates in a big For loop (which I realized as I was dreaming about this aforementioned conundrum)
DFX2KX
 
Posts: 250
Joined: Mon Apr 14, 2014 3:48 am


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 68 guests

cron