How do i shorten my code

Right now i’ve hard coded summ of ints, how do i shorten this code?

    public void GetDamageNumber()
    {

        InventorySlot slot1 = inventorySlots[0];
        InventoryItem itemSlot1 = slot1.GetComponentInChildren<InventoryItem>();
        if (itemSlot1 != null)
        {
            dmg1 = itemSlot1.item.damage;
        }
        InventorySlot slot2 = inventorySlots[1];
        InventoryItem itemSlot2 = slot2.GetComponentInChildren<InventoryItem>();
        if (itemSlot2 != null)
        {
            dmg2 = itemSlot1.item.damage;
        }
        InventorySlot slot3 = inventorySlots[2];
        InventoryItem itemSlot3 = slot3.GetComponentInChildren<InventoryItem>();
        if (itemSlot3 != null)
        {
            dmg3 = itemSlot1.item.damage;
        }
        totalDamage = dmg1 + dmg2 + dmg3;

    }

Like I said before, don’t use enums for damage.

So how do i use int in scriptable object, if i need to choose item type and damage is depended on item type?

Have two separate values. One for the item type, and another for the damage.

Or, make an new object that encapsulates both.

And how do i chage int value on different item damage type in scriptable object?

I don’t really understand what you’re asking.

To answer your original question, you can cast an enum into an integral value:

int someInt = (int)someEnum;

But this isn’t going to scale. And there’s no need to use an enum when you can just use an integer value.

Once again, your questions are just basic C# stuff. Please do some proper C# learning.

I have this code, inside this scriptable object i have int damage, how do i change this value depending on damage type enum?

public class ItemSO : ScriptableObject
{
    public TileBase tile;
    public Sprite image;
    public int damage;
    public ItemType type;
    public Damage Damage;
    public Speed Speed;
    public ActionType actionType;
    public Vector2Int range = new Vector2Int(5, 4);

    public bool stackable = false;

}


public enum ItemType
{
    Laser,
    Engine
}

public enum ActionType
{
    laser,
    engine
}

public enum Damage
{
    LG1 = 50,
    LG2 = 100,
    LG3 = 150,
    not_laser = 0
}

public enum Speed
{
    EG1 = 10,
    EG2 = 15,
    EG3 = 20,
    not_engine = 0
}

Forget the enums. Just use an integer value.

1 Like

But how do i change its value, for different item types?

Then just do a simple calculation:

public int GetDamage()
{
	int damageTypeValue = (int)Damage;
	return damage * damageTypeValue;
}

But this seems completely redundant when you could just changed the serialized damage value. Why does this value have to be different based on an arbitrary enum?

Having two fields that only differ by a capital letter is also a terrible idea.

Once again, basic C# stuff. Please go learn C#.

In ItemSO i have item type laser. And damage type for different laser, different laser have different int damage.

Then just… set a different damage value! Why do you need the enum at all?

Sorry my bad, i got what you mean now!

So how do i shorten this code now?

    public void GetDamageNumber()
    {

        InventorySlot slot1 = inventorySlots[0];
        InventoryItem itemSlot1 = slot1.GetComponentInChildren<InventoryItem>();
        if (itemSlot1 != null)
        {
            dmg1 = itemSlot1.item.damage;
        }
        InventorySlot slot2 = inventorySlots[1];
        InventoryItem itemSlot2 = slot2.GetComponentInChildren<InventoryItem>();
        if (itemSlot2 != null)
        {
            dmg2 = itemSlot1.item.damage;
        }
        InventorySlot slot3 = inventorySlots[2];
        InventoryItem itemSlot3 = slot3.GetComponentInChildren<InventoryItem>();
        if (itemSlot3 != null)
        {
            dmg3 = itemSlot1.item.damage;
        }
        totalDamage = dmg1 + dmg2 + dmg3;

    }

Please learn some basic C#: Branches and loops - Introductory tutorial - A tour of C#

I won’t respond to you any more until you have done some basic C# learning.

1 Like
    public void GetDamage()
    {
        for (int i = 0; i < inventorySlots.Length; i++)
        {
            InventorySlot slot = inventorySlots[i];
            InventoryItem itemSlot = slot.GetComponentInChildren<InventoryItem>();
            if (itemSlot != null)
            {
                damage = itemSlot.item.damage;
                totalDamage += damage;
                break;
            }
        }
    }

Add int totalDamage = 0; just before the for loop.
If that code compiles, then totalDamage must be a variable that already exists in your class. You need totalDamage to be a local variable that is initialised to 0.

There are C# guidelines for naming your variables so that it’s easier to avoid this problem. For instance, _privateVariable, PublicVariable and localVariable.

GetComponentInChildren is also a very expensive function. You may want to modify InventorySlot to contain a reference to its InventoryItem so you don’t need to find it each time.

1 Like

Ive added total damage int before for loop. But i get strange numbers, when i add item to inventorySlots[0] it prints out 300 in debug log, when it should be 100 for 1 item

The easiest way to understand what is happening is to step through the code with a debugger. You can place a breakpoint on the first line inside your GetDamage function and see how the values of each variable change as you step through it. You should be able to find which line is not doing what you expect much more easily than by adding Debug.Log statements everywhere.

This link has guides for getting started with a debugger for Visual Studio, VSCode and Jetbrains Rider.

Also, it’s good that you’re checking if itemSlot != null before accessing it. You may need to check if slot != null as well unless you’re absolutely sure that it will be there. Null checks are not free in terms of performance, but when you’re starting out it’s better to be safe. You may also want to handle the case when something is null when it shouldn’t be, such as by throwing an exception or returning an error value.

1 Like

When i try to attach debugger, it says build started and took x seconds. And doesnt attach.