Page 1 of 1

Apply parameter to all entities in type

Posted: Fri Jan 27, 2023 2:21 pm
by DemerNkardaz
Trying to change collision mask of any entities of chosen type, but my code works in only half.
If write "or" then it applies only for furnace entities, if "and" then only for storage tanks, if "," then error on launch. I don't know what need to change for good work with all chosen.

Code: Select all

local entity_array = (
	data.raw["furnace"] or
	data.raw["solar-panel"] or
	data.raw["burner-generator"] or
	data.raw["generator"] or
	data.raw["electric-pole"] or
	data.raw["container"] or
	data.raw["boiler"] or
	data.raw["assembling-machine"] or
	data.raw["turret"] or
	data.raw["transport-belt"] or
	data.raw["underground-belt"] or
	data.raw["splitter"] or
	data.raw["inserter"] or
	data.raw["reactor"] or
	data.raw["heat-pipe"] or
	data.raw["pipe"] or
	data.raw["pipe-to-ground"] or
	data.raw["radar"] or
	data.raw["lamp"] or
	data.raw["storage-tank"]
)

for e_name, entity in next, entity_array do entity.collision_mask = {"object-layer", "layer-40", "player-layer"} end

Re: Apply parameter to all entities in type

Posted: Fri Jan 27, 2023 2:29 pm
by lyvgbfh
Using commas in the table declaration is correct, but for `for` loops you will usually be doing it in the format

Code: Select all

for k, v in pairs(table)
where pairs is the missing bit in your code.

You can see a differential here

Edit: Also, data.raw is in the format of

Code: Select all

data.raw[category][name]
Edit 2: Also, tables are defined using {}, not ()

Re: Apply parameter to all entities in type

Posted: Fri Jan 27, 2023 2:34 pm
by DemerNkardaz
lyvgbfh wrote:
Fri Jan 27, 2023 2:29 pm
Using commas in the table declaration is correct, but for `for` loops you will usually be doing it in the format

Code: Select all

for k, v in pairs(table)
where pairs is the missing bit in your code.

You can see a differential here

Edit: Also, data.raw is in the format of

Code: Select all

data.raw[category][name]
I tried it before but
Image

Re: Apply parameter to all entities in type

Posted: Fri Jan 27, 2023 2:35 pm
by lyvgbfh
It's what I get for hasty reading. I edited in a few more notes. There's two loops needed because you're going through the category followed by each entity in the category.

Re: Apply parameter to all entities in type

Posted: Fri Jan 27, 2023 2:50 pm
by DemerNkardaz
lyvgbfh wrote:
Fri Jan 27, 2023 2:35 pm
It's what I get for hasty reading. I edited in a few more notes. There's two loops needed because you're going through the category followed by each entity in the category.
Ye, thank you! Now it works all
Image

Re: Apply parameter to all entities in type

Posted: Fri Jan 27, 2023 5:01 pm
by FuryoftheStars
A couple of suggestions (based on the partially shared code):
  1. Don't hard code the use of layer-40. Use the get_first_unused_layer() function in the \Factorio\data\core\lualib\collision-mask-util.lua file to return a layer and store it to a variable to then use where needed.
  2. Be careful about destructively overwriting collision masks. You can cause conflicts with other mods by doing this. If you simply need to add or remove a layer, then use (from the same file as above) the get_mask() function to retrieve its collision mask, then use add_layer() and remove_layer() functions to modify it the way you want, and finally set it back to the prototype. Example:

    Code: Select all

    local collision_mask_util = require("collision-mask-util")
    [...]
    local function add_collision_layer(prototype, layer)
        local collisions = collision_mask_util.get_mask(prototype)
        collision_mask_util.add_layer(collisions, layer)
        prototype.collision_mask = collisions
    end