java Constructor parameters

Started by
15 comments, last by Kk1496 9 years, 10 months ago

@Glass_Knife: i did run into problems with that inheritance system i was using. I intended to load the items into a room by a single list. I didn't think this could be done because i had separate classes for HealthItems and Equipable items (by equipable i meant weapons or tools).I don't think i could reference both types of items by just making a list of items.

I tried the other way I had proposed. I ran into a problem where both Health item constructor and equipable item constructor had the same type and number of parameters.

I'm all out of ideas now :(

suggestions anyone?

Advertisement


I'm all out of ideas now
suggestions anyone?

You fell victim to one of the classic blunders: Premature optimization:

Read this: http://c2.com/cgi/wiki?PrematureOptimization

Long story short, you're trying to design an efficient solution to a problem you think you may have in the future.

I think, therefore I am. I think? - "George Carlin"
My Website: Indie Game Programming

My Twitter: https://twitter.com/indieprogram

My Book: http://amzn.com/1305076532

I think someone might have suggested this before and i finally understand it. I created an item class with basic attributes for an item and created another class for Health items where i am making an item object ass one of the health item's variables

I ran into another problem where. I created item class with basic attributes for an item and created another class for each type of item that consists of a basic item object. so, in order to keep all the items i needed to store them in a list or array of objects. My problem now is that when i can't reference functions for the objects in the array.

You seem to be fundamentally misunderstanding something. Your specific item types, like Weapon, shouldn't contain an item. That isn't going to help you at all. For a purely componentized approach, your item class should hold a list (or map) of Components. Specific components give the item its properties.

For example, you would have a Component base class and some subclasses might be WeaponComponent and BuffComponent. Your Item class maintains properties common to all items, like durability, weight, isTradeable, or whatever. A WeaponComponent would maintain properties that differentiate weapons from other items, damageAmount, damageType, requiredSkill, and so on. Using this approach, you could define your weapon components in a JSON file (or YAML/XML/Take Your Pick). Swords, maces, and bows become defined purely by data, rather than by concrete classes. Then, when you load in a weapon, you can do something like this:


WeaponComponent weapon = new WeaponComponent( /* values from wherever go here */ );
Item item = new Item( /* item values for this particular weapon * / );
item.addComponent( weapon );

// Let's make it magic
BuffComponent buff = new BuffComponent( /* buff values from somewhere */ );
item.addComponent( buff );

itemsList.add( item );

Using this, you can turn any item in the game, such as a chair, into a weapon. You can give any item, whether it's a weapon or not, a magic buff. It gives you a great deal of flexibility. However, you also increase the complexity a bit. For one thing, you'll need a way to tell if an item has a specific component or not. For example, when the player wants to attack an NPC, you'll want to check if the item he selects to attack with actually has a weapon component. How you do this mostly depends on what sort of container you choose to hold an Item's components, but also whether you want the item types to be fixed or want to allow modders to add new items, and so on.

Between the extreme of the purely data-driven component-based approach and the other extreme of the strict class hierarchy lies a middle ground where you can come up with a sort of hybrid system. In other words, there is more than one way to skin this cat. Every approach will come with its pros and cons. For games that are small in scope and that don't need the benefits of a component-based system, the class hierarchy is perfectly fine. Implementing one of these systems is the easy part. The hard part in making yourself aware of all of the options available to you and learning how to choose the approach that fits your needs.

@Aldacron O-oh! I see, so I was trying to stick the gameObject inside of the component instead of the component into the game object!

Thank you to all who have participated in the topic!

Ok. so, i tried creating an item class with the basic things I wanted for every item and a list to load the necessary components into. I created a print function to print all of those attributes. Then, i moved on to the first component. I created variables for the things that i wanted to vary in this type of item. However, I don't know how to make the print function work for the components. I am extremely confused about how to make the classes interact in general. How do i reference the item when i'm trying to print out all of it's attributes.

This topic is closed to new replies.

Advertisement