Home > Back-end >  How to generate an array of pointers in C#?
How to generate an array of pointers in C#?

Time:12-15

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.

CodePudding user response:

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.

CodePudding user response:

To use pointers in C# you need an unsafe scope

void example() {

    int*[] tick_safe = new int*[4]; // shows an error

    unsafe {
        int*[] tick_unsafe = new int*[4]; // ok
    }
}

example();

In visual studio (2022, .NET 7) you have to manually allow unsafe code for your project:

  • right click your project > properties > build > general > allow unsafe code

And if you need an alternative to pointers, you could use an array of arrays

int[][] ticks = new int[4][];
  • Related