LogoLogo
  • Introduction
  • sdks
    • Feature Comparison
    • Unity
    • Godot
    • Defold
    • LÖVE
    • Unreal
  • Quick Start
    • Unity
    • Godot
    • Defold
    • LÖVE
    • Unreal
  • Server Side
    • Entities
    • Chunks
    • Dimensions
    • Events
    • Logging
    • Lua Environment
    • Git Primer
    • Physics
  • HTTP API
    • Authentication
    • Player API
  • Api Reference
    • Entity API
      • Create
      • Message
    • Client API
      • Message
    • Dimension API
      • Create
      • Delete
      • List
    • Events API
      • on_player_join
      • on_player_leave
    • Table API
      • Append
      • Remove
      • Join
    • Util API
      • Time
      • TimeMillis
    • HTTP Request API
      • Get
      • Post
      • Put
      • Delete
    • Physics API
      • NewBody
      • NewBoxShape
      • NewSphereShape
Powered by GitBook
On this page
  1. Api Reference
  2. Entity API

Create

api.entity.Create(type, x, y, z, data)

Creates a new entity.

This function will automatically call the init function for the specified entity type. It fails if there is no entity type that matches the provided type.

No more than 1024 new entities can be created in a single update tick.

Parameters:

Name
Type
Description

type

string

Defines the type of entity to spawn.

x

float

The position to spawn the entity, in terms of the x axis.

y

float

The position to spawn the entity, in terms of the y axis.

z

float

The position to spawn the entity, in terms of the z axis.

data

table

Custom lua table which can be used to store arbitrary data about the created entity.

Returns:

Type
Description

Entity

The entity which has been created, according to the parameters.

Example:

Create a new cat entity with custom data, whenever a chunk is loaded or reloaded.

-- init.lua
function init()
    local cat_x = chunk.X * chunk.Size
    local cat_y = chunk.Y * chunk.Size   
    
    local name_starts = {"Snow", "Fluff", "Fuzz"}
    local name_ends = {"ball", "ikins", "y"}

    local cat_name_start = name_starts[math.abs(chunk.X)%3 +1]
    local cat_name_end = name_ends[math.abs(chunk.Y)%3 +1]

    local cat_data = {
        name = cat_name_start..cat_name_end, 
        health = math.random(10)
    }
    
    local cat = api.entity.Create("cat", cat_x , cat_y, 1, cat_data)
       
    if cat.Data.health > 8 then
        print("My name is "..cat.Data.name.." and I am a strong cat!")
    end
end

-- Whenever a chunk is loaded, create a cat in the bottom left corner of the chunk.
-- The cat has two custom fields: 'name' and 'health'.
-- Its 'name' is generated based on this chunk's XY coordinates.
-- Its 'health' is a random value between 1 and 10.

-- Prints (subject to randomness):
-- My name is Snowball and I am a strong cat!
-- My name is Fluffy and I am a strong cat!
-- My name is Fuzzball and I am a strong cat!
-- My name is Fuzzy and I am a strong cat!
-- My name is Fluffikins and I am a strong cat!

PreviousEntity APINextMessage

Last updated 8 hours ago