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

alexgleason

  • Jr. Member
  • **
  • Posts: 72
  • 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
  1. -- ♡ Copying is an act of love. Please copy and share.
  2.  
  3. require("scripts/multi_events.lua")
  4.  
  5. -- When the player obtains their first item, assign it to slot 1 automatically
  6.  
  7. local function item_obtained_cb(self, variant, savegame_variable)
  8.   local slot_1 = sol.main.game:get_item_assigned(1)
  9.   if slot_1 == nil and self:is_assignable() then
  10.     sol.main.game:set_item_assigned(1, self)
  11.   end
  12. end
  13.  
  14. local item_metatable = sol.main.get_metatable("item")
  15. 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
  1. 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