C
C#2mo 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
2spooky2play2mo 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
SpReeD2mo 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
Buddy2mo 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
More Posts
I need help creating a function c#i have a data gird that when the user clicks on a cell it will play that intended audio file like a ✅ No route matches the supplied value. when trying put request to /venture/{id}/portfolio```c# // PUT: api/Venture/{id}/portfolio [HttpPut("{id}/Portfolio")] public async Task<AUnsure what this error meansI'm building an Azure Function on .Net 6 and this return snippet is giving me this error. I am new tHTTP Request Pending Time Unusually HighCan anyone throw out any ideas why my server (local dev) would hang on incoming http requests? I'm uMinimalApi vs Mvc - ValidationOfHeadersI have been playing around with MinimalApi, and was surprised to see that MinimalApi could not handlHow to get rid of nullable warning?What are the usual ways to get rid of these warnings? Do I have to check if everything is null beforyo im tied to make a game and there is problomtile class using System.Collections; using System.Collections.Generic; using TMPro; using UnityEnginNetArchTests Not Detecting Project Referencesanyone here familiar with netarchtests? I am trying to enforce clean architecture, and for some reasTrying to make a "Distributed SQLite" in C# and gRPC/Protobuf - don't know what to do nextHello, for a project that I am required to do, I need to make a simple distributed database system dUnable to get all the azure ad groups details which logged in user is a part of.I'm using Azure AD login for an app to login with Microsoft credentials and get the groups (and thei