0

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.

3
  • 1
    C# is not C++. Did you think about bitwise enums? Commented Dec 15, 2022 at 5:29
  • 2
    "as arrays don’t hold references to variables" - says who? Commented Dec 15, 2022 at 5:36
  • 5
    There's zero justification for pointers. You could replace all that obvious c-style code with a few lines of OO'ing. Commented Dec 15, 2022 at 5:47

1 Answer 1

3

Don't, this is not what pointers are for.

What you want to use is a class. You should wrap your variables inside a class, and this should also contain the associated logic:

public class Tick{
    private bool canDoX= true;
    private int tick = 0;
    public event EventHandler CycleEnd;
    // define all the constants
    public void Update(){
        if (!canDoX && tick >= DEFAULT_CYCLE_END_VALUE)
        {
            tick = DEFAULT_CYCLE_START_VALUE;
            canDoX = true;
            CycleEnd?.Invoke(this, EventArgs.Empty);
        }
        else if (!canDoX)
        {
            tick ++;
        }
    }
}

This lets you create a collection of ticks, and apply the logic to each of them. If needed you can use events, delegates or inheritance to customize the behavior if needed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.