Array element reference being set to partially incorrect memory address and data

64 views Asked by At

I am at the point in the custom ECS I'm making where I am trying to retrieve components I've stored. The issue is when I try to access the array element as a reference it ends up having a memory address that is half right; and the data I return ends up being nothing I've set (should be 150, ends up returning random garbage data).

correct : 0x000000123456789A

what i get : 0xFFFFFFFF3456789A or 0x000000003456789A (always starts with either eight 0's or F's)

When I debug the reference to the array itself visual studio shows that the memory address is pointing to my original array and the data I set is all there. I am not new to C# or pointers; but I am new to the 'ref' keyword, unsafe code, and any of the nuances it may have.

The actual code for trying to return said component reference is :

public struct Block
{
    private Array[] components;

    unsafe public readonly ref T Get<T>(int entityID)
    {
        ref ComponentType cType = ref ComponentType.GetInfo<T>();

        ref Array array = ref Components[cType.ID];
        T[] cmp = Unsafe.As<T[]>(array);

        return ref components[entityID];
    }
}

I've tried accessing the data in as many ways that I can think of using Unsafe.As casts and even using just raw pointers in a fixed statement; all of which resulted in the same memory address issues as I had above. My current best guess is the references to the arrays are incorrect in a way I don't quite understand, and my index to the element isn't going where I think it's going memory-wise.

0

There are 0 answers