I don’t have (and so haven’t done) this course, and I’m going by what I see on GitLab.
It sounds like you’ve already found a solution to the main problem: make a script to store the full array of enemies and reference that wherever you need it.
Something like this:
public class EnemyDatabase
{
public EnemyInfo[] Enemies; // Put all your enemies in here
}
Then in EnemyManager
create a Start method in which to get the data:
private void Start()
{
allEnemies = EnemyDatabase.Enemies;
}
Everything above should work without problems. The following is messier, so make sure you back up your scripts before trying to implement it.
The issue from what I can tell by looking at the code is with the Encounter
class included in the same file with EncounterSystem
… I don’t know what this looks like in Unity so I’m not sure if you could take the required EnemyInfo
directly from the proposed EnemyDatabase
. Possibly you could add an index value to it instead of EnemyInfo
:
[System.Serializable]
public class Encounter
{
public int EnemyIndex;
public int LevelMin;
public int LevelMax;
}
Then use that index in EnemyManager
:
public void GenerateEnemiesByEncounter(Encounter[] encounters, int maxNumEnemies)
{
currentEnemies.Clear();
int numEnemies = Random.Range(1, maxNumEnemies + 1);
for (int i = 0; i < numEnemies; i++)
{
Encounter tempEncounter = encounters[Random.Range(0, encounters.Length)];
int level = Random.Range(tempEncounter.LevelMin, tempEncounter.LevelMax + 1);
GenerateEnemyByIndex(tempEncounter.EnemyIndex, level); // Using an int here not a string
}
}
private void GenerateEnemyByIndex(int enemyIndex, int level) // Taking in (int, int) instead of (string, int)
{
// By using a consistent index it should be possible to skip the for loop
Enemy newEnemy = new Enemy();
newEnemy.EnemyName = allEnemies[enemyIndex].EnemyName;
newEnemy.Level = level;
float levelModifier = (LEVEL_MODIFIER * newEnemy.Level);
newEnemy.MaxHealth = Mathf.RoundToInt(allEnemies[enemyIndex].BaseHealth + (allEnemies[enemyIndex].BaseHealth * levelModifier));
newEnemy.CurrHealth = newEnemy.MaxHealth;
newEnemy.Strength = Mathf.RoundToInt(allEnemies[enemyIndex].BaseStr + (allEnemies[enemyIndex].BaseStr * levelModifier));
newEnemy.Initiative = Mathf.RoundToInt(allEnemies[enemyIndex].BaseInitiative + (allEnemies[enemyIndex].BaseInitiative * levelModifier));
newEnemy.EnemyVisualPrefab = allEnemies[enemyIndex].EnemyVisualPrefab;
currentEnemies.Add(newEnemy);
}
Again I want to stress that I’m not familiar with the course and the project so I might be missing important information. Even if this does work for the moment, making these changes could easily cause problems following later sections of the course if you haven’t completed it yet. (I think it’s still in early access at this time.) Plus, this idea to use an index number is potentially going to make it more difficult to know which enemies you’re referencing when setting up your scenes. It could definitely be improved, maybe using an enum or something like that.