Scrap Fleet (working now@last post)

A place for discussion of making game modifications.
Post Reply
Jem
Posts: 31
Joined: Tue Aug 15, 2017 9:21 am

Scrap Fleet (working now@last post)

Post by Jem »

I'm trying to make a button that scrap current fleet. With my limited understanding, this is as far as I can go. How do I make this work?

\Lua state\GUI\~GalaxyMap\@FleetShips.lua

Code: Select all

    local el = med_tooltip_icon(hover_id, icon,cx,cy)
--scrap.fleet
    local scrap_icon = notification_icons.misc.recycle
    local scrap_el = med_tooltip_icon(scrap_hover_id, scrap_icon,cx,bottom_bar.height*.45)
    scrap_el.on_click = scrap_fleet
    local function scrap_fleet()
       for scrap_ship ; fleet.ships
         fleet:scrap(scrap_ship) 
       end
    end
--scrap.fleet.end
    --local color = can_command_fleet(fleet) and blue or grey
Last edited by Jem on Fri Nov 15, 2019 3:07 am, edited 1 time in total.
User avatar
sven
Site Admin
Posts: 1621
Joined: Sat Jan 31, 2015 10:24 pm
Location: British Columbia, Canada
Contact:

Re: Scrap Fleet (not working)

Post by sven »

Jem wrote:How do I make this work?
You're very close to having working code there. The first problem is that scrap_hover_id needs to be defined as something. It doesn't matter what it's defined as, it just needs to be some unique value that's not being used as a hover id anywhere else in the game. Um, a reasonably descriptive string would work fine.

The second problem is that you've defined a local after you referenced it, so you're not actually injecting the definition you think you are. This code will run:

Code: Select all

--scrap.fleet
    local scrap_icon = notification_icons.misc.recycle
    local scrap_el = med_tooltip_icon('scrap_fleet_hover_id', scrap_icon,cx,bottom_bar.height*.45)
    local function scrap_fleet()
       for scrap_ship ; fleet.ships
         fleet:scrap(scrap_ship)
       end
    end 
    scrap_el.on_click = scrap_fleet
--scrap.fleet.end
However, it has at least 2 moderately serious problems.

The first is that you're defining a new scrap_fleet function each time you draw the fleet UI, which is inefficient. The second is that you're using low-level galaxy state update functions without wrapping them in a "order" call, so if you scrap a fleet with your button, you'll make the undo stack messy.

An implementation that fixes all this might look like this. First, above the draw_fleet function, define:

Code: Select all

local function scrap_fleet(el)
  execute_order('scrap_fleet',|) function()
    for scrap_ship ; el.fleet.ships
      el.fleet:scrap(scrap_ship)
    end
  end
end 
Then, in your fleet drawing hook, just say:

Code: Select all

--scrap.fleet
    local scrap_icon = notification_icons.misc.recycle
    local scrap_el = med_tooltip_icon('scrap_fleet_hover_id', scrap_icon,cx,bottom_bar.height*.45)
    scrap_el.fleet=fleet
    scrap_el.on_click = scrap_fleet
--scrap.fleet.end
Jem
Posts: 31
Joined: Tue Aug 15, 2017 9:21 am

Re: Scrap Fleet working now

Post by Jem »

Thanks Sven for your help in this. I can't be any more grateful, for you've pointed out the things I've overlooked due to my lack of understanding. I also realized I've missed to add the tooltip to say what that button does...

For anyone else who may want to try this [Scrap Fleet] go to \Lua state\GUI\~GalaxyMap\@FleetShips.lua
and add the following respectively. This action will remove the selected fleet and (silently) return coins&metal to your coffers/storage. Also as Sven mentioned, [Undo] works too, if you click this by mistake. Cheers!

*Edit: Added the scrap ship audio

Code: Select all

--scrap.fleet.function
local function scrap_fleet(el)
  execute_order('scrap_fleet',|) function()
    for scrap_ship ; el.fleet.ships
      el.fleet:scrap(scrap_ship)
    end
  end
  Sounds.SCRAP_SCUTTLE_SHIP()
end
--scrap.fleet.function.end

local draw_fleet_ships  = restore_color | function(fleet)
.
.
.
    local el = med_tooltip_icon(hover_id, icon,cx,cy)

--scrap.fleet.button
    local scrap_hover_id='scrap_this_fleet'
    local scrap_icon = notification_icons.misc.recycle
    local scrap_el = med_tooltip_icon(scrap_hover_id, scrap_icon,cx,bottom_bar.height*.45)
    med_icon_tooltip(scrap_hover_id, cx, bottom_bar.height*.45, "Scrap Fleet")
    scrap_el.fleet=fleet
    scrap_el.on_click = scrap_fleet
--scrap.fleet.button.end
Post Reply