Page 1 of 1

[Solved] Need help with Layered entities

Posted: Sat Sep 17, 2016 4:39 am
by anarchyinc
Hello all, I just spent the whole day working on an idea I had to combine an electrical pole, lamp, and a roboport zone extender into one entity. So far I've used the wiki and older mods "concrete lamp post" as references and updated the script the best I could to v14.

After several sloppy edits, I can now place my new electrical pole down then the lamp and roboport addons will spawn into place. If I pick up the pole, the addons are removed as intended. Only issue I have left is that my new logistic addon isn't connecting to my existing network. If I place it down directly it will connect to the network but not to the script spawned entities. The script spawned addons WILL connect to each other.

I've obviously done something incorrectly but as I've never played around with complicated on_event scripts, I haven't got a clue what I did wrong. And even less of a clue on where to start looking.

sloppy update of control.lua from factorio v12
control
backbone.lua
backbone

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 4:45 am
by anarchyinc
Looks like I can't do spoiler tags correctly either, I should go to bed.

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 4:53 am
by aubergine18
You missed the name (after the "=") from your spoiler

Code: Select all

[spoiler=This is my code]
 ... your code preferably wrapped in code tags ...
[/spoiler]

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 5:09 am
by Nexela
Also this might be the problem

force= game.forces.neutral


your logistic is looking for a network belonging to the neutral force and not the "player"s force

What you probaly want instead will be force=event.created_entity.force (or force=e.force in your case)

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 5:18 am
by Nexela
Also not related

surface.find_entities_filtered{area = {{X -0.5, Y - 0.5 }, {X + 0.5 , Y +0.5 }}, name= "backbone-light"}

If your looking for something in the same spot you can just use position

surface.find_entites_filtered{position=e.position, name="backbone-light"}

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 5:28 am
by aubergine18
First script - cleaned up and with tweak to force and search position as mentioned by Nexela:

Code: Select all

-- luacheck: globals script defines game

local addon = { 'backbone-light', 'backbone-logistics' }

-- create addons when factory built
local function BuiltEntity(event)
  local entity = event.created_entity

  if entity.name == "factory-backbone" then
    -- spawn addons
    for name = 1, #addon do
      entity.surface.create_entity {
        name     = addon[name],
        position = entity.position,
        force    = entity.force
      }
      .destructible = false
    end--for
  end--if entity.name

end--BuiltEntity


-- remove addons when factory destroyed
local function MinedEntity(event)
  local entity = event.entity

  if entity.name == "factory-backbone" then
    -- remove addons
    for name = 1, #addon do
      local found = entity.surface.find_entities_filtered {
        name = addon[name],
        position = entity.position
      }
      if found[1] then found[1].destroy() end
    end--for
  end--if entity.name

end--MinedEntity

script.on_event(defines.events.on_built_entity, BuiltEntity)
script.on_event(defines.events.on_robot_built_entity, BuiltEntity)
script.on_event(defines.events.on_preplayer_mined_item , MinedEntity)
script.on_event(defines.events.on_entity_died , MinedEntity)
script.on_event(defines.events.on_robot_pre_mined , MinedEntity)

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 5:37 am
by Nexela
aubergine18 wrote:First script - cleaned up and with tweak to force and search position as mentioned by Nexela:
These do almost the exact same thing
local pos = entity.position
local position = { pos.x, pos.y }

entity.position = {x = 0, y = 0}
local pos = {0, 0}

Both are valid positions for anywhere a position is required

Same is true for area (90% sure on this)
area={top_left = {x=0, y=0}, bottom_right = {x=0, y=0}}
area={top_left = {0, 0}, bottom_right = {0, y}}
area={{x=0, y=0}, {x=0, y=0}}
area={{0, 0}, {x=0, y=0}}
area={{0, 0}, {0, 0}}

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 5:39 am
by aubergine18
Nexela wrote: These do almost the exact same thing
local pos = entity.position
local position = { pos.x, pos.y }

entity.position = {x = 0, y = 0}
local pos = {0, 0}

Both are valid positions for anywhere a position is required
Ah yes, forgot about that one. I've been doing lots of stuff with color objects recently and those don't offer the same syntactic sugar as position objects.

