So I have created a fairly smooth game engine for my android game. I have a Renderer class which stores a single copy of each bitmap along with an array of rectangles. To renderer a specific image, it uses an id to get the rectangle and draws that portion of the associated bitmap. Each entity uses a texture id (currently an integer) to draws its texture through this renderer. However, there is one problem with this approach. If a texture has to be added, all of the ids have to be updated. I was thinking about creating a huge enum with names for each texture, so that the ids would only have to be changed in one place. I know this is bad practice, but would it be practical in this situation?
Members - Reputation: 108
Posted 20 May 2013 - 01:48 PM
I read on several forums that if you ever have to use a large enum (I mean hundreds of items), then either you are doing something wrong or there is a more efficient way to do things. Also, most of the times in games that I have seen an enum used they are less than fifty items or so. I guess another approach could be to create a hashmap that maps the indexes to a key, then store the key in each entity instead.
Edited by Dustin01, 20 May 2013 - 02:00 PM.
Moderators - Reputation: 19915
Posted 20 May 2013 - 03:01 PM
The motivation behind avoiding an enum in this case would be to let you keep resource/asset IDs decoupled from how they are stored and referenced internally in your code. For example, on larger projects, it is common to use a file name (whether an actual path on disk or a "pseudo" file name) such as "World/IceContinent/Temple/IcePillar.model" to refer to a specific asset. This can be a lot more manageable than having an enum entry for every asset.
In that kind of scenario a hashmap is often used as you mentioned. The real distinction isn't that it's somehow "bad" or "wrong" to use the enum - it's purely a matter of practical management.
If your code is clean, your asset count is small enough that you can hand-juggle the enum values, and you don't need richer features from a full asset ID system, go for it.
Moderators - Reputation: 38080
Posted 20 May 2013 - 04:44 PM
I've worked in large code bases that have either of the two solutions. They both have their own sets of pros and cons.
ApochPiQ touched on the one thing that hurts both of them: Make sure the unique ID never changes. Ever. It is a thing of nightmares.
This has several implications:
If you are going with an asset-naming approach, make sure the names are intelligently chosen before hand. People will hate you when they discover they need to rename 2500 assets in version control, rename textures and such in Maya, and otherwise undo and redo countless hours of work. Make sure the names are clearly documented to avoid their accidental use or misuse.
For numbers, if you assign an asset of ID=7, that object must be that value forever. If you decide to deprecate that ID don't remove it; just leave a dead number. When you create a new item add it to the end of the list, never add it next to its neighbors because it causes numbers to shuffle. Never intentionally create holes (e.g. player units start at 1000, enemy units start at 2000, map objects at 3000, etc) because eventually you will discover something is insufficient and will overlap another range and numbers will need to shuffle.
Personally I dislike filenames as a keyword since it limits extensibility. You'll invariably come up with an item that ought to have a different name but the rules force it. Other times you'll have something that needs a bit of functionality and it can only get it by renaming the asset to something it really isn't. I prefer having a bunch of enormous lookup tables with integer constants; constants can be added and removed from the tables much easier than renaming them in version control. Also with numbers, something can be added to multiple lists at once, it is hard to give something two names at once.
Edited by frob, 20 May 2013 - 05:32 PM.
Check out my book, Game Development with Unity, aimed at beginners who want to build fun games fast.
Also check out my personal website at bryanwagstaff.com, where I occasionally write about assorted stuff.
Members - Reputation: 108
Posted 21 May 2013 - 10:00 AM
This has been very helpful. I suppose I will just stick with using the integers as the ids.
Never intentionally create holes (e.g. player units start at 1000, enemy units start at 2000, map objects at 3000, etc) because eventually you will discover something is insufficient and will overlap another range and numbers will need to shuffle.
This is exactly what I did, in fact. I guess what I will do is go through and just try to determine all of the resources that I think I will need beforehand. If more must be added later, I will just add them to the end.