Community > Your scripts

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

(1/1)

alexgleason:
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) ----- ♡ 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)
--- End code ---

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) ---require("scripts/item_auto_assign")
--- End code ---

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

Navigation

[0] Message Index

Go to full version