EDIT: Updated script to make cleaner

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 5:43 am
by Nexela
aubergine18 wrote:
Nexela wrote: Both are valid positions for anywhere a position is required
Ah yes, forgot about that one. I've been doing lots of stuff with color objects recently and those don't offer the same syntactic sugar as position objects.

I figured that's what it was. color concept should really allow for this :)

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 5:46 am
by aubergine18
Nexela wrote:I figured that's what it was. color concept should really allow for this :)
In my Style mod, it does :) And also hex strings are supported without needing to do `color = color '#F00'`, you can just do `color = '#F00'` 8-)

Re: Need help with Layered entities

Posted: Sat Sep 17, 2016 5:59 am
by anarchyinc
Nexela wrote:Also this might be the problem

force= game.forces.neutral


your logistic is looking for a network belonging to the neutral force and not the "player"s force

What you probaly want instead will be force=event.created_entity.force (or force=e.force in your case)
Thank you! That was exactly it, I could have swore I read somewhere in the forums it had to be set to neutral but that was from a post a few years ago. That control lua is confusing as hell to me. Now that it works I'm afraid to clean my sloppy code.
Image

Re: [Solved] Need help with Layered entities

Posted: Sat Sep 17, 2016 6:05 am
by aubergine18
The reason the Concrete Lamppost mod uses neutral force is so player can't select the addons (they can select the post, but not the lamp). Although I think there's other ways to do this now (player selectable property in prototype, or when creating entity, can't remember which).

Re: [Solved] Need help with Layered entities

Posted: Sat Sep 17, 2016 6:20 am
by anarchyinc
aubergine18 wrote:The reason the Concrete Lamppost mod uses neutral force is so player can't select the addons (they can select the post, but not the lamp). Although I think there's other ways to do this now (player selectable property in prototype, or when creating entity, can't remember which).
Hum, I could still select what ever got placed down last by the script so I just removed the selection_box from the addons and it was good enough to test. Also, the cleaned code you sent leaves the light addon behind when picked up by player and bots. My original sloppy code works exactly as expected with force=e.force. I feel I can move on to other little tweaks and call my sloppy code good enough.

But thank you all very much for the help/advice.

Re: [Solved] Need help with Layered entities

Posted: Sat Sep 17, 2016 11:29 pm
by anarchyinc
Update: I went ahead and added back in the selection boxes so you could connect circuit connections to all 3 entities. I also added that new code that will show you the roboport status the devs recently added. I almost forgot about that one and wasn't going to add it at all. Oh, and I had to add the "not_bluepritable" flag to the attachments for it to work along side selection boxes.

Just wanted to let the forum know in case someone else later tries to do the same thing.
Working example
Working example
factory backbone.png (271.5 KiB) Viewed 6385 times

Re: [Solved] Need help with Layered entities

Posted: Sun Sep 18, 2016 5:39 am
by aubergine18
That looks awesome! Are you posting it to the mod portal?

Re: [Solved] Need help with Layered entities

Posted: Mon Sep 19, 2016 3:56 am
by anarchyinc
aubergine18 wrote:That looks awesome! Are you posting it to the mod portal?
Those graphics don't belong to me so I've got a request in the texture subforum. I used bob's logistic zone extender with a vanilla lamp pasted on top as a place holder. The graphics I made while I wait look pretty bad. I think I'm using too hi-res, my new things stick out too much. I like my new roboport though.
current test

Re: [Solved] Need help with Layered entities

Posted: Mon Sep 27, 2021 3:32 pm
by demongo
is it on the portal or is it private, i really wanna check it out(even if its not updated)

Re: [Solved] Need help with Layered entities

Posted: Mon Sep 27, 2021 3:36 pm
by mrvn
And if the entities are not blueprintable do the wire connections survive blueprints?

Re: [Solved] Need help with Layered entities

Posted: Mon Sep 27, 2021 5:32 pm
by Xorimuth
demongo wrote: Mon Sep 27, 2021 3:32 pm is it on the portal or is it private, i really wanna check it out(even if its not updated)
Looks like it is here: https://mods.factorio.com/mod/factory_backbone

(FYI for anyone else reading: this thread is from 2016!)

Re: [Solved] Need help with Layered entities

Posted: Tue Sep 28, 2021 3:19 pm
by mrvn
Yeah, kind of dated. Have you tried if the mod still works when you bump the version in the info.json to current?