Auto-assign an item to slot 1 if it's empty.

Started by alexgleason, October 13, 2018, 03:40:57 AM

Previous topic - Next topic
This script is useful for when your character receives the first item of the game. Rather than making them equip the item, you can make it automatically assigned to slot 1. Adding slot 2 support probably wouldn't be too difficult. Here's the script.

Code ( lua) Select
-- ♡ Copying is an act of love. Please copy and share.

require("scripts/multi_events.lua")

-- When the player obtains their first item, assign it to slot 1 automatically

local function item_obtained_cb(self, variant, savegame_variable)
  local slot_1 = sol.main.game:get_item_assigned(1)
  if slot_1 == nil and self:is_assignable() then
    sol.main.game:set_item_assigned(1, self)
  end
end

local item_metatable = sol.main.get_metatable("item")
item_metatable:register_event("on_obtained", item_obtained_cb)


Install it by creating the file scripts/item_auto_assign.lua in your project and pasting the above code.

Then, open scripts/features.lua and add this to the list:

Code ( lua) Select
require("scripts/item_auto_assign")

I am assuming you're using the Solarus engine boilerplate code, which already includes the multi_events script.
RIP Aaron Swartz