I have this piece of code that’s really huge, and I feel that I could shorten it down significantly if I used pointers. I don’t really see any other alternative, as arrays don’t hold references to variables.
What I’d like to do is to create a pointer array. I’d then place all of my “tick” variables inside of it, then I could iterate over them to complete the actions shown below.
[SerializeField] private bool canEatFood = true;
[SerializeField] private bool canTakeBath = true;
[SerializeField] private bool canPlay = true;
[SerializeField] private bool isScared = false;
[SerializeField] private bool beenPet = false;
private int canEatTick = 0;
private int canBatheTick = 0;
private int canPlayTick = 0;
private int beenPetTick = 0;
private int pettedCount = 0;
private const int PETS_TO_KILL_PER_CYCLE = 50;
private const int PET_HAPPINESS_VALUE = 1;
private const int DEFAULT_STARTING_VALUE = 10;
private const int DEFAULT_MAX_VALUE = 100;
private const int DEFAULT_MIN_VALUE = 0;
private const int DEFAULT_CYCLE_START_VALUE = 0;
private const int DEFAULT_CYCLE_END_VALUE = 500;
public void FixedUpdate()
{
CheckTicks();
}
private void CheckTicks()
{
if (!canEatFood && canEatTick >= DEFAULT_CYCLE_END_VALUE)
{
canEatTick = DEFAULT_CYCLE_START_VALUE;
canEatFood = true;
}
else if (!canEatFood)
{
canEatTick++;
}
if (!canTakeBath && canBatheTick >= DEFAULT_CYCLE_END_VALUE)
{
canBatheTick = DEFAULT_CYCLE_START_VALUE;
canTakeBath = true;
}
else if (!canTakeBath)
{
canBatheTick++;
}
if (!canPlay && canPlayTick >= DEFAULT_CYCLE_END_VALUE)
{
canPlayTick = DEFAULT_CYCLE_START_VALUE;
canPlay = true;
}
else if (!canPlay)
{
canPlayTick++;
}
if (beenPet && beenPetTick >= DEFAULT_CYCLE_END_VALUE)
{
beenPetTick = DEFAULT_CYCLE_START_VALUE;
beenPet = false;
CheckIfPetsShouldKill();
}
else if (beenPet)
{
beenPetTick++;
}
}
I tried
int*[] ticks = new int*[4];
which resulted in an error.
I then tried
ref int[] ticks = new ref int[4];
which also resulted in an error.