Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by ccrh · Aug 15, 2014 at 04:43 AM · inventoryinventory system

Defining an Item type in C#

I want to word this so it makes the most sense. I want to have a custom variable type that I can define a bunch of things for, like itemID and quantity and so on, but I also want to be able to define a ... void?

I want to make it so that if the player is holding an Item heldItem, the code can call heldItem.invicon to get the icon for the held item, call heldItem.quantity to find out how many are in the stack in the hand, call heldItem.rightclickAction to find what should happen for that Item when the right mouse button is pressed, AND all of those pieces of information are defined beforehand when the script for those items are being coded.

I code the main script then go through for each item I have in my game and define them such that they have all of the specifications for what an Item is

Also in the script I use to manage the inventory for the character I just have a list of items defined by something like

 public static int capacity = 4; //how big is the inventory
 public Item[] inv = new Item[capacity]; //define the inventory




I think I read about this in Java before and I think it might be something like implements or extends? Er, might be interface? I dont know what the equivalent would be in C#

I am sure this is a thing in C#, I just dont know the name of it or the exact syntax of it.

My gosh this is the worst brainfart I have had this entire year. I can SEE the kind of code I am talking about, I am sure minecraft even uses it in some of its code (forget that it is Java for a moment) Gah!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Kiwasi · Aug 15, 2014 at 04:50 AM

Two main ways to accomplish this

Inheritance. Each sword is a weapon.

 public class Weapon{
     public float damage;
 }
 
 public class Sword : Weapon {
     // Sword automatically has a float called damage
 }

The other option is interfaces

 public interface ILife {
     public float health;
 }
 
 public class Enemy : Monobehaviour, ILife {
     // This script must declare a public float health
 }

Its possible to do both of the following

 List<Weapon> listOfAllWeapons;
 List<ILife> listOfAllThingsWithHealth;

As always the official tutorial is excellent

Comment
Add comment · Show 4 · 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 GameVortex · Aug 15, 2014 at 06:19 AM 0
Share

Interfaces cannot contain variables though, only functions.

avatar image Kiwasi · Aug 15, 2014 at 06:28 AM 0
Share

$$anonymous$$SDN has a different opinion. Interface properties.

$$anonymous$$y code may not function exactly as written, but the idea is sound. Will admit that I have never tried to implement properties in an interface in Unity. I tend to use components in place of interfaces.

avatar image GameVortex · Aug 15, 2014 at 07:26 AM 0
Share

Yes, properties can be included too as long as they do not have a definition. The idea is sound I agree, an interface would work in this situation.

avatar image ccrh · Aug 15, 2014 at 09:24 AM 0
Share

This appears to be working perfectly, thank you so much!

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

AddComponent Question 1 Answer

Having scripts interact 1 Answer

Instantiate a destroyed GameObject by its original Prefab from the "Assests" folder? 0 Answers

how to detect wich item i have in my inevntory 0 Answers

Best way to implement RPG Inventory System (w/ Sized Items) 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