Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Assigning parameters of a struct


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 burnt_casadilla   Members   -  Reputation: 443

Like
1Likes
Like

Posted 08 April 2014 - 05:30 PM

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, 08 April 2014 - 06:11 PM.

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


Sponsor:

#2 Starnick   Members   -  Reputation: 1250

Like
0Likes
Like

Posted 09 April 2014 - 07:46 AM

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





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS