• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
MrMark

Idea: Using Dictionary<enum, animation> vs <string, animation> C#

5 posts in this topic

Hi guys

Working on a game engine in 2d C# and currently working on the animation and entity classes. An entity has a set of animations can play and I'm tossing up between using enums or using strings as the key, ie

Entity player = new Entity();
e.play("playerWalk")

vs

Entity player = new Entity();
e.play(playeranimations.walk);

the latter would ensure at compile time that the call to play always contains a valid animation, while the former is far simpler to implement.

Using the later, each different entity type would need its own enum, but that's not too bad as I can easily parse my sprite's xml metadata and add a bit of code generation as a pre compile step. I'm leaning towards the enum approach as it would eliminate one potential source of fauilts at run-time, but I feel like i'm abusing the concept of an enum, so other peoples opinions would be welcome.

Here's my code if it helps visualise things

Entity class:
[code]
public class Entity
{
private Dictionary<int, Animation> AnimationSet;
public Enum Animations;
public Entity(Enum animations)
{
AnimationSet = new Dictionary<int,Animation>();
Animations = animations;
}
public void AddAnimation(Enum id, Animation a )
{
if (id.GetType() == Animations.GetType())
{
int idint = Convert.ToInt32(id);
AnimationSet.Add(idint, a);
}
else
{
throw new ArgumentException(String.Format("Incorrect Enum used, this entity uses '{0}' but passed in ' {1}'", Animations.GetType(), id.GetType()));
}
}[/code]

Here's my unit tests - Shows the class in practice.
[code] [Test]
public void EntityConstruct()
{
Entity myPC = new Entity(new PlayerAnimations());
myPC.AddAnimation(PlayerAnimations.die, new Animation("afd", null, 1, 1, 1, 1, 1, 1, 1));
}
[Test]
public void EntityConstructFail()
{
Entity myPC = new Entity(new PlayerAnimations());
Assert.Throws(typeof(ArgumentException), delegate() { myPC.AddAnimation(TankAnimations.die, new Animation("afd", null, 1, 1, 1, 1, 1, 1, 1)); });
}

enum PlayerAnimations { walk, run, die } // auto generated
enum TankAnimations { roll, shoot, die } // auto generated[/code]
0

Share this post


Link to post
Share on other sites
An enum with code generation would add a step to your content pipeline, the code would have to be rebuilt each time a new animation type is added. You can merge the enum's speed with the string's user friendliness by making a hashed string class. Store the string as a character array and hash it into a 32 or 64 bit int during creation, which is then used for all comparisons.
Because it works as a string, you can add content without any additional recompilation or asset conditioning. You can get rid of all runtime hashing fairly trivially, giving you the best of performance and flexibility.
0

Share this post


Link to post
Share on other sites
Have you tried to make some profiling on using one and then the other? My personal experience with c# is that string operations are so fast, even string comparing, that you can get away with it using plain strings, with no hashing needed. However, i recommend you to do a test first and check how much you can gain with using enums, but as turch said, you are adding up complexity too early. This is an optimization that should be done only when clear proof exists that the one with the strings is too slow.
0

Share this post


Link to post
Share on other sites
edit: realised that C# doesn't have typedef or define (not the way I use it below anyway). The advice still applies, you just have to use a C# appropriate way of doing it like a wrapper class.

[quote name='meeshoo' timestamp='1329323487' post='4913381']
This is an optimization that should be done only when clear proof exists that the one with the strings is too slow.
[/quote]

I'd add that you should go ahead and set up a way to change which method you use. Do a typedef or define something like ASSETSTRING to string. When trying out hashed strings all you have to do is change the define to your hashstring class, and everything will automagically work. If you just stick regular strings in your code, it will be virtually impossible to find all the right strings to replace, and you will probably introduce a lot of bugs. Later, when you've settled on an implementation, you can find+replace ASSETSTRING to whatever if you want.
0

Share this post


Link to post
Share on other sites
I'm leaning towards the version that uses the string. Unless you're going to be calling that Play() function a lot, the overall speed difference between using a string versus an int32 (or Enum) is going to be negligible. Plus, if you're concerned about errors creeping in, have the Play() function throw an exception or write to a log it it can't find the passed in string in the list of animations.
0

Share this post


Link to post
Share on other sites
From his post he doesn't seem to be concerned about speed and I wouldn't think he need to either. You basically call once each time you change animations. No need to worry about speed. What he is worried about is being able to add an animation name that doesn't exist and avoiding those development issue errors.

Pesronally I'd go string and leave it to the developer to work out that the animation names are correct. Nice and simple. I think you are trying to solve a problem that isn't that big of a problem to begin with.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0