Wind Turbines

by OwnlyMe

Animated wind turbines that turn into wind direction

Content
4 days ago
0.17 - 2.0
247
Power

g Correct entity name? Need to make them indestructible.

4 years ago
(updated 4 years ago)

Hi,

thank you for this great mod, I totally love these turbines! They look really amazing and they're a great addition to the other power solutions.

I have just one problem - I'm building the turbines next to my rail network and I've made all my other rail structures indestructible so biters don't touch them. So I'm trying to make these turbines indestructible as well. But I fail to do so.

I'm using the following script in control.lua:

script.on_event(defines.events.on_built_entity, function(event)
if event.created_entity.name == "ENTITY-NAME" then
event.created_entity.destructible = false
end
end)

It works for all other entities I've needed but I guess I'm using an incorrent entity-name for the turbines. I've tried all the following:

ownly_wind_turbine_mk1
ownly_wind_turbine_mk2
ownly_wind_turbine_mk3
ownly_wind_turbine_mk
ownly_wind_turbine_mk"..level.."_"
ownly_wind_turbine_build_mk1
ownly_wind_turbine_build_mk2
ownly_wind_turbine_build_mk3
ownly_wind_turbine_collision_box_mk1
ownly_wind_turbine_collision_box_mk2
ownly_wind_turbine_collision_box_mk3

I went through all the codes and I couldn't find anything that works.

If you could help me out I would really appreciate it.

Thank you!

4 years ago

the entity gets replaced on build.. i think you need to change the code of the mod to make it work or search 1 tick after the construction for a wind turbine on that position..
this would work:

turbine_names = {
    ["ownly_wind_turbine_build_mk1"] = 1,
    ["ownly_wind_turbine_build_mk2"] = 2,
    ["ownly_wind_turbine_build_mk3"] = 3,
    ["ownly_wind_turbine_mk1"] = 1,
    ["ownly_wind_turbine_mk2"] = 2,
    ["ownly_wind_turbine_mk3"] = 3,
}

script.on_event({defines.events.on_built_entity,defines.events.on_robot_built_entity,defines.events.script_raised_revive}, function(event)
    local entity = event.created_entity or event.entity
    if turbine_names[entity.name] then
        if not global.on_tick[event.tick+1] then
            global.on_tick[event.tick+1] = {}
        end
        table.insert(global.on_tick[event.tick+1], {
            func = function(vars)
                local find = vars.surface.find_entities_filtered{name = {"ownly_wind_turbine_mk1","ownly_wind_turbine_mk2","ownly_wind_turbine_mk3"}, position = vars.position}
                for _, ent in pairs(find) do
                    ent.destructible = false
                end
            end,
            vars = {position = entity.position, surface = entity.surface}
        })
    end

end)
script.on_event(defines.events.on_tick, function(event)
    if not global.on_tick then global.on_tick = {} end
    if global.on_tick[event.tick] then
        for _, tbl in pairs(global.on_tick[event.tick]) do
            tbl.func(tbl.vars)
        end
        global.on_tick[event.tick] = nil
    end
end)
4 years ago
(updated 4 years ago)

Thank you so much for the quick reply!

I copied your code into your mod's control.lua,

 
after:

WIND_SPEED_MULT = 22

 

and before:

script.on_init(function()
global.turbines = {}
global.version = 4

end)

 

but unfortunately it doesn't work. After destruction they don't get rebuilt.

4 years ago
(updated 4 years ago)

no that code was meant to be used in your own script...
of course you might need to merge the events so they don't get overwritten
i tried it ingame with the console by just putting a /c in front of the code

4 years ago

Oh! I see. My coding skills only go so far, sorry about that. :)

Anyway, you're right, it works! I've put the code into another mod's control.lua so I don't have to put it into /c every time and it's exactly what I needed.

Thanks a bunch! Really appreciate your help.

4 years ago

you're welcome

New response