Enable/unhide technology with an event?

Place to get help with not working mods / modding interface.
Post Reply
Doctor_Willis
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Sep 26, 2023 11:45 am
Contact:

Enable/unhide technology with an event?

Post by Doctor_Willis »

I'm hoping this is a dumb question, but can you change the enabled/hidden property of a technology with an event?
I thought it would be data.raw.technology["hidden-technology"].hidden = false (similar for enabled), but that's not working.

Can this be done?

Code: Select all

data:extend(
{
  {
    type = "technology",
    name = "available-technology",
    icon_size = 256, icon_mipmaps = 4,
    icons = "__myMod__/graphics/technology/available-technology.png"
    unit =
    {
      count = 1,
      ingredients = {{"automation-science-pack", 1}},
      time = 1
    },
    order = "a"
  },
  {
    type = "technology",
    name = "hidden-technology",
    icon_size = 256, icon_mipmaps = 4,
    icons = "__myMod__/graphics/technology/hidden-technology.png"
    prerequisites = {"available-technology"},
    unit =
    {
      count = 1,
      ingredients = {{"automation-science-pack", 1}},
      time = 1
    },
    order = "a"
  }
})

Code: Select all

script.on_event(defines.events.on_research_finished, function(event)
    local researchFinished = {
        ["available-technology"] = function()
            otherFunctionThatTakesTime()
            data.raw.technology["hidden-technology"].hidden = false
            data.raw.technology["hidden-technology"].enabled = true
        end
    }
    if researchFinished[event.research.name] then
        researchFinished[event.research.name]()
    end
end)
I realise that this specific scenario could be achieved by making technologies upgrades (although admittedly I'm having trouble getting that to work too), but I'd like to do it this way if I can, because the "available-technology" event does extra stuff in-world, which takes some time and if the player completes the new research before the "otherFunctionThatTakesTime()" finishes, then it breaks.

Bilka
Factorio Staff
Factorio Staff
Posts: 3155
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Enable/unhide technology with an event?

Post by Bilka »

Data.raw isn't available during runtime. You have to access the technology on the force that finished the research:

Code: Select all

script.on_event(defines.events.on_research_finished, function(event)
    local researchFinished = {
        ["available-technology"] = function(force)
            otherFunctionThatTakesTime()
            force.technologies["hidden-technology"].enabled = true
        end
    }
    if researchFinished[event.research.name] then
        researchFinished[event.research.name](event.research.force)
    end
end)
And hidden isn't writable during runtime on technologies, that's why I only set enabled. Just don't set the technology to be hidden during the prototype stage, just disabled. It still won't show up until it's enabled at runtime.
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Doctor_Willis
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Sep 26, 2023 11:45 am
Contact:

Re: Enable/unhide technology with an event?

Post by Doctor_Willis »

Thanks Bilka.

I'm still getting an error though. I'm guessing I've missed setting something else up somewhere.

Error while running event myMod::on_research_finished (ID 18)
...yMod__/scripts/researchFinishedEvents.lua:290: attempt to index global 'force' (a nil value)
stack traceback:
...yMod__/scripts/researchFinishedEvents.lua:290: in function '?'
...yMod__/scripts/researchFinishedEvents.lua:295: in function <...yMod__/scripts/researchFinishedEvents.lua:68>

For what it's worth:
Line 68 - script.on_event(defines.events.on_research_finished, function(event)
Line 290 - force.technologies["hidden-technology"].enabled = true
Line 295 - researchFinished[event.research.name](event.research.force)

Bilka
Factorio Staff
Factorio Staff
Posts: 3155
Joined: Sat Aug 13, 2016 9:20 am
Contact:

Re: Enable/unhide technology with an event?

Post by Bilka »

Did you also change the line where you define the function so that it takes the force as a parameter?

["available-technology"] = function(force)
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Doctor_Willis
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Sep 26, 2023 11:45 am
Contact:

Re: Enable/unhide technology with an event?

Post by Doctor_Willis »

:oops: I had not.
That's fixed it, thank you.


... It's been a very long day

Post Reply

Return to “Modding help”