Summary - Inventory system
- Game Development course
- Advanced
- Lesson 30/71
- 4 min
Combine the inventory system with a single summary task.
Inventory system summary
The inventory system has three layers: data, logic and interface. Item information is stored with ItemData, the Inventory class manages the slots, and the interface is drawn with prefab slot widgets. The same items are merged as a stack.
int toplamYigin = birAdet + ikiAdet;Thanks to modular design each layer can be developed independently.
Practice Inventory system summary task
In a class named EnvanterOzet, define a string katman field with the value "Veri", an int slotSayisi field with the value 20, and an int islem field. In the Awake method, set the katman value to "Arayuz". In the Start method, print the pieces "Katman: ", katman, " Slot: " and slotSayisi joined together with Debug.Log. In the Update method, increase the islem counter by one every frame.
Starter code
using UnityEngine;
public class EnvanterOzet : MonoBehaviour
{
// string katman alanini "Veri", int slotSayisi alanini 20 degeriyle tanimla
// int islem alanini tanimla
void Awake()
{
// katman degerini "Arayuz" yap
}
void Start()
{
// "Katman: " + katman + " Slot: " + slotSayisi biciminde yazdir
}
void Update()
{
// islem sayacini her karede bir artir
}
}Sample solution
using UnityEngine;
public class EnvanterOzet : MonoBehaviour
{
string katman = "Veri";
int slotSayisi = 20;
int islem;
void Awake()
{
katman = "Arayuz";
}
void Start()
{
Debug.Log("Katman: " + katman + " Slot: " + slotSayisi);
}
void Update()
{
islem++;
}
}Expected output
Katman: Arayuz Slot: 20
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does a 'stack' do in an inventory system?
int toplamYigin = birAdet + ikiAdet;- It increases the slot count
- It places items in the scene
- It changes the color of the interface
- It gathers the same items in one slot Correct answer
Explanation: A stack collects items of the same type under one slot, letting them be carried without spending slots.
2 Compute the maximum item count
int slotSayisi = 20;
int yiginLimiti = 10;
int maksimumOge = slotSayisi * ____;
int slotSayisi = 20;
int yiginLimiti = 10;
int maksimumOge = slotSayisi * yiginLimiti;
Explanation: The total capacity is found by multiplying the slot count by the stack limit of each slot.
3 Inventory summary table
| Term | Meaning |
|---|---|
ItemData |
The data that holds item info |
Inventory |
The system that manages slots |
Stack |
Combining the same items |
Slot widget |
The slot display in the interface |