|
|
Or, use your gamerDNA username: (more...)
| ||||||
| |
![]() |
| | LinkBack | Thread Tools | Rate Thread | Display Modes |
| | #1 (permalink) |
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 231
+8 Internets | Partial classes vs other methods In my RPG, my Character class is getting WAY too big. I need to split it up, and I know of 2 methods for doing so... but which would be the best way? (btw this is using C#) I could use partial classes; public partial class Character, CharacterBattle, CharacterInventory, etc etc etc I could also make a CharacterBattle, CharacterInventory, etc etc into their own objects and create them in the Character class. Which would you do and why? |
| | |
| | #2 (permalink) |
| Registered User Join Date: Dec 2002
Posts: 373
| Partial classes are definately more elegant, after all, thats what they're there for. But essentially, both of those methods are doing pretty much the same thing. I would say which you think would be easier and/or neater. |
| | |
| | #3 (permalink) |
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 231
+8 Internets | I'm actually thinking of doing it with objects, few people said it would be much easier to refactor later on and if i ever ran into trouble with the partial class it could be a nightmare tracking down bugs |
| | |
| | #4 (permalink) |
| Fires of Heaven Officer Join Date: Jan 2002 Location: Melbourne, Australia
Posts: 3,366
+25 Internets | I'm no programmer but seems to me that using classes may be the better option but one more beneficial when working in a team environment where you have different people working on different parts. If you're doing everything yourself then maybe it's not such an issue? ps either way, comment / document everything! 8) |
| | |
| | #5 (permalink) |
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 231
+8 Internets | well I didn't know about partial classes, so I thought about doing it that way to learn about those... but... thye're SOO easy lol, and I totally saw a lot of potential for headaches down the road. (although setting them up initially would have been SO much easier) I'm just about done refactoring everything to work with the new system I'm setting up, only took a few hours, and at the same time I'm also refactoring my battle system to be cleaner so its good that I did this i guess. Oh man, you should see my code comments... When I'm done working on something for the day i take some time to go through EVERYTHING and comment it all... A friend saw my code and was baffled "Why do you comment everything like this? the method name is pretty descriptive...!" ![]() |
| | |
| | #6 (permalink) |
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 231
+8 Internets | actually, after playing around with it before owrk and coming home after work and playing around with it, i did the following; Battle methods I put into a partial class (because I have a LOT of battle methods and I didn't want them in the main class, but with the way I have everything else set up, i didn't want to have to keep passing things like the 'animation class to it and what not. Was much easier to just make that a partial class.) Inventory, Stats, and Magic I made objects used by the main class. Figure this was the easiest solution for my problems ![]() |
| | |
| | #7 (permalink) | ||
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 901
| While I'm mainly a Java developer, and we don't have partial classes, after reading the description on them it seems like a pretty darn bad idea to me, so I would stay away from them as much as possible if I were you 8) Seems to me (without trying to insult you here) that you somewhat lack the knowledge of object oriented design. It doesn't make sense to me to have classes called CharacterBattle, CharacterInventory, unless they are classes that should be different from classes named Battle and Inventory (though Battle is really a bad name for a class as well, BattleManager comes to mind for instance) and they are subclassing Inventory and Battle. A CharacterInventory class should instead be replaced by a function/method in your character class code. Code: private Inventory inventory = new Inventory(); // either subclass a collection [bad idea imo] or use Composition [the better choice]
public Inventory getInventory(){
return inventory;
}
public boolean addToInventory(Item item){
return inventory.add(item);
} This might seem like nitpicking but you will get a lot more clean design and a design that makes sense in a OOP kind of view, which is better in the long run. Quote:
Quote:
A note about a Battle logics class... Obviously a class like this should be a singleton, without a bunch of static methods. Code:
public class BattleManager {
private static BattleManager instance = new BattleManager(); // to avoid multiple instances of it. If you instanciate it with getSingleton multiple threads *might* create multiple instances
private BattleManager(){}
public static BattleManager getSingleton(){
return instance;
}
// your battlemethods! none static since the instance can be reached everywhere
} Last edited by slitz; 04-03-2008 at 12:56 AM.. | ||
| | |
| | #8 (permalink) | ||||
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 231
+8 Internets | Quote:
![]() Quote:
Quote:
Your inventory code is similar to what I have in my code. Code:
List(Item) inventoryItems = new List(Item)();
List(Weapon) inventoryWeapons = new List(Weapon)();
List(Armor) inventoryArmor = new List(Armor));
List(Armor) inventoryShields = new List(Armor)();
List(Armor) inventoryHelms = new List(Armor)();
Weapon equippedWeapon = new Weapon();
Armor equippedHelm = new Armor();
Armor equippedArmor = new Armor();
Armor equippedShield = new Armor();
public Weapon EquippedWeapon
{
get { return equippedWeapon; }
}
public Armor EquippedArmor
{
get { return equippedArmor; }
}
public Armor EquippedHelm
{
get { return equippedHelm; }
}
public Armor EquippedShield
{
get { return equippedShield; }
}
NOTE: Above, the arrows don't draw in the post, so I replaced them with ( )'s I also have a few methods to add item to inventory, sell item, destroy item, equipp item, etc etc... I am ALWAYS looking for feedback on how to do things though, i dont want to get trapped into one mindset, i'd much rather have the thought process of several solutions so I can pick the one that works best for my situation. (one of the reasons I started this thread )Quote:
Thanks for the help! Last edited by Cloud9_; 04-03-2008 at 10:50 AM.. | ||||
| | |
| | #9 (permalink) |
| where is my mind Join Date: Dec 2006
Posts: 5,855
+31 Internets | You can definately learn them concurrently, but yes, start utilizing OO as soon as possible. It's so much cleaner I can't even imagine not using it especially in the example you provided which seems so well suited towards it. |
| | |
| | #10 (permalink) | |
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 231
+8 Internets | Quote:
![]() EDIT: Also, I've tried to utilize OO design from day 1, my first thought was "if i will somehow be able to use this more than once in my code, put it into another method / class" and that snowballed into what i've been doing after reading the books I've read. An example is my items / inventory... each item is inherited from a BaseObject class, so are weapons, armor, helms, spells, etc. Each Spell creates a new spell and is added to a list of "knownSpells", calling the function GetKnownSpells returns the list, and then it can be used to draw that list on screen in my draw method. Last edited by Cloud9_; 04-03-2008 at 11:08 AM.. | |
| | |
| | #11 (permalink) |
| Registered User Join Date: Jun 2005
Posts: 268
+3 Internets | I'd say if you've been coding 4 months and you are already trying for good oo design you are ahead of the game. I wrote horrible horrible code for at least 2 years after I turned pro, not to mention the 5ish years before that ![]() Getting into the OO frame of mind was really hard for me, as I was an assembly / performance guy. If you haven't, have a look at design patterns. They are useful, but you shouldn't try to shoehorn everything into a pattern. It's cool to see what the supposed "best attack" is for alot of common tasks though. |
| | |
| | #12 (permalink) |
| Open the eyes Join Date: Oct 2002 Location: Not in fucking Acton, MA anymore!
Posts: 4,261
| If you really want to dig into Object Oriented design buy the Design Pattern book recommended in the C# XNA thead. Head First Design Patterns- it's good and incredibly helpful. I just finished running through the entire book. Still been too lazy to restart my project thought :| |
| | |
| | #13 (permalink) | |
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 231
+8 Internets | Quote:
| |
| | |
| | #14 (permalink) | ||
| euro scum Join Date: Aug 2002 Location: Sweden
Posts: 901
| Quote:
Quote:
You don't need five collections / containers whatever you want to call it, for five different items. You want one in fact. Assume you have a interface called Item with methods/functions that works for all items. Weight could be such function since all items have a weight if weight is relevant for your game, value of the item could be another one etc... A weapon isn't very specific either so that could be a interface as well with methods such as getDamageRange() and armor isn't specific either so that could be its own interface with getArmorClass() method etc. So in the end you have (this is just a extremely simple example ): Code: public interface Item {
// methods here
}
public interface Weapon implements Item {
// methods from item and Weapon here
}
public class TwoHandWeapon implements Weapon {
// 2HWeapon, weapon and item methods here
} So what's the big benefit from this more than giving all your Item, Weapon and Armor a blueprint (though this is reason enough..)? Code:
List(Item) inventoryList = new List(Item)();
private void fillInventoryWithRandomJunk(){
Item item1 = new TwoHandWeapon();
Item item2 = new ChestArmor();
Item item3 = new Junk(); // assuming it implements the Item interface
inventoryList.add(item1);
inventoryList.add(item2);
inventoryList.add(item3);
} Being the big pain in the ass I am, I would also have to comment on your Equipped stuff. I guess the large Character class is also a lot due to "design flaw" (note here, I'm really not trying to tell you that you suck, imo you've done pretty darn good for 4 months of programming, I'm just trying to give you some advice). While when you think of a Character in game that he have his own equipment, his stats, his appearance etc, the logics of the different attributes shouldn't be in the Character class, rather they should be available from the character class. Bad example (in character class): Code:
private Color hairColor = Color.BLACK;
private Beard characterBeard = new Beard();
public Color getHairColor(){
return hairColor;
}
public Beard getBeard(){
return characterBeard;
} Code: private Appearance characterAppearance = new Appearance();
// obviously, the different set methods should be called when creating your character
public Color getHairColor(){
return characterAppearance.getHairColor();
}
public Beard getBeard(){
return characterAppearance.getBeard();
} You could also of course, replace your List with a Map Hope you don't take this the wrong way now! And good luck with your project. Last edited by slitz; 04-04-2008 at 02:35 AM.. | ||
| | |
| | #15 (permalink) | ||||
| Registered User Join Date: Jul 2002 Location: Los Angeles California
Posts: 231
+8 Internets | Quote:
Quote:
Quote:
Quote:
Also, i did say above i'd release the source when the project was complete, this is NOTHING compared to the crap I'll prolly take for my coding choices when its actually complete > haha.. but I welcome it because I really am looking to improve | ||||
| | |
![]() |
|
| Thread Tools | |
| Display Modes | Rate This Thread |
| |