자료 구조를 말씀하시는 건가요?
보통 사이즈가 정해져 있는 인벤 같은 경우에는 배열로 하고,
유동적으로 늘어나는 것은 맵 같은 자료를 많이 써요.
아무래도 인벤토리의 아이템들은 서치도 빈번하게 일어나니까요.
또 인덱스 위치랑 바로 매칭이 되고요.
저도 기존에 온라인 rpg 유지보수 일 하면서 소소를 많이 봤는데 대부분 이런식으로 하더라구요.
그리고 이번에 직접 플젝에 인벤을 구현했는데 직접 손으로 만들어보니까 생각보다
신경 써야 할 부분들이 많더라구요.
인벤토리 한 칸에 들어가야 할 정보들... 즉, 아이템 하나가 가지는 속성들을 잘 생각하셔서
구조체로 만들고 이것들을 관리하는 자료구조를 잘 적용하고 설계하는 것을 주 목표로 잡으시면 될 것 같네요
일단 첨부하신건 보지 않았습니다.
아이템에 수량이 존재한다면 다음처럼 풀 수 있을 것입니다.
저는 c#으로 짜기때문에 c#으로 작성해보겠습니다.
갯수를 가진 아이템 넣고 빼는 것중에서 가장 심플한 형태라고 생각합니다.
이 재료로 이제 알아서 뿌려주시면 됩니다.
UI와 연결하는것이나 아이템을 저장&불러오는 것은 본인의 재량으로...
저 컴포넌트를 가진 게임오브젝트를 인벤토리로 만드시고
OnGUI 함수 만드셔서 직접 뿌려줘도 될 것입니다.
Stacking Items in a Inventory List
I have my inventory script, an enemy script with item dropping and looting, and an Item Class script all in Java. It all works fine, but I'm trying to find a way to make Items stackable. I have added a stackable boolean to my items and set them to true as well as a stack limit and amount variable. What I don't understand is how, when a button is clicked, can I search through a list for that item, if it exists change a varialble on that item. That is my only problem with this script right now. Here are those scripts(one being just a snippet). My Item Class. My Enemy my inventory class And the snippet that deals with the inventory in my levelmanager script. I'm not looking for a system all coded out, or I would have already done that. I like the way my system works, and the fact I made it. this is the first time I've come to the answers community and I hope I can learn something here. Any help would be appreciated :) |
Well there are some things i would change ;) First the boolean is redundant. Since you have a MaxStack variable you can simply set it to 0 or -1 for items that aren't stackable. Next thing is how can you tell that two items are of the same type? ItemID? Name? You have to specify a criteria which you can use to identify similar items. I would probably create a filter function. Something like that: Note: I just wrote / modified that here on UA so there might be some syntax errors ;) I just compared the ItemID in this case. Maybe you want something else?! |
Take a look, maybe you can do something like this(C# Code): (Tested) |
The code is like http://www.burgzergarcade.com/. Thanks to his tutorials i was able to start a game. I have been modifying and changing so it can become my own game. i would really like to be able to stack items, since my inventory will have infinity items. I am not sure how to start with the code to check if item is same vs quantity vs stack number.
- public class Item
- {
- public int itemID;
- public string name = "";
- public int level;
- public bool stackable = true;
- public int quantity = 1;
- public int maxQuantity = 99;
- public int stack = 1;
- public int maxStack = 5;
- public int attack;
- public int physicalDefense;
- public int charges;
- public int cost;
- public string description = "";
- public Texture2D icon;
- }
-
- public static class ItemGenerator
- {
-
- public static Weapon AddMeleeWeapon(ItemsNames meleeWeaponName, int quantity)
- {
- Weapon meleeWeapon = new Weapon();
-
- switch(meleeWeaponName)
- {
- case ItemsNames.Sword:
- meleeWeapon.itemID = 1;
- meleeWeapon.name = "Sword";
- meleeWeapon.level = 1;
- meleeWeapon.stackable = true;
- meleeWeapon.quantity = quantity;
- meleeWeapon.maxQuantity = 99;
- meleeWeapon.stack = 1;
- meleeWeapon.maxStack = 5;
- meleeWeapon.attack = 10;
- meleeWeapon.cost = 5;
- meleeWeapon.description = "Iron Sword";
- break;
- default:
- break;
- }
-
- meleeWeapon.icon = Resources.Load( "Item/Icon/Weapon/Melee/" + "Sword" ) as Texture2D;
-
- return meleeWeapon;
- }
- }
-
- public class Chest : MonoBehaviour
- {
- public List<Item> loot = new List<Item>();
-
- void Start()
- {
- loot.Add(ItemGenerator.AddMeleeWeapon( ItemsNames.Sword ));
- loot.Add(ItemGenerator.AddMeleeWeapon( ItemsNames.Sword ));
- loot.Add(ItemGenerator.AddMeleeWeapon( ItemsNames.Sword ));
- loot.Add(ItemGenerator.AddMeleeWeapon( ItemsNames.Sword ));
- }
- }
-
- public class PC : BaseCharacter(MonoBehavior)
- {
- private List<Item> inventory = new List<Item>();
-
- public List<Item> Inventory
- {
- get { return inventory; }
- set { inventory = value; }
- }
- }
-
- public class MyGUI : MonoBehaviour
- {
-
- private void LootWindow(int id)
- {
-
- for(int cnt = 0; cnt < chest.loot.Count; cnt++)
- {
- if(GUI.Button(new Rect(5 +(50 * cnt), 10, 50, 50), new GUIContent(chest.loot[cnt].icon)))
- {
- PC.Instance.Inventory.Add(chest.loot[cnt]);
- chest.loot.RemoveAt(cnt);
- }
- }
- }
-
- public void InventoryWindow(int id)
- {
- int cnt = 0;
-
- for(int y = 0; y < 15; y++)
- {
- for(int x = 0; x < 10; x++)
- {
- if(cnt < PC.Instance.Inventory.Count)
- {
- if(GUI.Button(new Rect(5 + ( x * buttonWidth ), 20 + (y * 50), 50, 50), new GUIContent(PC.Instance.Inventory[cnt].icon)))
- {
- }
- }
- else
- {
- GUI.Button(new Rect(10 + x * 50, 25 + y * 50, 50, 50), "");
- }
-
- cnt++;
- }
- }
- }
- }
This is Trimmed down to explain what i have.
'유니티 > Note' 카테고리의 다른 글
| 유니티 퀘스트 관련 (0) | 2014.04.10 |
|---|---|
| 유니티 스크립트 호출 순서 제어 (0) | 2014.04.09 |
| 유니티 인벤토리 관련 구현 (0) | 2014.04.07 |
| delegate, action, event 활용 관련 (0) | 2014.04.07 |
| 안드로이드 In-app Billing Version 3 - 구글번역 (0) | 2014.03.27 |
| 파이어 폭스 유용한 부가기능 (0) | 2014.02.24 |



You are brilliant. This worked like a dream, albeit with some modification to my inventory managing but it worked out perfectly. This is where I was trying to go with my script, but I had no idea how to approach this. Thank you very much for showing me.