Page 1 of 1

Enable/unhide technology with an event?

Posted: Fri Apr 26, 2024 8:20 am
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.

Re: Enable/unhide technology with an event?

Posted: Fri Apr 26, 2024 8:51 am
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.

Re: Enable/unhide technology with an event?

Posted: Fri Apr 26, 2024 9:08 am
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)

Re: Enable/unhide technology with an event?

Posted: Fri Apr 26, 2024 9:46 am
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)

Re: Enable/unhide technology with an event?

Posted: Fri Apr 26, 2024 9:57 am
by Doctor_Willis
:oops: I had not.
That's fixed it, thank you.


... It's been a very long day