Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Interactive Inventory System
- jdizurieta2010
-
11 posts
Interactive Inventory System
I am making a point-and-click game. You need to drag and drop items to use them and escape a map.
I tried making an interactive inventory system. You get an item, and a slot (with the item) appears, then you get another item, and the first slot moves a little bit to the right, the second slot appears next to it, and so on.
Similar to how Roblox: Doors does it.
Here is my failed attempt of doing that. I would greatly appreciate if you could help me, it would help me learn something new.
I tried making an interactive inventory system. You get an item, and a slot (with the item) appears, then you get another item, and the first slot moves a little bit to the right, the second slot appears next to it, and so on.
Similar to how Roblox: Doors does it.
Here is my failed attempt of doing that. I would greatly appreciate if you could help me, it would help me learn something new.
- Hostcreator2
-
74 posts
Interactive Inventory System
Got it! Here's how you can create an interactive inventory system in Scratch, similar to Roblox: Doors, where items appear in slots, and the slots shift to accommodate new items:
Key Concepts
Variables for Inventory Management:
inventory (list): Stores the names of the collected items.
maxSlots (number): The maximum number of slots the inventory can display at a time.
slotSpacing (number): The distance between inventory slots.
Sprites:
Slot Sprite: A visual container for each item.
Item Sprites: Drag-and-drop interactable items.
Step-by-Step Guide
1. Create a List for Inventory
In Scratch, create a list called inventory. Each item you collect will be added to this list.
2. Slot Logic
Create a “Slot” sprite to represent an inventory slot.
In the Slot sprite:
scratch
Copy code
when green flag clicked
hide // Slot sprites are clones; hide the original
Whenever an item is added:
scratch
Copy code
when I receive
delete all clones
set to (1)
repeat (length of ) // Create slots for each item
create clone of
change by (1)
end
For the slot clones:
scratch
Copy code
when I start as a clone
show
set to (i) // Track the item's position
go to x: ((-200) + ((index) - 1) * (slotSpacing)) y: (-150) // Adjust slot position
3. Add Items to Inventory
For each interactable item, use a script like this:
scratch
Copy code
when this sprite clicked
add to
broadcast
hide // Hide the item in the world
4. Drag-and-Drop Items
In the Slot sprite:
Track dragging:
scratch
Copy code
when this sprite clicked
set to
repeat until <not <mouse down?>>
go to
end
set to
Check if dropped in a usable location:
scratch
Copy code
if <touching > then
// Execute logic for using the item
delete (index) of // Remove the item
broadcast // Update the inventory
end
Additional Features
Max Slots: If you want a limited number of slots visible:
scratch
Copy code
repeat (min of (maxSlots) and (length of ))
Highlight Slots: Use a costume change to visually indicate which slot/item is being dragged or selected.
Animations: Use smooth transitions when slots move:
scratch
Copy code
glide (0.2) secs to x: ((-200) + ((index) - 1) * (slotSpacing))
Key Concepts
Variables for Inventory Management:
inventory (list): Stores the names of the collected items.
maxSlots (number): The maximum number of slots the inventory can display at a time.
slotSpacing (number): The distance between inventory slots.
Sprites:
Slot Sprite: A visual container for each item.
Item Sprites: Drag-and-drop interactable items.
Step-by-Step Guide
1. Create a List for Inventory
In Scratch, create a list called inventory. Each item you collect will be added to this list.
2. Slot Logic
Create a “Slot” sprite to represent an inventory slot.
In the Slot sprite:
scratch
Copy code
when green flag clicked
hide // Slot sprites are clones; hide the original
Whenever an item is added:
scratch
Copy code
when I receive
delete all clones
set to (1)
repeat (length of ) // Create slots for each item
create clone of
change by (1)
end
For the slot clones:
scratch
Copy code
when I start as a clone
show
set to (i) // Track the item's position
go to x: ((-200) + ((index) - 1) * (slotSpacing)) y: (-150) // Adjust slot position
3. Add Items to Inventory
For each interactable item, use a script like this:
scratch
Copy code
when this sprite clicked
add to
broadcast
hide // Hide the item in the world
4. Drag-and-Drop Items
In the Slot sprite:
Track dragging:
scratch
Copy code
when this sprite clicked
set to
repeat until <not <mouse down?>>
go to
end
set to
Check if dropped in a usable location:
scratch
Copy code
if <touching > then
// Execute logic for using the item
delete (index) of // Remove the item
broadcast // Update the inventory
end
Additional Features
Max Slots: If you want a limited number of slots visible:
scratch
Copy code
repeat (min of (maxSlots) and (length of ))
Highlight Slots: Use a costume change to visually indicate which slot/item is being dragged or selected.
Animations: Use smooth transitions when slots move:
scratch
Copy code
glide (0.2) secs to x: ((-200) + ((index) - 1) * (slotSpacing))
- Jareddddddd
-
1000+ posts
Interactive Inventory System
snip
yo is this chat gpt?? many various inconsistencies.
- GamingCat12345
-
100+ posts
Interactive Inventory System
Please don't use ChatGPT to help other people. You can use it for yourself, but not others. Some of the time, they won't know that someone used AI to get the response. Long script that was clearly ChatGPT
Last edited by GamingCat12345 (Nov. 20, 2024 02:06:24)
- GamingCat12345
-
100+ posts
Interactive Inventory System
I am making a point-and-click game. You need to drag and drop items to use them and escape a map.
I tried making an interactive inventory system. You get an item, and a slot (with the item) appears, then you get another item, and the first slot moves a little bit to the right, the second slot appears next to it, and so on.
Similar to how Roblox: Doors does it.
Here is my failed attempt of doing that. I would greatly appreciate if you could help me, it would help me learn something new.
Okay. I've located a problem. Then another 2 that causes that problem. Then more problems causing those. Im going to remake the entire code.
Make sure the “my slot” and “x pos” variables are “for this sprite only”
when green flag clicked
set [length v] to [0]
delete all of [inventory v]
set [my slot v] to [0]
forever
if <(length) < (length of [inventory v] :: list)> then
change [length v] by (1)
change [my slot v] by (1)
create clone of [myself v]
end
if <(length) > (length of [inventory v] :: list)> then
change [length v] by (-1)
change [my slot v] by (-1)
end
end
when I start as a clone
forever
if <(my slot) > (length of [inventory v] :: list)> then
delete this clone
end
set [x pos v] to (((Range) / (length of [inventory v] :: list)) * ((my slot) - (((length of [inventory v] :: list) + (1)) / (2))))
change x by (((x pos) - (x position)) / (10))
if <(round (x pos)) = (round (x position))> then
set x to (x pos)
end
end
The Range variable is just whatever number that you want to be the range of your inventory slots.
Last edited by GamingCat12345 (Nov. 20, 2024 02:04:11)
- jdizurieta2010
-
11 posts
Interactive Inventory System
First time I read I was like. Huh? So specific….Please don't use ChatGPT to help other people. You can use it for yourself, but not others. Some of the time, they won't know that someone used AI to get the response. Long script that was clearly ChatGPT
- Oisthebestletter
-
100+ posts
Interactive Inventory System
(#6)Please do not necropost. The last post here was from 2024. I just wasted my time reading this.First time I read I was like. Huh? So specific….Please don't use ChatGPT to help other people. You can use it for yourself, but not others. Some of the time, they won't know that someone used AI to get the response. Long script that was clearly ChatGPT
- jdizurieta2010
-
11 posts
Interactive Inventory System
It's not abandoned, I just got a notification for this.(#6)Please do not necropost. The last post here was from 2024. I just wasted my time reading this.First time I read I was like. Huh? So specific….Please don't use ChatGPT to help other people. You can use it for yourself, but not others. Some of the time, they won't know that someone used AI to get the response. Long script that was clearly ChatGPT
Last edited by jdizurieta2010 (Jan. 28, 2025 23:45:47)
- jdizurieta2010
-
11 posts
Interactive Inventory System
I am making a point-and-click game. You need to drag and drop items to use them and escape a map.
I tried making an interactive inventory system. You get an item, and a slot (with the item) appears, then you get another item, and the first slot moves a little bit to the right, the second slot appears next to it, and so on.
Similar to how Roblox: Doors does it.
Here is my failed attempt of doing that. I would greatly appreciate if you could help me, it would help me learn something new.
Okay. I've located a problem. Then another 2 that causes that problem. Then more problems causing those. Im going to remake the entire code.
Make sure the “my slot” and “x pos” variables are “for this sprite only”when green flag clicked
set [length v] to [0]
delete all of [inventory v]
set [my slot v] to [0]
forever
if <(length) < (length of [inventory v] :: list)> then
change [length v] by (1)
change [my slot v] by (1)
create clone of [myself v]
end
if <(length) > (length of [inventory v] :: list)> then
change [length v] by (-1)
change [my slot v] by (-1)
end
end
when I start as a clone
forever
if <(my slot) > (length of [inventory v] :: list)> then
delete this clone
end
set [x pos v] to (((Range) / (length of [inventory v] :: list)) * ((my slot) - (((length of [inventory v] :: list) + (1)) / (2))))
change x by (((x pos) - (x position)) / (10))
if <(round (x pos)) = (round (x position))> then
set x to (x pos)
end
end
The Range variable is just whatever number that you want to be the range of your inventory slots.
I get what you're trying to do, so I recreated your code but it's just creating multiple backgrounds for the inventory.
- Discussion Forums
- » Help with Scripts
-
» Interactive Inventory System