Sign in to follow this  

Feedback on object creattion...

This topic is 4717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm working on my first game, and wanted some feedback on my current implementation for various object creation(items/spells/characters etc.). Currently I define a 'base' class like 'Item'. In that class's constructor I pass an identifier for the type if Item I want to create. Then inside of the class constructor I have a switch statement that will set the appropriate values for the class members based on the value of the identifier. Does this sound like a decent implementation? I really didn't want to define a different class for every individual item/spell/character etc., etc. Questions/Comments/suggestions ?? Thanks, Dave

Share this post


Link to post
Share on other sites
I think object creation should relate more to what variables are actually being stored for that class, then what they actually look like in the game. Take for instance if a sword and a knife have the same variables (worth, attack, weight, etc...) but only the picture is diffrent, then I would use the same class except load a diffrent picture or model. Also your switch object creation is a good idea, there is a design pattern called an "object factory", and that's exactly what it does. Make sure when you make it that it's expandable to adjust the diffrent objects variables upon creation, ie make sure you can use overloaded constructors.
hope this helps,
JDev

Share this post


Link to post
Share on other sites
Thanks JDev. Feels good to know I'm doing something half-ay right. The base classes I've constructed do have get/set methods for all of the defined class members (at least the ones that should have get/set), and I have a couple of implementations using overloaded contructors, where there may be a slight difference between objects.

Thanks again,
Dave

Share this post


Link to post
Share on other sites
Quote:
Original post by dhartles
I'm working on my first game, and wanted some feedback on my current implementation for various object creation(items/spells/characters etc.). Currently I define a 'base' class like 'Item'. In that class's constructor I pass an identifier for the type if Item I want to create. Then inside of the class constructor I have a switch statement that will set the appropriate values for the class members based on the value of the identifier. Does this sound like a decent implementation?


It sounds fairly reasonable, providing all your items are fairly similar in terms of properties and behaviour.

However if you find yourself having switch statements all over the place so that you can perform different activities based on the Item's subtype, that would be a good indication that subclassing Item would make sense.

Quote:
I really didn't want to define a different class for every individual item/spell/character etc., etc.


Basically the question you have to keep in mind is, "Which is more effort; creating different subclasses for each one, or writing the code to do different things based on the item's sub-type?" As soon as the answer is the second, you should consider subclassing to make your life easier.

Share this post


Link to post
Share on other sites

This topic is 4717 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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