Accessing Inherited variable before it instanciation
Hello all ! Sorry if my english is bad, i'm not a native :)
I have a class Enemy.cs, then each type of enemy got his own script which derives from Enemy.cs, so I can override functions like Shot(), Move(), etc. I got a boolean "isBig", because for spawning purpose, some enemies have twice the size of others. With parameters in a function that is not shown here to simplify, I can spawn different enemies in the same time but I can't have more than 30 enemies that pop in the same time, so I need to calculate before instantiate them if my parameters are right. You have more explanations on the code annotations.
So if my conception is good, my question is : How can I ask the isBig variable value of the derived class of Enemy.cs before even instantiate them ? Is there a way to store data that came from a parent class and is override before the Game is started ? I don't know if I am clear so don't hesitate to ask question.
Here are my scripts : The parent class Enemy.cs, where i wrote the base mechanics for all enemies and avoid duplicate code :
public class Enemy : MonoBehaviour
{
protected float _speed = 4.0f;
protected float _fireRate = 2.0f;
protected int _health = 1;
protected int _damage = 5;
protected int _score = 10;
protected bool _isBig;
protected Player _player;
protected virtual void Start()
{
InitializeEnemy();
}
void InitializeEnemy()
{
_player = GameObject.Find("Player").GetComponent<Player>();
if (_player == null)
{
Debug.LogError("Player is NULL");
}
}
public bool isBig()
{
return _isBig;
}
[...]
}
SpawnManager where I want to have access to the "isBig" boolean of my child class :
public class SpawnManager : MonoBehaviour
{
[SerializeField]
private Enemy[] _enemiesPrefab;
[SerializeField]
private Transform[] _spawnPositions;
void Start()
{
// Exemple where I have FALSE instead of TRUE, because my ennemy is not instantiated so it takes the ISBIG variable from the parent class, and I want the isBig from the enemy that I indicate. Of course I don't know in advance if it will be a big enemy or not (and so take the right script type directly) but this is to demonstrate that even if i know that, it will not work.
Big_Enemy bigEnemyScript = _enemiesPrefab[1].GetComponent<Big_Enemy>();
Debug.Log(bigEnemyScript.isBig());
//Here is the real purpose of this question, I pass in parameters that I want instantiate _enemiesPrefab[0] 25 times and _enemiesPrefab[1] 3 times. Since index 0 is an enemy which count for 1 size, and index 1 is an enemy that is 2 size ("a big enemy"), result will be 25x1 + 3x2 = 31. Instead of this, I got 28 because of 25x1 + 3x1 cause the isBig of the big enemy is take from his parent and not from him...
Debug.Log(CheckTotalEnemies(0, 25, 1, 3));
}
private bool CheckTotalEnemies(int firstEnemyType, int firstEnemyNumber, int secondEnemyType = -1, int secondEnemyNumber = 0, int thirdEnemyType = -1, int thirdEnemyNumber = 0)
{
int total = 0;
// I multiply by two if it's a big enemy then I add to the total, or I directly add to the total if it's a 1 size enemy.
total = _enemiesPrefab[firstEnemyType].isBig() ? total += firstEnemyNumber * 2 : total += firstEnemyNumber;
// Add a second, or a third enemy type is optional
if (secondEnemyType != -1)
{
total = _enemiesPrefab[secondEnemyType].isBig() ? total += secondEnemyNumber * 2 : total += secondEnemyNumber;
if (thirdEnemyType != -1)
total = _enemiesPrefab[thirdEnemyType].isBig() ? total += thirdEnemyNumber * 2 : total += thirdEnemyNumber;
}
// I check if the total doesn't exceed the maximum enemy size which is 30
return total <= 30 ? true : false;
}
}
And this the script of a big enemy which inherit from Enemy.cs :
public class Big_Enemy : Enemy
{
protected override void Start()
{
base.Start();
InitializeDefaultValue();
}
void InitializeDefaultValue()
{
_speed = 1.0f;
_fireRate = 10f;
_health = 1;
_damage = 20;
_score = 10;
_isBig = true;
}
}
The script of a simple enemy is the same without the IsBig line.
Thank you very much for any help ! :)
Answer by Kazuma17 · Apr 12, 2020 at 06:40 AM
Up please :)
Please, any help is good to take ! :)
To resume my problem in a few lines : I got derived class (polymorphism) that change a parent value variable at instanciation. The problem is that I want to create a spawning editor, and I need to know before any instanciation the value of this variable in the child class. Since this value in the child class is changed in it instanciation, to my editor it value is always the value of the parent class. Is there a way that without instanciation, to attribute an inherit value to a derived class ?
Thank you !