Basic question here, but I’m struggling with this little issue. I promise it’s not a very involved problem once you get through my explanation.
I have a FactionType enum that I use for easy drop-down menus and selection and everything. I also have a Faction class (scriptable object) which contains basic information for each faction such as an icon, a list of units associated with that faction, and a factionType. I guess see the code below. As a warning, please excuse any poor coding practices for now (though I’m open to being made aware of them) such as naming convention, public/private variables… I’m literally just tossing this together quickly for functionality right now and I am aware of many of these practices.
[CreateAssetMenu(menuName="Faction")]
public class Faction : ScriptableObject
{
public Sprite icon;
public FactionType type;
public FactionUnitList units;
}
[CreateAssetMenu(menuName = "Units/Faction Unit List")]
public class FactionUnitList : ScriptableObject
{
public List<GameObject> UnitPrefabs { get { return unitPrefabs; } private set { } }
[SerializeField] List<GameObject> unitPrefabs;
}
In a ‘setup panel’ kind of script, I am now attempting to let the player cycle through all of the available factions, and have an ‘icon panel’ display an icon for each of the units found in the FactionUnitList associated with the proper faction. Part of this setup panel script looks like this:
FactionType[] factionTypes;
Faction[] factions;
int numberOfFactions;
public FactionType selectedFactionType;
private Faction selectedFaction;
int selectedFactionIndex = 0;
private void Awake()
{
factionTypes = FactionType.GetValues(typeof(FactionType)) as FactionType[];
numberOfFactions = factionTypes.Length;
selectedFactionType = factionTypes[0];
UpdateFields();
}
public void SelectNextFaction()
{
selectedFactionIndex = (selectedFactionIndex + 1) % numberOfFactions;
selectedFactionType = factionTypes[selectedFactionIndex];
UpdateFields();
}
So you can see that I’m populating the factionTypes array with however many entries are in the Enum, using GetValues(). The player cycles through the factionTypes with a button, and we update the selectedFactionType variable with their current choice. Now, I want to turn that selectedFactionType into the appropriate selectedFaction itself (from the enum, retrieve the correct Scriptable Object)…
Since the Faction class itself has a reference to “it’s FactionType”, is there any convenient way to fetch the Faction that matches the FactionType? The only thing I can really think of is to load up ALL of the Factions, then loop through them and check when Faction.factionType matches the selectedFactionType. But even getting that initial array of all Faction objects seems a bit out of reach for me right now. I’ve considered using Resources.Load to just load the Faction scriptable objects directly, but I know it’s generally not recommended, slow, and I’d have to restructure my asset hierarchy.
Another option might be just to create a ‘dictionary’ class of sorts, which could link a FactionType to a Faction, but then I’ll have added just one more thing to remember/forget each time I add a faction.
Appreciate any responses and anyone who takes the time to read through this. Thanks.