- Home /
Real Simple Inventory and Vendor System Help
Ok so i'm trying to create a very simple inventory and vendor system using the scripts below. Right now, i'm stuck at the point where i want to just click on an item from the vendor window and then the GUI button of this item disappears from him and appears in the player's inventory. I tried to use the "GetComponent" method which i've used successfully on other scripts but with no luck. Is there any other way to do such a thing? Of course, i'd love to have a drag-and-drop system but that's a little more advanced than what i want right now. Thanks!
 Vendor Script.js
 
 var range : float;
 var player : GameObject;
 
 var close : boolean = false;
 var vendorwindow : boolean = false;
 
 function OnMouseOver(){
     if((Input.GetMouseButtonDown(0)) && close){
         vendorwindow = true;
     }    
 }
 
 function Update(){
     if(Vector3.Distance(player.transform.position, transform.position)<=range){
         close = true;
     }    
     else{
         close = false;
         vendorwindow = false;
     }
 }
 
 function OnGUI(){
     if(vendorwindow){
         GUI.Box(new Rect(20,10,160,160), "SHOP");
         
         if(GUI.Button(new Rect(40,40,50,40),"Sword")){
             Debug.Log("I bought a sword!");
         }
         if(GUI.Button(new Rect(40,90,50,40),"Shield")){
             Debug.Log("I bought a shield!");
         }
         if(GUI.Button(new Rect(110,40,50,40),"Bow")){
             Debug.Log("I bought a bow!");
         }
         if(GUI.Button(new Rect(110,90,50,40),"Arrows")){
             Debug.Log("I bought some arrows!");
         }    
     }
 }
 Player Inventory.js
 
 var openINV : boolean = false;
 
 function Update(){
     if(Input.GetKeyDown(KeyCode.I)){
         openINV = !openINV;
     }    
 }
 
 function OnGUI(){
     if(openINV){
         GUI.Box(new Rect(Screen.width - 180,Screen.height - 180,160,160),"INVENTORY");
         
         GUI.Box(new Rect(Screen.width - 160,Screen.height - 150,50,40),"");
         GUI.Box(new Rect(Screen.width - 160,Screen.height - 100,50,40),"");
         GUI.Box(new Rect(Screen.width - 90,Screen.height - 150,50,40),"");
         GUI.Box(new Rect(Screen.width - 90,Screen.height - 100,50,40),"");
     }
 }
 
You can't hard-code something that is dynamically changing...
Your inventory/vendor list of items shouldn't be a sequence of ifs. It should be a list that you iterate through
Answer by Clet_ · May 19, 2014 at 11:57 PM
Edit : Just noted your answer was in JS. I don't know shit about JS, but it should be easy to translate.
You should have a class named item that looks like this :
 public class Item {
     private string _name;
     public string name {
         get { return _name; }
     }
     
     public Item(string name) {
         if(string.IsNullOrEmpty (name)) {
             throw new System.ArgumentNullException("name");
         }
 
         _name = name;
     }
 
     public void OnItemBought () {
         Debug.Log (string.Format "You bought a {0}!", _name);
     }
 }
Then, you'd need to have a vendor and an inventory class who both has a list of items :
 List<Item> items = new List<Item>();
After the vendor list has been initialized i.e. every item it can sell has been added to its item list. You'd be able to display dynamically the list with a function that looks something like :
 for(int i = 0; i < vendor.items.Count; i++) {
     if(GUIButton(new Rect(40, 40 + 50*i, 50, 40), vendor.items[i].name)) {
         inventory.Add (vendor.items[i]);
         vendor.items[i].OnItemBought ();
         vendor.items.RemoveAt (i--);
     }
 }
The code won't work as is. It's up to you to apply this concept to your specific environment
No worries. I've wrote some little scripts in C# as well so it should be easy to understand your answer. It's just that i've never worked with Lists before.
You should really explore Generic collections. Take a day off and look into Dictionary, List, Stack, Queue, LinkedList, HashSet, etc.
Thank me later.
Your answer
 
 
             Follow this Question
Related Questions
Item/inventory system-equipable items 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Can't Assign Item In Array 1 Answer
Problem with dropping items from my inventory 0 Answers
Crafting system 2 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                