Custom Color

by DiRten

More Colors for Vehicles, Players, Trains, Train Stops, Tanks, Cargo-wagons, Fluid-Wagons, Spidertrons -RGB value (Including color saturation) -Copy/Paste color (Shift-RMB -> Shift-LMB) *Vehicle don't change color with player*

Utilities
10 months ago
1.1
1.60K
Transportation Trains

b Crashes when mod is added to running game

1 year, 2 months ago

Your mod will crash when it's added to a running game:

Error while running event CustomColor::on_configuration_changed
__CustomColor__/libraries/gui/actions.lua:33: attempt to index global 'customcolor_window_frame' (a nil value)

You can fix this by changing lines 31-35 of libraries/gui/actions.lua to this:

function gui_destroy(player)
        if player.gui.relative.customcolor_window_frame ~= nil then
                 player.gui.relative.customcolor_window_frame.destroy()
        end
end

Then you'll get another error:

Error while running event CustomColor::on_configuration_changed
Gui element with name customcolor_open_button already present in the parent element.
stack traceback:
  [C]: in function 'add'
  __CustomColor__/libraries/gui/open_button.lua:2: in function 'create_open_button'
  __CustomColor__/libraries/gui/actions.lua:12: in function 'gui_create'
  __CustomColor__/libraries/update.lua:39: in function <__CustomColor__/libraries/update.lua:1>

It seems the logic in the function gui_valid() (in libraries/gui/actions.lua) is off:

function gui_valid(player)
  if player.gui.relative.customcolor_window_frame == nil then
    return false
  else
    return true
  end
end

When your mod is added to a running game, it will first run gui_create() for on_init and then again for on_configuration_changed. I've logged some debugging output and can confirm that gui_valid() returned false both times. Perhaps you've changed the frame's name at some time, or never create the frame?

As a workaround, I've changed the function in libraries/gui/open_button.lua to this:

function create_open_button(target_gui, mod_name, mod_icon, anchor)
  -- BEGIN OF CHANGE
  if target_gui and target_gui.valid then
    local name = mod_name .. "_" .. "open_button"
    local open_button = target_gui[name]

    if not (open_button and open_button.valid) then
      open_button = target_gui.add
  -- END OF CHANGE
      {
        type = "sprite-button",
        name = name,  -- LINE CHANGED
        sprite = mod_icon,
        style = "side_menu_button"
      }
    end   -- LINE ADDED
    if anchor ~= nil then
      if anchor.type == nil then
        open_button.anchor =
        {
          gui = defines.relative_gui_type[anchor.relative_gui],
          position = defines.relative_gui_position[anchor.position]
        }
      else
        open_button.anchor =
        {
          gui = defines.relative_gui_type[anchor.relative_gui],
          type = anchor.type,
          position = defines.relative_gui_position[anchor.position]
        }
      end
    end
  end   -- LINE ADDED
end

This works (i.e. the game will load without crashing), but of course you should still try to fix the failing test in gui_valid().

1 year, 2 months ago

Thanks! I fix that.

1 year, 2 months ago

I forgot to delete open_button in gui_destroy().
I added delete open_button.
Crash are not registered.
I'm pretty tired, Perhaps I missed something in the code, if so, please let me know.

1 year, 2 months ago

Thanks, it seems to be working now!

New response