Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Computer Based Store

Discussion in 'Scripting' started by rawlife1995, Sep 5, 2014.

  1. rawlife1995

    rawlife1995

    Joined:
    May 2, 2013
    Posts:
    23
    I Am Trying To Make An "Online" Store/s Much Like In GTA Like You Get On The Computer Purchase What You Want It Will Then Show Up In The Players Inventory And You Can Use/Place Said Item, I Am In A Bit Of A Rut I Cant Figure The Best Way To Make The "Online" Store And Make It Show All Items Etc. Any Words Of Wisdom Are Appreciated Thanks.
     
  2. TD10074405

    TD10074405

    Joined:
    Aug 23, 2014
    Posts:
    35
    You will need three things:
    1. Some knowledge of code
    2. A GUI System
    3. Game object(s)
    Firstly,
    You will need to setup an inventory system that can be called by the user to show what items they have.
    The easiest way to do so is to use the GUI, without any gui objects (Skins, textures etc) you can do so pretty easily.
    Here is a simple way to get a GUI setup:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class BASIC_C_SHARP_INVENTORY : MonoBehaviour {
    6.   public List<SlotDef> Inventory = new List<SlotDef>();
    7.   public bool InventoryOpen;
    8.   public bool LMDown;
    9.  
    10. public void Start(){
    11.   InventoryOpen = false;
    12. }
    13. public void Update(){
    14.     if (Input.GetMouseButtonDown(0)) {
    15.         LMDown = true;
    16.     }else if(Input.GetMouseButtonUp(0)){
    17.         LMDown = false;
    18.     }
    19. }
    20. //This adds the item to your inventory
    21. public void AddItem(ItemDefine iDef){
    22.     bool Finished = false;
    23.  
    24.     //Make sure there are 25 slots in the inventory
    25.     for(int x = 0; x < 25; x++){
    26.         if(Inventory[x].iDef.ITM_Icon == null){
    27.             if(!Finished){
    28.                 Inventory[x].iDef = iDef;
    29.                 Finished = true;
    30.             }
    31.         }
    32.     }
    33.  
    34. }
    35.  
    36. public OnGUI(){
    37.   int ITEM = 0;
    38.     if(InventoryOpen){ //Inventory is open, Show the GUI!
    39.         for(int y = 0; y < 5; y++){
    40.             for(int x = 0; x < 5; x++){
    41.                 Inventory [ITEM].Slot.x = (x * 55) + 10; // Position of icon width
    42.                 Inventory [ITEM].Slot.y = (y * 55) + 10; // Position of icon height
    43.                 Inventory [ITEM].Slot.width = 50;         // icon width
    44.                 Inventory [ITEM].Slot.height = 50;         // icon height
    45.            
    46.                 //Create our Slot, show the icon too
    47.                 GUI.Box (Inventory [ITEM].Slot, new GUIContent (Inventory [ITEM].iDef.ITM_Icon, Inventory [ITEM].iDef.ITM_Name));
    48.            
    49.             ITEM++;
    50.             }
    51.         }
    52.     }
    53. }
    54.  
    55. [System.Serializable]
    56. public class SlotDef{
    57. public ItemDefine iDef;
    58. public Rect Slot;
    59. }
    This code will create a simple Inventory, you will now need to make a file called ItemDefine (in C#) filled with fields such as ITM_Icon (Texture2D), ITM_Name (String), and a ITM_Type (Enumerator)

    It would be better to make the AddItem() function an inherited class so that a loot system could access it, but because its public it shouldn't be too bad.

    Each item you put through the "AddItem()" function will be added to your inventory. Once its full, its full.

    The next step would be then to create objects with the item definitions, create a menu system that only loads on mouse over of a specific object (I.E Computer model) and click which then saves items to your inventory if you have enough money to buy an item - This could be achieved by inserting a Float field into the item definition i.e ITM_Cost = 100.00F; then checking on the menu system that the player has => 100 coins, if so Add Item to inventory

    Also.. PLEASE PLEASE PLEASE stop using CaMeL CaSe (Humps), There IS No Need To Put A Capital Letter At The Start Of Each Word, It Is Annoying And Hard To Read.
     
    Last edited: Sep 6, 2014
  3. rawlife1995

    rawlife1995

    Joined:
    May 2, 2013
    Posts:
    23
    Any chance there is a more simple way, i hate to sound dumb but i don't really understand all of what you put and i don't like to copy and paste, I don't need anything super advance I'm am doing this project to further my learning as I've already done basic games etc. If not I will study the code more thanks again.
     
  4. TD10074405

    TD10074405

    Joined:
    Aug 23, 2014
    Posts:
    35
    Right, any kind of inventory is not going to be simple

    You will need some sort of knowledge of stuff like linked structures (Arrays, List and Vectors) because you're going to need somewhere to store your items/game objects.

    I have added the basics for a simple inventory below. including the shop system, Although they are COMPLETELY UNTESTED and so may have errors as its 2AM and I'm half asleep! If you do have any errors/problems/questions, drop me a PM and I'll try to mentor you through the processes..

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic; //Adds the ability to use LinkedLists
    5.  
    6. public class BASIC_INVENTORY : MonoBehaviour {
    7.  
    8.     //This creates a list of objects that follow the style in the SlotDefine class below.
    9.     //It makes all items of type .Item, which can inherit variables from ItemDefine such as ITM_Icon, ITM_Name etc
    10.     public List<SlotDefine> Inventory = new List<SlotDefine>();
    11.     public static BASIC_INVENTORY Instance;    //Create an "Instance" of the inventory transaction.
    12.    
    13.     public Start(){
    14.         Instance = this;
    15.     }
    16.    
    17.     //This is a function that just stores items in a list to be used by another function.
    18.     public void AddItem(ItemDefine AOR_ITM){
    19.             bool Finished = false;                //Keep adding items until finished, Because we're not finished, Finished is always false until we set it
    20.             for(int x = 0; x < 25; x++){        //Set the amount of items we can add to our inventory. in this case 25
    21.                 if(Inventory[x].Item.ITM_Icon == null){            //If the Inventory slot is empty (null)
    22.                     if(!Finished){                                //And we're not finished
    23.                         Inventory[x].Item = AOR_ITM;            //The inventory slot is now the item that is of type ItemDefine i.e has a name, icon, price, type
    24.                         Finished = true;                        //We're finished adding the item!
    25.                 }
    26.             }
    27.         }
    28.     }
    29. }
    30.  
    31. [System.Serializable]
    32. public class SlotDefine{
    33.     public ItemDefine Item;        //Creates an interface to the ItemDefine class.
    34.     public Rect Slot;            //Creates a Rectangle called Slot to hold our variables for height/width of Icons
    35. }
    36.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [System.Serializable]
    5. public class ItemDefine {
    6.     //These variables are declared in the inspector
    7.     public string ITM_Name; //The items name
    8.     public int ITM_Count; //For stacking Items
    9.     public int ITM_Cost; //How many coins the item will cost
    10.     public Texture2d ITM_Icon;    //The icon for our "Website" and Inventory
    11.     public Item_type ITM_Type;    //An item from our list below
    12.    
    13.     //A selective list for the inspector, with a dropdown menu.
    14.     public enum Item_type{
    15.         Weapon,
    16.         Ammo,
    17.         Clothing,
    18.         Food
    19.     }
    20. }

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class ComputerStore : MonoBehaviour{
    7.  
    8.     //This script must be attached to an object you want to click on i.e a model of a PC/Laptop
    9.    
    10.     public List<ItemDefine> ShopStock = new List<ItemDefine>(); //A list to store our purchasable items in
    11.     public PlayerStats Player;    //Interface with the PlayerStats class!
    12.     public float Cash;            //A Float is chosen because we can have $1.50 / £1.50 or $0.40/£0.40 etc and an integer is a whole number i.e 1, 2, 3 etc
    13.     public bool RMDown;            //Check if the Left mouse button is down
    14.    
    15.     public Update(){
    16.         Cash == Player.Coins; //Our integer "Cash" is now equal to whatever value the player has in their purse
    17.        
    18.        
    19.         //Check if the Right mouse button is being pressed!
    20.         if (Input.GetMouseButtonDown(1)) {
    21.             RMDown = true;
    22.         }else if(Input.GetMouseButtonUp(1)){
    23.             RMDown = false;
    24.         }
    25.     }
    26.    
    27.     //Check if the mouse is over our PC/Laptop model on the map
    28.     public void OnMouseOver(){
    29.         if(Input.GetKeyDown(KeyCode.Mouse0)){ //If we left-click on it, open the GUI!
    30.             OnGUI();    //When you click on the object its attached to, the menu <i>should</i> popup!
    31.         }
    32.     }
    33.    
    34.     public OnGUI(){
    35.         int Countr = 0;    //A counter of our items
    36.         Rect ICON_REKT = new Rect((y*55)+10,(x*55)+10, 50, 50);
    37.         GUI.Box(new Rect(Screen.width/2, Screen.height/2, 300, 300), ""); //Creates our Window the items are show in
    38.         for(int y = 0; y < 5; y++){ //Create our vertical row (Think axis on a graph)
    39.             for(int x = 0; x < 5; x++){ //Create our horizontal row
    40.                 GUI.Box(ICON_REKT, ShopStock[Countr].ITM_Icon); //Creates some icons of whats for sale!
    41.                
    42.                 int StockID;
    43.                 /*Switch statement for item ids
    44.                 *You'll have to write a switch statement,
    45.                 *I'm lazy and its 2Am
    46.                 * Basically if(ITEM NAME == "Shoe"){
    47.                 *                     StockID = 1;
    48.                 *                }
    49.                 */
    50.                 //StockID = 1, 2, 3 etc
    51.                 if(ICON_REKT.Contains(Event.current.mousePosition) && RMDown){            //Check if the cursor is over a slot and if the Right mouse is being clicked
    52.                     CheckValue(StockID);
    53.                 }
    54.                
    55.                 Countr++; //Increment the counter by one each time the loop is completed!
    56.             }
    57.         }
    58.     }
    59.  
    60.     public void CheckValue(int id){
    61.         if(ITM_Cost > Cash){    //If the item costs more than the cash you have. You can't buy it.
    62.             Print("Sorry, you don't have enough to buy this!");
    63.         }else if(ITM_Cost <= Cash){    //If the item is equal to or less than the cash you have, You can buy it!
    64.             debug.Log("Before:" + Cash);        //Cash before transaction
    65.                 Cash -= ITM_Cost;                //Take the cost of the item from the cash. Probably redundant
    66.                 Player.Rem(ITM_Cost);            //Remove the amount of the item from your coins
    67.             debug.Log("After:" + Cash);            //Tell us how much cash we have left!
    68.             BASIC_INVENTORY.Instance.AddItem(ShopStock[id]);    //Interfaces with the BASIC_INVENTORY.CS class, Accesses the "AddItem" function
    69.                                                                 //Adds the item from the counter in the process
    70.         }
    71.     }
    72. }
    73.  

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerStats : MonoBehaviour {
    5.     public float Coins;
    6.     public float minCoins = 0;        //Cant have less than 0!
    7.     public float maxCoins = 100000000;    //Cant be more than.. what ever that is!
    8.    
    9.     public start(){
    10.         Coins = 0;            //Set our start coins at 0
    11.     }
    12.    
    13.     //Its simple to add more coins, call this class from an item, give it an onMouseOver + left mouse click check which then on pick up will add however many
    14.     //coins you want to your purse
    15.     public void AddCoins(int Add){
    16.         if (Coins < maxCoins) {
    17.                 Coins += Add;        //This adds the value of Add from Coins
    18.         }
    19.         if (Coins > maxCoins) {
    20.             Coins = maxCoins;        //If your coins ever get past the maximum reset it to the maximum so if max was 100 and you found 101 coins, you could only
    21.                                     //Ever carry 100 coins
    22.         }
    23.     }
    24.     public void RemCoins(int Rem){
    25.         if (Coins > minCoins) {
    26.                 Coins -= Rem;            //This takes away the value of Rem from Coins
    27.         }
    28.         if (Coins < minCoins) {        //If you ever lose more than the minimum it will be reset to 0;
    29.             Coins = minCoins;
    30.         }
    31.     }
    32. }