Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
  • Help Room /
avatar image
0
Question by steadyrollinman · Jan 29, 2018 at 10:41 AM · inheritancegame developmentarchitectureabstractinterfaces

advice on resource management system architecture

hi so i got resources like wood stone food,each can be produced by multiple sources. each source will have different amount and rate of harvest. I will be harvesting with mouse click. I am confused how to go about and set things up.

First i have a resource script like this

 // class to define resources
 // attached to every object which can produce resource
 public class Resource: MonoBehaviour
 {
     public enum ResourceTypes
     {
         Wood,
         Stone,
         Food,
     };
   
     public ResourceTypes resourceType;
     public float gatherableAmount;
     public float gatherPerSec;
     public GameObject resourceImage;
     public AudioClip ResourceGatheringSfx;
 }
 

and add this as a component to each of the sources and select the resource type then with my input class that handles raycast i will have to compare hit.resourceType with a string of the resource type which to me feels bad cuz of alot of if statements.

 public class ResourceInput : MonoBehaviour
 {
     Resource res;
     void Update ()
     {
         if (Input.GetMouseButton (0)) 
         {
             Vector3 mousePos = new Vector3 (Input.mousePosition.x, Input.mousePosition.y, 10f);
             Ray ray = Camera.main.ScreenPointToRay (mousePos);
             RaycastHit hit;
 
             if (Physics.Raycast (ray, out hit, 100f)) 
             {
                 if (hit.collider.tag == hit.transform.GetComponent<Resource> ().resourceType.ToString ()) 
                 {
                     res = hit.transform.GetComponent<Resource> ();
 
                     if (res.gatherableAmount > 0) 
                     {
                         res.gatherableAmount -= res.gatherPerSec * Time.deltaTime;  
                         ResourceManager.Instance.AddResource (res.resourceType, res.gatherPerSec * Time.deltaTime);
 
                     } 
                     else 
                     {
 
                         hit.transform.gameObject.SetActive (false);
                     }
                 }
             }
         }
     }
 }


the problem arises in this script with all the if statements especially when i add more resources

 // class to manage resources collected
 public class ResourceManager : Singleton<ResourceManager>
 {
 
     public float woodCollected;
     public float stoneCollected;
     public float foodCollected;
 
     public void AddResource (Resource.ResourceTypes resType, float amount)
     {
         if (resType.ToString () == "Wood") {
             woodCollected += amount;
         }
         if (resType.ToString () == "Stone") {
             stoneCollected += amount;
         }
         if (resType.ToString () == "Food") {
             foodCollected += amount;
         }
     }
 }


can someone suggest me a good structure in which i do not have to do these if statements?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by KittenSnipes · Jan 29, 2018 at 11:02 AM

@steadyrollinman

I think I would make a separate class to define the type of resource then I would make a class that holds all the resources. I would make my resource class hold the string of the name, and a gameObject of the object. I would define whatever my resource is in that class and then hold how much of it in the other. Thats about it. Something like:

 Resource.AddResource(ResourceType.Wood, 10);
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 steadyrollinman · Jan 29, 2018 at 11:28 AM 0
Share

hmm i dont get how thats different than my first approach. I will still have to use if statements in my harvesting class using raycast to see which resource to add. i am trying to avoid that. what i want is that i should be able to click and call a method Harvest and i should harvest without this class knowing what to harvest

avatar image KittenSnipes · Jan 29, 2018 at 11:32 AM 0
Share

I’ll make sure to make some example scripts to show you how it is different when I am at my computer so until then cheers mate

avatar image steadyrollinman KittenSnipes · Jan 30, 2018 at 01:15 PM 0
Share

hey @$$anonymous$$ittenSnipes I have tried to follow your suggestion and updated the question but the problem remains the same. Can you clarify what should be done?

avatar image
0

Answer by Nighfox · Jan 30, 2018 at 02:06 PM

Not a really elegant solution, but if you want a quick one to use enums and in the same time serialize your members easily, you can do something like this. All you need to do is to assign types yourself (either in the inspector or some Init method):

 using System.Collections.Generic;
 using UnityEngine;
 
 public enum ResourceType { Wood, Stone, Food };
 
 public class ResourceManager : MonoBehaviour {
 
     //list of resource types, should only have unique types
     public List<ResourceData> resources = new List<ResourceData>();
 
     public void AddResource(ResourceType resourceType, float amount)
     {
         //loop through types of resources
         foreach (ResourceData res in resources)
         {
             //match found
             if (res.resourceType == resourceType)
                 //add amount to res data
                 res.amount += amount;
         }
     }
 }
 
 [System.Serializable]
 public class ResourceData
 {
     //you can add a definition var here, which will contain defining data (name, desc, etc)
     public ResourceType resourceType;
     public float amount;
 }

Now you can add as many types as you want without worrying to create an if statement for each resource type.

Comment
Add comment · Show 1 · 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 steadyrollinman · Jan 31, 2018 at 08:19 AM 0
Share

Thanks for replying with a approach using enums ,this is definitely better than my approach. it broadens my understanding thanks.

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

77 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 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 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 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

Need a parent class that holds a method that use (or not) a parameter 1 Answer

Child class cannot access property in method? 1 Answer

How to make RequireComponent list possible options. 1 Answer

Generic RPG Ability-System structure 0 Answers

Strange behaviour when inheriting parent class with Monobehaviour 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