Skip to content

EntityDB

Entity related typessource

When cloning an entity type, remember to save it in the script for as long as you need it. Otherwise the memory will be freed immediately, which eventually leads to a crash when used or overwritten by other stuff:

-- Create a special fast snake type with weird animation
special_snake = EntityDB:new(ENT_TYPE.MONS_SNAKE)
special_snake.max_speed = 1
special_snake.acceleration = 2
special_snake.animations[2].num_tiles = 1
set_post_entity_spawn(function(snake)
-- 50% chance to make snakes special
if prng:random_chance(2, PRNG_CLASS.PROCEDURAL_SPAWNS) then
-- Assign custom type
snake.type = special_snake
-- This is only really needed if types are changed during the level
snake.current_animation = special_snake.animations[2]
end
end, SPAWN_TYPE.ANY, MASK.MONSTER, ENT_TYPE.MONS_SNAKE)

You can also use Entity.user_data to store the custom type:

-- Custom player who is buffed a bit every level
set_callback(function()
-- Doing this to include HH
for i,v in ipairs(get_entities_by_mask(MASK.PLAYER)) do
local player = get_entity(v)
-- Create new custom type on the first level, based on the original type
if not player.user_data then
player.user_data = {}
player.user_data.type = EntityDB:new(player.type.id)
end
-- Set the player entity type to the custom type every level
player.type = player.user_data.type
-- Buff the player every subsequent level
if state.level_count > 0 then
player.type.max_speed = player.type.max_speed * 1.1
player.type.acceleration = player.type.acceleration * 1.1
player.type.jump = player.type.jump * 1.1
end
end
end, ON.POST_LEVEL_GENERATION)

Illegal bad example, don't do this:

set_callback(function()
-- Nobody owns the new type and the memory is freed immediately, eventually leading to a crash
players[1].type = EntityDB:new(players[1].type)
players[1].type.max_speed = 2
end, ON.POST_LEVEL_GENERATION)

Used in Entity and get_type
Stores static common data for an ENT_TYPE. You can also clone entity types with the copy constructor to create new custom entities with different common properties. This tool can be helpful when messing with the animations. The default values are also listed in entities.json.

Constructors

function EntityDB.new(p1: EntityDB): EntityDB
p1: EntityDB

Returns EntityDB

function EntityDB.new(p1: integer): EntityDB
p1: integer

Returns EntityDB

Fields

acceleration

number

source

animations

table<integer, Animation>

source

blood_content

integer

source

collision2_mask

integer

MASK, will only call collision2 when colliding with entities that match this mask.

source

collision_mask

integer

MASK used for collision with floors, walls etc.

source

damage

integer

source

default_color

Color

source

default_flags

integer

source

default_hitbox_enabled

boolean

source

default_more_flags

integer

source

default_shape

SHAPE

source

default_special_offsetx

number

source

default_special_offsety

number

source

description

STRINGID

source

draw_depth

integer

source

elasticity

number

source

friction

number

source

glow_alpha

number

source

glow_blue

number

source

glow_green

number

source

glow_red

number

source

height

number

source

hitboxx

number

Half of the width of the hitbox

source

hitboxy

number

Half of the height of the hitbox

source

jump

number

source

leaves_corpse_behind

boolean

source

life

integer

source

max_speed

number

source

offsetx

number

Offset of the hitbox in relation to the entity position

source

offsety

number

Offset of the hitbox in relation to the entity position

source

properties_flags

integer

source

sacrifice_value

integer

Favor for sacrificing alive. Halved when dead (health == 0).

source

search_flags

MASK

MASK

source

sound_killed_by_other

SOUNDID

source

sound_killed_by_player

SOUNDID

source

sprint_factor

number

source

tilex

integer

source

tiley

integer

source

weight

number

source

width

number

source