Advertisement

Assigning parameters of a struct

Started by April 08, 2014 11:30 PM
0 comments, last by Starnick 10 years, 9 months ago

This is a pretty simple question, but I havent used c# in a while so I'm a bit rusty.


 struct Voxel
        {
            Vector3 Position;
            enum VoxelType
            {
                air = 0,
                grass = 1
            };
        }

This is how I'm going to create the voxel data structure.


for (int x = 0; x < CHUNKSIZE; x++)
            {
                for (int y = 0; y < CHUNKSIZE; y++)
                {
                    for (int z = 0; z < CHUNKSIZE; z++)
                    {
                        ChunkArray[x, y, z] = new Voxel();
                        
                    }
                }
            }

I populate my ChunkArray with voxel structures. My question is, how do I set the Voxel Position variable using the x, y, and z variables?


Voxel[,,] ChunkArray = new Voxel[32, 32, 32];

Edit:

I think I solved my problem...


                        ChunkArray[x, y, z] = new Voxel
                        {
                            Position = new Vector3(x, y, z),
                        };

If you see a post from me, you can safely assume its C# and XNA :)

FYI,

The access level on that struct member is private by default, that sounds like what was tripping you up? Also, the access modifier on the struct type itself is going to be either internal if it's not nested, or private if it is.

You can define the member as public, which you can just assign at will. Or keep it private and create a public getter/setter. Also, create a constructor in the Voxel type that takes in a Vector3. In this particular case you're using an object initializer.


public struct Voxel
{
     public Vector3 Position;

     public Voxel(Vector3 pos)
     {
          Position = pos;
     } 
}

Voxel v;
v.Position = new Vector3(x,y,z);

Voxel v2 = new Voxel(new Vector3(x,y,z));

Voxel v3 = new Voxel();
v3.Position = new Vector3(x,y,z);

This topic is closed to new replies.

Advertisement