Apply parameter to all entities in type

Place to get help with not working mods / modding interface.
Post Reply
User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Apply parameter to all entities in type

Post 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

lyvgbfh
Fast Inserter
Fast Inserter
Posts: 165
Joined: Fri Jul 10, 2020 6:48 pm
Contact:

Re: Apply parameter to all entities in type

Post 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 ()

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Apply parameter to all entities in type

Post 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

lyvgbfh
Fast Inserter
Fast Inserter
Posts: 165
Joined: Fri Jul 10, 2020 6:48 pm
Contact:

Re: Apply parameter to all entities in type

Post 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.

User avatar
DemerNkardaz
Inserter
Inserter
Posts: 26
Joined: Wed Jan 18, 2023 12:32 am
Contact:

Re: Apply parameter to all entities in type

Post 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

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2481
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: Apply parameter to all entities in type

Post 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
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles

Post Reply

Return to “Modding help”