> For the complete documentation index, see [llms.txt](https://zenith391.gitbook.io/exdilin/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://zenith391.gitbook.io/exdilin/lua-mods/adding-blocks.md).

# Adding Blocks

Now that you have created a Lua mod in [Introduction](/exdilin/lua-mods/introduction.md), let's continue by adding blocks.

## The Exdilin API

The Exdilin API is a global variable that allows to interact with Blocksworld. It has a lot of functions which are all referenced in the Lua API Reference. But in this tutorial, we'll be interested in `exdilin.new_block`.

The `exdilin.new_block` function takes a table (see [Lua - Tables](https://www.tutorialspoint.com/lua/lua_tables.htm)), today we will only need to understand two fields in the table: `id` and `modelName`.

The first is straightforward, id must be set to the block's identifier/name, model name is the name (inside Blocksworld's Resources!) of the 3D model of the block, this defaults to the block's name, but we don't have any model to put for our block, so we will use the cube's model. The cube's model name is very simple, it's simply `Cube`, with all that setup let's create a block named Test.

```lua
-- ... previous code

function pre_init()
    local block = exdilin.new_block({
        id = "Test",
        modelName = "Cube"
    })
end
```

However the block does not register itself (read: does not exist yet), so we will have to register it.

```lua
function pre_init()
    local block = exdilin.new_block({
        id = "Test",
        modelName = "Cube"
    })
    block.register()
end
```

Cool, the block is registered, however we cannot place it from the Build Panel :confused:&#x20;

To do that we need to add a block item, it is not really specific to block and can be applied to script functions, textures, and others.

Manually creating a block item is exhausting and takes a lot of lines of code, hopefully the block we just saved in a variable provides a way to do just that, the `default_block_item` function.

This function is simple, it takes two arguments: the first is the icon name and the second is the rarity (rarity define the border around it, like does it have any, are they golden, etc.)

The icon of the default cube block is `Yellow/Cube`, and the rarity will be `common` (note: you can choose any rarity that is present in [Rarity Levels](/exdilin/glosarry/rarity-levels.md))

Like the block, we will also have to register that new block item, now let's write the code:

```lua
function pre_init()
    local block = exdilin.new_block({
        id = "Test",
        modelName = "Cube"
    })
    local blockItem = block.default_block_item("Yellow/Cube", "common")
    blockItem.register()
    block.register()
end
```

{% hint style="info" %}
**As you should know if you learnt Lua**, the `block` part in `block.default_block_item` refers to the variable we did just above (`local block =` ...), so if you name your variable something else like `idontknowlua`, the function should be `idontknowlua.default_block_item.`
{% endhint %}

Now launch Blocksworld, scroll down the Blocks tab and tada! A new block you just made has appeared!

You can also fiddle with the rarity and the icon, try the `Yellow/Cylinder` icon, or the `rare` rarity.
