Better way to program resource manager script

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
*

I have made some slight modifications to your code, in some cases I renamed the variables so that they made a bit more sense reading them (i.e. resourceType instead of resource, etc.)

I made a change to your GetResource() so that it actually returns a reference to your resource object. It uses the optimize Find() method of the list, with a lambda method that provides the predicate (basically the conditional test) to determine which object it will return.

I refactored the UpdateResourceValue() method to use the modified GetResource() so that it would get the object, validate it is not null and then update the value. The result of my changes are as follows:

using System;
using System.Collections.Generic;
using UnityEngine;

public class ResourceManager : MonoBehaviour
{
    public static ResourceManager instance;
    public List<Resource> resourceList = new List<Resource>();

    private void Awake() { instance = this; }

    // Using the List<> Find() method to get a reference to th resource object.
    private Resource GetResource(ResourceType resourceType)
    {
        return resourceList.Find(resource => resource.resourceType == resourceType);
    }

    // Updates the values in the resource list
    public void UpdateResourceValue(ResourceType resourceType, int amount)
    {
        Resource resource = GetResource(resourceType);

        if (resource != null)
            resource.amount += amount;
        else
            Debug.LogError("Could note find resource of required type in ResourceManager, UpdateResourceValue()");
    }

    private void Start()
    {
        //How I will update the resources from another class
        ResourceManager.instance.UpdateResourceValue(ResourceType.Wood, -100);
    }

}

[Serializable]
public class Resource
{
    public ResourceType resourceType;
    public int amount = 0;
}

public enum ResourceType
{
    Wood,
    Steel,
    RawFood,
    Rations
}