Managing game engine entities

Understanding the language, error messages, etc.

Managing game engine entities

Postby gladoscc » Thu Feb 05, 2015 3:29 pm

Say I want to have a number of entities (such as enemies), each with their x & y positions, health, direction, pointer to the bitmap, etc. What's a good approach for managing them, such as creating, destroying, etc?

Just 'entity entities[20]' with a 'unsigned int entityCount', with entities added to the end (removals involve moving items in the array back)?

What's a good approach?
gladoscc
 
Posts: 7
Joined: Mon May 19, 2014 7:49 am

Re: Managing game engine entities

Postby ajsb113 » Fri Feb 06, 2015 1:47 am

From my limited experience, I would recommend using structs. You can then have an array of them and loop through the array accessing the values by saying for example Entities[i],x and such. If you want to see it in a whole program, you can take a look at my very messy code in my game Maruino. Hope this helps!
User avatar
ajsb113
 
Posts: 45
Joined: Tue Jun 24, 2014 4:47 am
Location: Illinois, United States

Re: Managing game engine entities

Postby Myndale » Fri Feb 06, 2015 7:55 am

Totally agree with ajsb113. Gamebuino/Arduino has so little available RAM that you really don't want the memory overhead and fragmentation headaches of dynamic memory management. If you have multiple types of different-size units then you can pack them into a single union, you'll probably wind up using more memory but you'll be able to use a single array to hold all of your units.

There are lots of little tricks you can play to save memory. For example let's say you have an array of 100 units and you need to keep track of whether they are active and which team they're with (i.e. black or white). That's 2 booleans per unit, or 200 bytes if you do it the obvious way (which Gamebuino doesn't have to spare). Or you can pack both of those bools into a single byte, so now you only need 100 bytes. That still leaves 6 bits per unit sitting there unused, so how about you pack the bits into a separate array; the code will be a bit more complex but now you only need 200 bits in total, or 25 bytes. If you think about it the active flag isn't actually needed, all you need to do is keep all the active units at the bottom of the table and a single byte indicating how many of them there are, so now we're at 13+1 bytes. Keep the black units at the bottom of the table and the white units at the other end and you're now down to 2 bytes, or 14 bits if you want to be really stingy.
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 19 guests

cron