4
\$\begingroup\$

I am trying to make a voxel game.

Voxel games made by blocks and each block has it's own information such as durability, step sounds, strength, ...

How do i store these information inside of blocks? or is it true to save their information inside of themselves.

Do i store them on all blocks instances or static properties?

public class Block
{

    public virtual float durability
    {
        get
        {
            return 1f;
        }
    }

    public virtual float strength
    {
        get
        {
            return 1f;
        }
    }

    public virtual StepSound stepSound
    {
        get
        {
            return new StepSound ( "stone.ogg" );
        }
    }

}

Then create a class by inheriting Block class:

public class BlockDirt : Block
{

    public override float durability
    {
        get
        {
            return 0.5f;
        }
    }

    public override float strength
    {
        get
        {
            return 0.5f;
        }
    }

    public override StepSound stepSound
    {
        get
        {
            return new StepSound ( "dirt.ogg" );
        }
    }

}

Now i have all the above properties inside of any BlockDirt instances.

But it takes to much memory. just think i have 100 BlockDirt.

Or Do i create a base class:

public class Block
{

}

Then inherit it for creating blocks:

public class BlockDirt : Block
{

    public static StepSound stepSound = new StepSound ( "dirt.ogg" );
    public static float durability = 0.5f;
    public static float strength = 0.5f;

}

And use the BlockDirt static properties for all BlockDirt instances?

I don't know how to get static properties from class instance, but i know i can do it using Reflection but it takes more CPU and memory than instance based data.

What should i do?

Any help is appreciated.

\$\endgroup\$
  • 1
    \$\begingroup\$ If you want to imitate Minecraft, why not have a look at Minecraft's source? Modding will teach you a ton about this sort of thing. \$\endgroup\$ – Draco18s no longer trusts SE Jul 1 '17 at 15:10
  • \$\begingroup\$ Where do i find Minecraft source code? Do you mean Minecraft forge? the source code of the Minecraft Forge is not readable. \$\endgroup\$ – Hasan Bayat Jul 1 '17 at 15:58
  • \$\begingroup\$ Minecraft Forge's sorce is open source on github, but no, what I mean is: download the MDK and install it. After opening the project in Eclipse or IntelliJ, under referenced libraries, will be a read-only version of Minecraft's source code. \$\endgroup\$ – Draco18s no longer trusts SE Jul 1 '17 at 17:27
  • \$\begingroup\$ Is it decompiled version of Minecraft jar files or is the official source files and contains names correctly? Do you mean create mods for minecraft and learn how it works? \$\endgroup\$ – Hasan Bayat Jul 2 '17 at 7:06
  • 1
    \$\begingroup\$ Names are supplied by MCP and is very comprehensive. I haven't run into a automatically generated name in 1.11 yet. And you don't have to create mods, my point is you can look at how Minecraft does the thing you want to do and learn from it. \$\endgroup\$ – Draco18s no longer trusts SE Jul 2 '17 at 15:16
3
\$\begingroup\$

I would recommend you take a look at the flyweight pattern. It allows you to use objects such as tiles or blocks in very large numbers when a simple repeated representation would use an unacceptable amount of memory. The principle is well represented by this picture :

enter image description here

You basically have one object Tree that is instanciated and which defines all shared attributes between trees. You then just have to create position parameters to tell the engine where to display this single object.

Here is the full example applied to your problem if you want to know more.

EDIT : A few concrete examples :

  • Creating Aliens on Unity (C#), comparing memory use. link
  • Counter-Strike example (JAVA), reuse of the player models. link
  • A live explaination by a youtuber (JAVA). link

I put Java examples as Java is pretty similar to C# in its syntax and structure. I'm sure you can find more example online, and perhaps better one.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Thanks, this is a cool design pattern. but it would be better to say how do i implement it in Unity game engine using c#? \$\endgroup\$ – Hasan Bayat Jul 7 '17 at 11:48
  • \$\begingroup\$ The design pattern simply outlines the principle, the implementation is really up to you. I won't give you the full implementation as I don't know your game and simply don't have the time but I'll update my answer to include examples :) \$\endgroup\$ – Shashimee Jul 7 '17 at 11:58
  • \$\begingroup\$ Added, tell me if you need further explaination ! \$\endgroup\$ – Shashimee Jul 7 '17 at 12:20
0
\$\begingroup\$

I don't know how Minecraft did it, but very long ago, I once wrote a Minecraft clone. You could have a look on how I did it.

I am definitely not saying that this is how you should do it, or this is a good way to do it, but it worked. Note that I was probably only 17 back then and had little to no experience with this.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.