LogoLogo
  • Get Started
  • Getting Started
    • Initial Setup
    • Create Your First Game
    • API Reference
    • Build Your First World Map
    • Multiplayer Testing
    • Use Templates & Examples
    • Styling & Assets
      • Modeling Guidelines
      • Texturing Guidelines
      • Default Assets
  • Build Faster With AI Tools
  • SDK Guides
    • Assets
    • Audio & SFX
      • Audio Manager
    • Blocks & Chunks
      • Block Types
      • Block Type Registry
      • Chunks
      • Chunk Lattice
    • Camera
    • Chat & Commands
    • Debugging
    • Entities
      • Animations
      • Block Entities
      • Colliders & Hitbox
      • Child Entities
      • Entity Controllers
        • Base Entity Controller
        • Pathfinding Entity Controller
        • DefaultPlayer Entity Controller
        • Simple Entity Controller
      • Entity Manager
      • Model Entities
      • Movement & Pathfinding
      • Player Controlled Entities
    • Events
    • Input & Controls
    • Lighting
      • Ambient Light
      • Light Manager
      • Point Lights
      • Spot Lights
      • Sun Light (Directional)
    • Mobile
    • Persisted Data
    • Players
      • Player Manager
      • Persisted Player Data
    • Plugins
    • Physics
      • Colliders
      • Collision Groups
      • Debugging
      • Gravity
      • Raycasts
      • Rigid Bodies
    • User Interface
      • Overlay UI
      • Scene UIs
      • Scene UI Manager
    • Worlds
      • Map Data Format
  • Helpful Resources
    • HYTOPIA Architecture & Platform Overview
    • Useful Third-Party Tools
Powered by GitBook
On this page
  • Setting And Removing Blocks
  • Limitations Of Blocks
  • Next Steps
Export as PDF
  1. SDK Guides

Blocks & Chunks

PreviousAudio ManagerNextBlock Types

Last updated 4 months ago

In HYTOPIA, a world's terrain is made up of blocks. A block is a 1x1x1 sized cube or any subdivided shape that can fit within a 1x1x1 cube.

These blocks are then organized and arranged in a structure called a chunk, which is made up by 16x16x16 blocks (4,096).

Blocks also have textures, which are a single image applied to all faces for the block, or alternatively can be a texture image for each face of the block.

Manipulating blocks and chunks is key to creating games that have dynamic terrain, or terrain that can be constructed / destructed by players (Think similar to breaking/placing blocks in Minecraft).

The BlockType, BlockTypeRegistry, Chunk and ChunkLattice classes are all key to managing and manipulating the terrain blocks of a world.

Let's explore some very common uses of blocks, block types and chunks.

Setting And Removing Blocks

Here's a quick example showcasing setting and removing blocks. We'll set or remove a block every 1 second based on if a block is already set or not.

// You could place this code in your game setup function, or any callback, etc.
// There's no SDK opinionation on where or how you set/remove blocks.

// Create and register a new generic block type for gravel.
// Note: When you use world.loadMap, a number of block types will
// be preloaded. You can view them by console.log(world.blockTypeRegistry.getAllBlockTypes());
// Or look at the "blockTypes" property in your assets/map.json
const gravelBlockType = world.blockTypeRegistry.registerGenericBlockType({
  id: 1, // must be between 1 and 255, cannot be 0, 0 is reserved for air or "no block".
  textureUri: 'blocks/gravel.png',
  name: 'Gravel',
});

setInterval(() => { // Every second, if a block is set, remove it. If not, set it.
  const coordinate = { x: 0, y: 1, z: 0 }; 
  const hasBlock = world.chunkLattice.hasBlock(coordinate);
  const blockTypeId !hasBlock ? gravelBlockType.id : 0; // 0 = no block, which removes the block
  
  world.chunkLattice.setBlock(blockTypeId);
}, 1000)

Limitations Of Blocks

Within HYTOPIA, blocks are fixed cubes that cannot be translated or rotated. Think of them like the ground, walls, etc that are unmovable.

In some cases, you may want blocks that can interact with the physical world, be pushed around, perhaps a block that is a moving platform, etc. For this, you'll want to use Block Entities.

Next Steps

We strongly recommend you check out the following guides to get a better idea of different things you can do with block types, a block type registry, chunks and a chunk lattice.

Note: With HYTOPIA, you must register a new block type with the world.blockTypeRegistry before it can be set as a block in the world. You can.

learn about the BlockTypeRegistry here

Block Types

Learn more about block types.

Block Type Registry

Learn more about the Block Type Registry

Chunks

Learn more about Chunks

Chunk Lattice

Learn more about the Chunk Lattice.

A HYTOPIA world, all worlds are made of blocks.