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
1
Question by NickWalker12 · Apr 11, 2012 at 11:22 AM · c#itemmanagementadvice

FPS Advice for Item Management

Hello,

Working with C#, I've been looking into plausible methods for item management, and more specifically, item data storage. My requirements are:

  • Five item classes (Weapon, Collectable, Food etc.)

  • Of these five classes, there are 5-10 different item types. (Machine gun, pistol, sniper rifle etc). No difference in script, just different variables in the inspector.

  • Of these items, each individual item has its properties edited at runtime. (Weapon.fRateOfFire += 1)

  • Every single object needs to be able to be picked up/dropped, and stored in the player inventory.

While I'm not looking for one outright answer, any and all advice you could give to improve my method, or suggest appropriate alternatives, would be highly valuable.

My method

  • Item Manager: Stores the functions that allow picking up/dropping, and structures that the player can use to store variable data for instances of items.

  • Player_Generic: On a per-player basis, this script attaches to the player prefab, and deals with controls, interactions, and player data storage, including the player inventory. Every time the player picks up an object, its data is copied from its script and placed into this structure, to be stored in a list.

  • (Example) Item_Weapon: The class for each item type. This only contains public variables, and is attached to each item prefab.

  • Item_Weapon_Pistol(In the pistol prefab): In the inspector, I can set default values for these items. These will be spawned into the game world with their default properties, and during run-time, these variables can be edited.

While, in theory, this will work fine, it does seem like somewhere, what I'm doing is unnecessary. For example, I'm storing individual item properties in two places, depending on where it is. I.e. Both in the instance of the object, or as a structure inside the Player_Generic script. Ideally, I would just reference a global list of items. However, I have no idea how to save that, especially with varying structure types.

Which leads me to my next problem, how can I store multiple structs of varying (and sometimes identical) types in an inventory? Similar to minecraft, you could have two identical items, except one has high durability, while one has low. I know I need to make a primitive GUID system, but its only necessary for the inventory, as, outside it, items are separated by scripts.

To pick up items, I've got a working ray cast from player mouse, that returns the game object hit if it has the tag "Item". I then check that item for each of the scripts:

  1. Attempt to get the first script type using getcomponent. (working)

  2. If it is not null, get the data of that type (working) and put it into a struct. (not attempted yet, so far I can debug.log the data that I obtain)

  3. Delete the instance and return (working)

  4. If it is null, repeat with next script type, and see if the item contains that script.

The trouble I've had with structuring Unity scripts is that it differs quite dramatically with the language I'm learning primarily (C++). While I've been adjusting, I'm always worried that the methods that I've got for each system is completely inefficient, I just cant think of a better way.

However, from what I've read, making a global list of all items, and then simply moving the reference to the player inventory, seems to be the best option, I just need an example of that (or a base) to work with.

Cheers for reading, and thanks for the help!

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
1
Best Answer

Answer by Kryptos · Apr 11, 2012 at 11:29 AM

To solve the duplicate properties issues, you can 'delegate' the properties to a data class. The item object and Player_Generic will then have a reference to the data rather than dealing with copies.

To make sure thoses data are editable inside Unity, just make a Serializable class. Something like:

 public interface Item_Data
 {
 }

 [System.Serializable]
 public class Item_WeaponData : Item_Data
 {
     public float yourVariable;

     // ...
 }

 public class Item_Weapon : MonoBehaviour
 {
     public Item_WeaponData data;

     // ...
 }

 public class Player_Generic : MonoBehaviour
 {
     public List<Item_Data> itemsDataList;

     // ...
 }
Comment
Add comment · Show 3 · 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 NickWalker12 · Apr 11, 2012 at 11:39 AM 0
Share

Thank you very much! This always happens: I ask a question and moments later stumble upon loads of resources. So to deviate slightly, could you tell me more about the function "interface". I've never seen it before, so have no idea how its used. I can see that you put in in the same place as $$anonymous$$onobehaviour, but then again I dont know much about that either.

avatar image Kryptos · Apr 11, 2012 at 12:17 PM 0
Share

An interface is a class definition: you declare properties and methods without their implementation. It is like a contract.

In C#, a class can only inherits from one class, but can implement several interfaces.

In your case, the goal is to be able to have different Item_Data in the same list (`List`). But it is not mandatory, you can also use the non-generic list from System.Collections. Or you can create a base class for all your items and inherits from it:

 public class Item_Data
 {
     // put common properties here
 }

 [System.Serializable]
 public class Item_WeaponData : Item_Data
 {
     // put specific properties here
 }

The main part of my code sample is the use of a Serializable class which values can be edited inside Unity.

avatar image NickWalker12 · Apr 11, 2012 at 12:51 PM 0
Share

Again, thank you! Very informative! So an interface is like a virtual class in C++, that defines values, but to declare them, you must declare them inside classes?

Also, so you are saying that [System.Serializable] will prevent run-time variable changing? Sort of like an external database?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need advice for C# Freelance-Work 1 Answer

How to force a script to affect only a single instance of the object it is applied to? 0 Answers

Inventory AddItem help 1 Answer


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