C
C#8mo ago
~Linc~

Can Someone help me fix this messy code and simplify it.

public List<Slot> FuelSlots = new List<Slot>(); public List<Slot> InputSlots = new List<Slot>(); public List<Slot> OutputSlots = new List<Slot>(); [Header("Furnace Slots")] public GameObject FuelSlotOne; public GameObject FuelSlotTwo; public GameObject InputSlotOne; public GameObject InputSlotTwo; public GameObject InputSlotThree; public GameObject OutputSlotOne; public GameObject OutputSlotTwo; public GameObject OutputSlotThree; private Slot FuelOne; private Slot FuelTwo; private Slot InputOne; private Slot InputTwo; private Slot InputThree; private Slot OutputOne; private Slot OutputTwo; private Slot OutputThree; private void Start() { FuelOne = FuelSlotOne.GetComponent<Slot>(); FuelTwo = FuelSlotTwo.GetComponent<Slot>(); InputOne = InputSlotOne.GetComponent<Slot>(); InputTwo = InputSlotTwo.GetComponent<Slot>(); InputThree = InputSlotThree.GetComponent<Slot>(); OutputOne = OutputSlotOne.GetComponent<Slot>(); OutputTwo = OutputSlotTwo.GetComponent<Slot>(); OutputThree = OutputSlotThree.GetComponent<Slot>(); }
3 Replies
2spooky2play
2spooky2play8mo ago
you can probably use a loop to store and get the slots
public List<Slot> FuelSlots = new List<Slot>();
public List<Slot> InputSlots = new List<Slot>();
public List<Slot> OutputSlots = new List<Slot>();
public List<Slot> FuelSlots = new List<Slot>();
public List<Slot> InputSlots = new List<Slot>();
public List<Slot> OutputSlots = new List<Slot>();
you already have these lists, why not use those?
SpReeD
SpReeD8mo ago
You are misusing the concept of a Field and a Property. I don't know from where and when the Furnace Slots are set, but here's an example:
internal class Fuel
{
public List<Slot> FuelSlots { get; } = [];
public List<Slot> InputSlots { get; } = [];
public List<Slot> OutputSlots { get; } = [];

[Header("Furnace Slot")]
public GameObject FuelSlotOne { get; init; } = new();
[Header("Furnace Slot")]
public GameObject FuelSlotTwo { get; init; } = new();
[Header("Furnace Slot")]
public GameObject InputSlotOne { get; init; } = new();
[Header("Furnace Slot")]
public GameObject InputSlotTwo { get; init; } = new();
[Header("Furnace Slot")]
public GameObject InputSlotThree { get; init; } = new();
[Header("Furnace Slot")]
public GameObject OutputSlotOne { get; init; } = new();
[Header("Furnace Slot")]
public GameObject OutputSlotTwo { get; init; } = new();
[Header("Furnace Slot")]
public GameObject OutputSlotThree { get; init; } = new();

public void Start()
{
this.FillSlotList("FuelSlot", this.FuelSlots);
this.FillSlotList("InputSlot", this.InputSlots);
this.FillSlotList("OutputSlot", this.OutputSlots);
}
}

internal static class Extensions
{
public static Slot GetSlot(this GameObject gameObject)
{
return gameObject?.GetComponent<Slot>();
}

public static void FillSlotList(this Fuel fuel, string name, IList<Slot> slotlist)
{
IEnumerable<PropertyInfo> properties = typeof(Fuel).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo item in properties.Where(x => x != default && x.Name.StartsWith(name, StringComparison.InvariantCultureIgnoreCase) && x.GetCustomAttributes<HeaderAttribute>().Any()))
{
slotlist.Add(((GameObject)item.GetValue(fuel)).GetSlot());
}
}
}
internal class Fuel
{
public List<Slot> FuelSlots { get; } = [];
public List<Slot> InputSlots { get; } = [];
public List<Slot> OutputSlots { get; } = [];

[Header("Furnace Slot")]
public GameObject FuelSlotOne { get; init; } = new();
[Header("Furnace Slot")]
public GameObject FuelSlotTwo { get; init; } = new();
[Header("Furnace Slot")]
public GameObject InputSlotOne { get; init; } = new();
[Header("Furnace Slot")]
public GameObject InputSlotTwo { get; init; } = new();
[Header("Furnace Slot")]
public GameObject InputSlotThree { get; init; } = new();
[Header("Furnace Slot")]
public GameObject OutputSlotOne { get; init; } = new();
[Header("Furnace Slot")]
public GameObject OutputSlotTwo { get; init; } = new();
[Header("Furnace Slot")]
public GameObject OutputSlotThree { get; init; } = new();

public void Start()
{
this.FillSlotList("FuelSlot", this.FuelSlots);
this.FillSlotList("InputSlot", this.InputSlots);
this.FillSlotList("OutputSlot", this.OutputSlots);
}
}

internal static class Extensions
{
public static Slot GetSlot(this GameObject gameObject)
{
return gameObject?.GetComponent<Slot>();
}

public static void FillSlotList(this Fuel fuel, string name, IList<Slot> slotlist)
{
IEnumerable<PropertyInfo> properties = typeof(Fuel).GetProperties(BindingFlags.Public | BindingFlags.Instance);

foreach (PropertyInfo item in properties.Where(x => x != default && x.Name.StartsWith(name, StringComparison.InvariantCultureIgnoreCase) && x.GetCustomAttributes<HeaderAttribute>().Any()))
{
slotlist.Add(((GameObject)item.GetValue(fuel)).GetSlot());
}
}
}
Buddy
Buddy8mo ago
No, absolutely not! This is Unity, not normal C# Just use an array if the size never changes (at runtime) and access by index. Simple as that. 1 . So for Unity, you cannot initialize a new instance of objects inheriting from UnityEngine.Object. 2. Unity does not like Properties as they are not serialized by the editor (by default), you can however do [field: SerializableField] but it isn't exactly worth the trouble. 3. Reflection is not recommended in Unity because it will cause a permanent overhead.
Want results from more Discord servers?
Add your server