Block Type Registry

Most games will have many block types, and keeping track of those block types is important for your own game systems and internal HYTOPIA systems.

This is where the BlockTypeRegistry comes into play. Allowing us to register, unregister, retrieve and iterate known BlockType instances of our world.

Accessing A BlockTypeRegistry

The BlockTypeRegistry is used as a singleton and automatically created for a given world instance.

You can access the block type registry for a world like this:

world.blockTypeRegistry

Registering Block Types

Before a block type can be set as a block at a coordinate in the world through world.chunkLattice.setBlock() or chunk.setBlock(), the block type must have been previously registered with the world.blockTypeRegistry through world.blockTypeRegistry.registerBlockType() or world.blockTypeRegistry.registerGenericBlockType().

In this example, let's dive into how to register a block type, as well as the difference between registerBlockType() and registerGenericBlockType()

// This usage of world.blockTypeRegistry.registerGenericBlockType()
const dirtBlock = world.blockTypeRegistry.registerGenericBlockType({
  id: 1,
  textureUri: 'textures/dirt.png',
  name: 'Dirt'
});

// Is equivalent too this
const dirtBlock = new BlockType({
  id: 1,
  textureUri: 'textures/dirt.png',
  name: 'Dirt'
});

world.blockTypeRegistry.registerBlockType(dirtBlock);

// Using either is a matter of preference and 
// convenience, internally they result in
// the same outcome.

Other BlockTypeRegistry Usage

The BlockTypeRegistry exposes ways to iterate known block types, or get registered block types by ID. Here's an example:

// Returns an array of all block types
// for the world
world.blockTypeRegistry.getAllBlockTypes();

// Returns the block type for the provided ID,
// or throws if the provide ID is not registered.
const id = 15
world.blockTypeRegistry.getBlockType(id);

Diving Deeper

The BlockTypeRegistry class is constantly evolving. You can find the latest BlockTypeRegistry API Reference here.

If there are features that we don't currently support for BlockTypeRegistry that you'd like to see added to the HYTOPIA SDK, you can submit a feature request here.

Last updated