This is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Item
{
public Inventory inventory;
public enum ItemType
{
Blocks,
Potions,
Weapons,
}
public ItemType itemType
public bool stackable;
}
and what i am trying to do is to change only the enum ItemType Blocks
to stackable so that i can just use the add the item to the list, and if the item is block, it will be set to stackable by default , how can i achieve this? I tried ItemTypes.Blocks.stackable = true; But it won't work.
CodePudding user response:
Option 1:
Use an extension method to determine what is stackable.
public class Item
{
public Inventory inventory;
public enum ItemType
{
Blocks,
Potions,
Weapons,
}
public ItemType itemType; // Defaults to Blocks
}
public static class ItemTypeExtensions
{
public static bool IsStackable(this Item.ItemType type)
{
return type == Item.ItemType.Blocks;
}
}
Usage
var item = new Item();
bool isStackable = item.itemType.IsStackable(); // true
item.itemType = Item.ItemType.Potions;
bool isStackable = item.itemType.IsStackable(); // false
Option 2:
When you declare an enum value, it automatically uses the default element as the value. For any enum, that is the one that has the value 0. If you don't give them values (as in this case), the compiler will assign the first element with value 0. Therefore, the way you have constructed the enum ItemTypes.Blocks
is the default value. You can rely on this default when your Item
object is constructed.
public class Item
{
public Inventory inventory;
public enum ItemType
{
Blocks,
Potions,
Weapons,
}
public ItemType itemType; // Defaults to Blocks
public bool stackable = true; // Set default explicitly (which matches up with blocks)
}
However, if you want the boolean value to stay in sync with your enum value, you either need to set them at the same time or create a method on the Item class to set them both together.
CodePudding user response:
Your question is quite poorly-worded so it is rather confusing. I'm going to make my best guess as to what you mean and recommend based on that. Firstly, get the enum
out of the class. Nesting types is generally not a good idea.
public enum ItemType
{
Blocks,
Potions,
Weapons
}
Next, use properties to expose public data rather than fields. You can then provide additional implementation details:
public class Item
{
public Inventory Inventory { get; set; }
public ItemType ItemType { get; set; }
public bool IsStackable => ItemType == ItemType.Blocks;
}
The IsStackable
property is read-only and its value is generated on the fly based on the value of the ItemType
property.
CodePudding user response:
You can use struct for this purpose, because enum
does not do this. enum
only numbers defined titles in int.
public struct ItemType
{
public List<Block> blocks;
public int potions;
public List<Weapon> weapons;
}
public ItemType itemType; // define struct same as enum
Note that despite the list definition in struct, structs will not be able to define initial values. Therefore, you need to enter the first values of the list after defining it.
public void Start() => itemType.blocks = new List<Block>();
Then just add and subtract to the block list. struct
have many uses in defining multiple variables, but changing the variables within them may not always be easy. See link X for more information.
public void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
itemType.blocks.Add(new Block());
Debug.Log(itemType.blocks.Count);
}
}