Izon
Izon

Reputation: 1

Mod Factorio - an attempt to turn off the music while the game is running

I'm trying to write a mod that adds music to the game. Just add music is obtained through the data.lua file

here is the code (data.lua):

require("music.gameplay.gameplay")
require("music.rock.rock")
require("music.starcraft.starcraft")

these are albums - each has a file that adds music via "data:extend"

There are no problems here, everything works. But I would like to implement such a feature - the ability to disable a certain album in the game settings (mod settings)

I created a button in the "settings.lua" file

here is the code (settings.lua):

data:extend({
    {
        type = "string-setting",
        name = "my-mod-always-difficult",
        setting_type = "runtime-global",
        default_value = "yes",
        allowed_values = {"yes", "no"}
    }
})

And from this moment the problems begin. I tried to add music when starting the game in the same "data.lua" file with the condition.

if settings.global["my-mod-always-difficult"].value == "yes" then
    require("music.randFT.gameplay.gameplay")
    require("music.randFT.rock.rock")
    require("music.randFT.starcraft.starcraft")
end

returns an error: attempt to index field 'global' (a nil value)

then I decided to delete the tracks during the map launch in the "control.lua" file: here is the code (settings.lua):

local function removeAddedMusic()
  for i, sound in ipairs(data.raw["ambient-sound"]) do
    if sound.name ~= "Terran_1" then
      table.remove(data.raw["ambient-sound"], i)
    end
  end
end

script.on_init(removeAddedMusic)

tried to delete all but one music - "Terran_1" another mistake: factorio attempt to index global 'data' (a nil value)

I understand that I use to "data" and "global" at the moment when they have the value "nil" but how to do otherwise, I don't understand

I tried to do a couple more actions, but the same trouble, the same mistakes.

Upvotes: 0

Views: 138

Answers (1)

Eph
Eph

Reputation: 1321

I believe it's just settings["my-mod-always-difficult"].value without the global.

Then your removeAddedMusic function would need to be in the data stage, as the control stage can no longer modify the data global.

Upvotes: 0

Related Questions