Author Topic: Auto-assign an item to slot 1 if it's empty.  (Read 4175 times)

0 Members and 1 Guest are viewing this topic.

  • Full Member
  • ***
  • Posts: 128
  • Vegan on a Desert Island
    • View Profile
    • Vegan on a Desert Island
Auto-assign an item to slot 1 if it's empty.
« on: October 13, 2018, 03:40:57 AM »
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.


-- 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 =
  if slot_1 == nil and self:is_assignable() then, self)

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]
I am assuming you're using the Solarus engine boilerplate code, which already includes the multi_events script.
RIP Aaron Swartz