Sign in to follow this  

Assigning parameters of a struct

This topic is 1378 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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),
                        };
Edited by burnt_casadilla

Share this post


Link to post
Share on other sites

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);

Share this post


Link to post
Share on other sites
Sign in to follow this