Hello,
So I’ve just programmed a small resource manager script that I plan to use in my game, but I’m not sure if this is the best way to do it.
Is there a way to make it simpler that I don’t know of?
So here is the code, I’ll [119465-resourcemanager.txt*|119465]
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResourceManager : MonoBehaviour
{
public static ResourceManager instance;
public List<Resource> resources = new List<Resource>();
private void Awake(){instance = this;}
//Is there a better way to get the resource out of the list? I'm not sure how to use .Find() with a class
private int GetResource(ResourceType resource)
{
//Loop over list till resource is found and return
for(int i = 0; i < resources.Count;i++)
{
if(resources*.resource == resource)*
return i;
}
//If nothing found
return -1;
}
//Updates the values in the resource list
public void UpdateResourceValue(ResourceType resource, int amount)
{
int value = GetResource(resource);
//Should I make this a try-catch instead, and than do no checking of value if >= 0?
//Check if in the correct range
if (value >= 0)
resources[value].amount += amount;
else
Debug.LogError(“Incorrect Index in ResourceManager, UpdateResourceValue()”);
}
private void Start()
{
//How I will update the resources from another class
ResourceManager.instance.UpdateResourceValue(ResourceType.Wood,-100);
}
}
[System.Serializable]
public class Resource
{
public ResourceType resource;
public int amount = 0;
}
public enum ResourceType
{
Wood,
Steel,
RawFood,
Rations
}
The above will work, but I enjoy improving upon my programming so any hints and tips would be really helpful.
Please Note that I can only upload a .txt file. Just rename the uploaded .txt file to .cs
*