• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Ibzy · Jan 17, 2014 at 12:53 AM · classinventoryitems

'Item' class with various effects

Hi all,

I have followed various tutorials showing me how to create such things as an Inventory and 'loot' system for an RPG style game when I first started with Unity. I am now creating my first 'original' game and am struggling with 'Items'.

Very simply put I have a Shop and and Inventory. The shop will sell things like Weapons, Consumables, Upgrades and so on, and when purchased they move into the Inventory.

The store is an array of 'Items' which when bought get removed from the Shop array and added to the characters Inventory array.

I know to have things like Weapons and Consumables as sub-classes of Item with variables specific to their type (Weapon: power/type/fire rate, Consumable: Stat/Buff Value), but with Upgrades I want them to affect a different variable (such as armour, or weapon power).

I am struggling with my approach to this problem, so any suggestions are welcome.

One idea I toyed with was having the upgrades added to the inventory array too, just hidden (excluded when the inventory is populated) and the variable is += upgrade*100 (for example) but I don't know how (or even if) I can extract the item.type of each item in the array, and have it show - almost like [SELECT * FROM ITEMS WHERE ITEMS.TYPE = UPGRADE]?

Thanks

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by kannan21 · Jan 17, 2014 at 10:01 AM

Here is an example of how upgrade will affect different items in a different way.

 public class Item 
 {
 public virtual void upgrade()
 {
 }
 }

 Public class Weapon : Item
 {
 damage = 10;
 public Override void Upgrade
 {
 damage = damage +10;
 print("damage is now "+damage);
 }
 }
 
 Public class HealingPotion: Item
 {
 healValue = 10;
 public Override void Upgrade
 {
 healValue = healValue +10;
 print("healValue is now "+healValue);
 }
 }
 
 public class Inventory
 {
 public list<items> itemsInInventory;
 void Start()
 {
 item weapon = new weapon();
 item heal = new HealingPotion();
 itemsInInventory.Add(weapon);
 itemsInInventory.Add(heal);
 }
 public void UpgradeAll()
 {
 foreach(item i in itemsInInventory)
 {
 i.Upgrade();
 }
 }
 }


 result will be like:
 damage is now 20
 healValue is now 20
 
 The upgrade functions works differently for each item since we used virtual - override method.
Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ibzy · Jan 17, 2014 at 10:55 AM 0
Share

I like the look of this, but will this not require a setup for each type of upgrade?

This works well in the way that every item, on purchase, can call upgrade(), and if its something to be stored it does nothing (blank upgrade()) and upgrades can apply their respective upgrade() then destroy themselves (removeat[i]).

Iay be answering my own question here, but could an overridden upgrade() receive an int for magnitude? Have the item class include this and feed it in to the upgrade function like upgrade(item.magnitude)? That would then allow for various strengths of upgrade?

Thanks guys!

avatar image kannan21 · Jan 17, 2014 at 11:20 AM 0
Share
 operator overloading still works. So do like this
 
 public class Item 
 {
 public virtual void upgrade()
 {
 }
 public virtual void upgrade(int value)
 {
 }
 }
 
 and call the int overloaded function if needed.
avatar image

Answer by Fabkins · Jan 17, 2014 at 01:16 AM

Maybe one idea is you could add scripts to the objects. eg something like:

 obj.AddComponent("frostDamage");

So if you had a sword and has a normal doDamage script. You could check whether there are other scripts that could also do damage , they just need to have a common method. Get the damage produced from each script, sum them up, and thats the damage you will inflict.

Same could be for armour, etc. That way you can stack all sorts of things. Like a weapon that also has armour capabilities.

Comment

People who like this

0 Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ibzy · Jan 17, 2014 at 08:20 AM 0
Share

This sounds like a good approach, and has made me reconsider how I would do this, however doesnt fit with this particular project as the only equippable I have is Weapon. (Will definitely keep this in mind when creating my "traditional" RPG though)

My two new ideas for approach are: have Items and Upgrades separate. Separate classes, separate tab in the shop etcand thus Upgrades can be treated completely different to Items. Or, have Upgrade as an Item you purchase and equip. Rather than each upgrade adding to armour, you can only equip one (or maybe a handful) at a time - obviously higher benefits at higher cost further in the game.

avatar image Fabkins · Jan 17, 2014 at 10:24 AM 0
Share

If you only have one upgrade then just create one generic class and subclass it.

If you can have more than one upgrade , this is where the above could work for you. Yes, the upgrade could be an item. Whether you attach a component directory to the object or you attach an object with upgrade has similar effect.

In retrospect, I would probably favour having the upgrades as separate objects , each have a standard class (which you could subclass, see virtual example below), and attached the object to your weapon.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Inventory armor wielding proplem,How to convert from derived to base 1 Answer

Unity script system 2 Answers

About Classes in js 2 Answers

stack by quanity...? C# 2 Answers

How to make an inventory for my text advenutre? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges