Skip to content

Commit 4e256f9

Browse files
committed
Add notice for tools added to inventory
Signed-off-by: Ryan Luu <ryan.luu@ryanluu.dev>
1 parent e58cfcd commit 4e256f9

6 files changed

Lines changed: 55 additions & 8 deletions

File tree

‎docs/api-reference.md‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,31 @@ isInventoryOpen(): boolean
6666

6767
Returns `true` if the inventory is open.
6868

69+
### getHotbarSlotCount
70+
71+
```
72+
getHotbarSlotCount(): number
73+
```
74+
75+
Returns the number of hotbar slots. This can change with viewport size and screen orientation (typically 3, 5, or 10). Slot indices from `1` through this value are on the hotbar; higher indices are inventory overflow.
76+
6977
## Events
7078

7179
### backpackItemAdded
7280

7381
```
74-
backpackItemAdded(item: BackpackItem): BindableEvent
82+
backpackItemAdded(item: BackpackItem, slotIndex: number): BindableEvent
7583
```
7684

77-
Fires when an item is added to the backpack.
85+
Fires when an item is added to the backpack. `slotIndex` is the slot the item was placed in. Compare with [getHotbarSlotCount](#gethotbarslotcount) to tell hotbar from inventory.
7886

7987
### backpackItemRemoved
8088

8189
```
82-
backpackItemRemoved(item: BackpackItem): BindableEvent
90+
backpackItemRemoved(item: BackpackItem, slotIndex: number): BindableEvent
8391
```
8492

85-
Fires when an item is removed from the backpack.
93+
Fires when an item is removed from the backpack. `slotIndex` is the slot the item occupied at removal time.
8694

8795
### backpackItemEquipped
8896

‎src/Api/getHotbarSlotCount.luau‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--!strict
2+
3+
const Players = game:GetService("Players")
4+
const RunService = game:GetService("RunService")
5+
6+
const player = Players.LocalPlayer :: Player
7+
const playerGui = player:WaitForChild("PlayerGui")
8+
const screenGui = playerGui:WaitForChild("SatchelGui") :: ScreenGui
9+
10+
const DEFAULT_HOTBAR_SLOT_COUNT = 10
11+
12+
const function getHotbarSlotCount(): number
13+
assert(RunService:IsClient(), "getHotbarSlotCount can only be called on the client")
14+
15+
const count = screenGui:GetAttribute("HotbarSlotCount")
16+
if typeof(count) == "number" then
17+
return count
18+
end
19+
return DEFAULT_HOTBAR_SLOT_COUNT
20+
end
21+
22+
return getHotbarSlotCount

‎src/Client.client.luau‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ const handle = Instance.new("ScreenGui")
3636
handle.Name = "SatchelGui"
3737
handle.ResetOnSpawn = false
3838
handle.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
39+
handle:SetAttribute("HotbarSlotCount", 10)
3940
handle.Parent = playerGui
4041

4142
const root = ReactRoblox.createRoot(handle)
@@ -89,6 +90,10 @@ const function Satchel()
8990
const rows, setRows = useState(4)
9091
const tenFootInterface, setTenFootInterface = useState(false)
9192

93+
useEffect(function()
94+
handle:SetAttribute("HotbarSlotCount", slots)
95+
end, { slots })
96+
9297
useEffect(function()
9398
const function updateBackpackSize()
9499
const screenOrientation = playerGui.CurrentScreenOrientation

‎src/Components/useBackpack.luau‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,17 @@ const function useBackpack(player: Player, hotbarSlotCount: number): BackpackSta
7777
end
7878
end
7979

80-
local function trackTool(tool: Tool)
80+
local function trackTool(tool: Tool, fireAdded: boolean?)
8181
if slotIndexByTool[tool] then
8282
fireEquippedChangedEvents(tool)
8383
return
8484
end
8585
local slotIndex = findNextSlotIndex()
8686
toolBySlotIndex[slotIndex] = tool
8787
slotIndexByTool[tool] = slotIndex
88-
backpackItemAdded:Fire(tool)
88+
if fireAdded ~= false then
89+
backpackItemAdded:Fire(tool, slotIndex)
90+
end
8991
fireEquippedChangedEvents(tool)
9092
end
9193

@@ -97,7 +99,7 @@ const function useBackpack(player: Player, hotbarSlotCount: number): BackpackSta
9799
toolBySlotIndex[slotIndex] = nil
98100
slotIndexByTool[tool] = nil
99101
equippedStateByTool[tool] = nil
100-
backpackItemRemoved:Fire(tool)
102+
backpackItemRemoved:Fire(tool, slotIndex)
101103
return true
102104
end
103105

@@ -125,9 +127,10 @@ const function useBackpack(player: Player, hotbarSlotCount: number): BackpackSta
125127
local function connectInventoryContainer(container: Instance)
126128
table.insert(inventoryConnections, container.ChildAdded:Connect(onInventoryChildAdded))
127129
table.insert(inventoryConnections, container.ChildRemoved:Connect(onInventoryChildRemoved))
130+
-- Existing tools on character/backpack sync are not "added" events
128131
for _, child in container:GetChildren() do
129132
if child:IsA("Tool") then
130-
trackTool(child)
133+
trackTool(child, false)
131134
end
132135
end
133136
end

‎src/TopbarIcon.client.luau‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,16 @@ icon.toggled:Connect(function(isSelected, fromSource)
3939
end
4040
end)
4141

42+
-- Notice only for tools that landed past the hotbar (not visible with inventory closed)
43+
Satchel.backpackItemAdded:Connect(function(_item, slotIndex)
44+
if slotIndex > Satchel.getHotbarSlotCount() and not Satchel.isInventoryOpen() then
45+
icon:notify()
46+
end
47+
end)
48+
4249
Satchel.inventoryOpened:Connect(function()
4350
icon:select()
51+
icon:clearNotices()
4452
end)
4553

4654
Satchel.inventoryClosed:Connect(function()

‎src/init.luau‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ return {
1010
getTheme = require(api.getTheme),
1111
setTheme = require(api.setTheme),
1212
getTopbarIcon = require(api.getTopbarIcon),
13+
getHotbarSlotCount = require(api.getHotbarSlotCount),
1314
openInventory = require(api.openInventory),
1415
closeInventory = require(api.closeInventory),
1516
isInventoryOpen = require(api.isInventoryOpen),

0 commit comments

Comments
 (0)