Reserve collision_mask layer

Place to post guides, observations, things related to modding that are not mods themselves.
Post Reply
Jaren Taq
Burner Inserter
Burner Inserter
Posts: 7
Joined: Sat Nov 28, 2020 8:26 am
Contact:

Reserve collision_mask layer

Post by Jaren Taq »

If you want to reserve a collision_mask layer dynamically, just copy a prototype and allocate an unused layer to its collision mask. During runtime, the layer can be retrieved by using the ingame prototype information system.

I attached some example code:

data.lua

Code: Select all

local util = require("util")
local collision_mask_util = require("collision-mask-util")

local function reserve_collision_layer(lookup_name)
    local prototype = util.table.deepcopy(data.raw["simple-entity-with-force"]["simple-entity-with-force"]
    prototype.name = 
    local layer = collision_mask_util.get_first_unused_layer()
    if not layer then
        error("no free collision layer found")
    end
    prototype.collision_mask = {layer}
    data:extend{prototype}
    return layer
end

local your_collision_layer = reserve_collision_layer("modname-collision-layer-identifier")
During runtime, the layer name can be fetched with the following function:
control.lua

Code: Select all

local function get_collision_layer(lookup_name)
    for l, _ in pairs(game.entity_prototypes[lookup_name].collision_mask) do
        return l
    end
end

local your_collision_layer = get_collision_layer("modname-collision-layer-identifier")

Post Reply

Return to “Modding discussion”