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