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.
You will need three things:
- Some knowledge of code
- A GUI System
- 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:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class BASIC_C_SHARP_INVENTORY : MonoBehaviour {
public List<SlotDef> Inventory = new List<SlotDef>();
public bool InventoryOpen;
public bool LMDown;
public void Start(){
InventoryOpen = false;
}
public void Update(){
if (Input.GetMouseButtonDown(0)) {
LMDown = true;
}else if(Input.GetMouseButtonUp(0)){
LMDown = false;
}
}
//This adds the item to your inventory
public void AddItem(ItemDefine iDef){
bool Finished = false;
//Make sure there are 25 slots in the inventory
for(int x = 0; x < 25; x++){
if(Inventory[x].iDef.ITM_Icon == null){
if(!Finished){
Inventory[x].iDef = iDef;
Finished = true;
}
}
}
}
public OnGUI(){
int ITEM = 0;
if(InventoryOpen){ //Inventory is open, Show the GUI!
for(int y = 0; y < 5; y++){
for(int x = 0; x < 5; x++){
Inventory [ITEM].Slot.x = (x * 55) + 10; // Position of icon width
Inventory [ITEM].Slot.y = (y * 55) + 10; // Position of icon height
Inventory [ITEM].Slot.width = 50; // icon width
Inventory [ITEM].Slot.height = 50; // icon height
//Create our Slot, show the icon too
GUI.Box (Inventory [ITEM].Slot, new GUIContent (Inventory [ITEM].iDef.ITM_Icon, Inventory [ITEM].iDef.ITM_Name));
ITEM++;
}
}
}
}
[System.Serializable]
public class SlotDef{
public ItemDefine iDef;
public Rect Slot;
}
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.
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.
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…
BASIC_INVENTORY.CS
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //Adds the ability to use LinkedLists
public class BASIC_INVENTORY : MonoBehaviour {
//This creates a list of objects that follow the style in the SlotDefine class below.
//It makes all items of type .Item, which can inherit variables from ItemDefine such as ITM_Icon, ITM_Name etc
public List<SlotDefine> Inventory = new List<SlotDefine>();
public static BASIC_INVENTORY Instance; //Create an "Instance" of the inventory transaction.
public Start(){
Instance = this;
}
//This is a function that just stores items in a list to be used by another function.
public void AddItem(ItemDefine AOR_ITM){
bool Finished = false; //Keep adding items until finished, Because we're not finished, Finished is always false until we set it
for(int x = 0; x < 25; x++){ //Set the amount of items we can add to our inventory. in this case 25
if(Inventory[x].Item.ITM_Icon == null){ //If the Inventory slot is empty (null)
if(!Finished){ //And we're not finished
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
Finished = true; //We're finished adding the item!
}
}
}
}
}
[System.Serializable]
public class SlotDefine{
public ItemDefine Item; //Creates an interface to the ItemDefine class.
public Rect Slot; //Creates a Rectangle called Slot to hold our variables for height/width of Icons
}
ItemDefine.cs
using UnityEngine;
using System.Collections;
[System.Serializable]
public class ItemDefine {
//These variables are declared in the inspector
public string ITM_Name; //The items name
public int ITM_Count; //For stacking Items
public int ITM_Cost; //How many coins the item will cost
public Texture2d ITM_Icon; //The icon for our "Website" and Inventory
public Item_type ITM_Type; //An item from our list below
//A selective list for the inspector, with a dropdown menu.
public enum Item_type{
Weapon,
Ammo,
Clothing,
Food
}
}
ComputerStore.CS
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ComputerStore : MonoBehaviour{
//This script must be attached to an object you want to click on i.e a model of a PC/Laptop
public List<ItemDefine> ShopStock = new List<ItemDefine>(); //A list to store our purchasable items in
public PlayerStats Player; //Interface with the PlayerStats class!
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
public bool RMDown; //Check if the Left mouse button is down
public Update(){
Cash == Player.Coins; //Our integer "Cash" is now equal to whatever value the player has in their purse
//Check if the Right mouse button is being pressed!
if (Input.GetMouseButtonDown(1)) {
RMDown = true;
}else if(Input.GetMouseButtonUp(1)){
RMDown = false;
}
}
//Check if the mouse is over our PC/Laptop model on the map
public void OnMouseOver(){
if(Input.GetKeyDown(KeyCode.Mouse0)){ //If we left-click on it, open the GUI!
OnGUI(); //When you click on the object its attached to, the menu <i>should</i> popup!
}
}
public OnGUI(){
int Countr = 0; //A counter of our items
Rect ICON_REKT = new Rect((y*55)+10,(x*55)+10, 50, 50);
GUI.Box(new Rect(Screen.width/2, Screen.height/2, 300, 300), ""); //Creates our Window the items are show in
for(int y = 0; y < 5; y++){ //Create our vertical row (Think axis on a graph)
for(int x = 0; x < 5; x++){ //Create our horizontal row
GUI.Box(ICON_REKT, ShopStock[Countr].ITM_Icon); //Creates some icons of whats for sale!
int StockID;
/*Switch statement for item ids
*You'll have to write a switch statement,
*I'm lazy and its 2Am
* Basically if(ITEM NAME == "Shoe"){
* StockID = 1;
* }
*/
//StockID = 1, 2, 3 etc
if(ICON_REKT.Contains(Event.current.mousePosition) && RMDown){ //Check if the cursor is over a slot and if the Right mouse is being clicked
CheckValue(StockID);
}
Countr++; //Increment the counter by one each time the loop is completed!
}
}
}
public void CheckValue(int id){
if(ITM_Cost > Cash){ //If the item costs more than the cash you have. You can't buy it.
Print("Sorry, you don't have enough to buy this!");
}else if(ITM_Cost <= Cash){ //If the item is equal to or less than the cash you have, You can buy it!
debug.Log("Before:" + Cash); //Cash before transaction
Cash -= ITM_Cost; //Take the cost of the item from the cash. Probably redundant
Player.Rem(ITM_Cost); //Remove the amount of the item from your coins
debug.Log("After:" + Cash); //Tell us how much cash we have left!
BASIC_INVENTORY.Instance.AddItem(ShopStock[id]); //Interfaces with the BASIC_INVENTORY.CS class, Accesses the "AddItem" function
//Adds the item from the counter in the process
}
}
}
PlayerStats.cs
using UnityEngine;
using System.Collections;
public class PlayerStats : MonoBehaviour {
public float Coins;
public float minCoins = 0; //Cant have less than 0!
public float maxCoins = 100000000; //Cant be more than.. what ever that is!
public start(){
Coins = 0; //Set our start coins at 0
}
//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
//coins you want to your purse
public void AddCoins(int Add){
if (Coins < maxCoins) {
Coins += Add; //This adds the value of Add from Coins
}
if (Coins > maxCoins) {
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
//Ever carry 100 coins
}
}
public void RemCoins(int Rem){
if (Coins > minCoins) {
Coins -= Rem; //This takes away the value of Rem from Coins
}
if (Coins < minCoins) { //If you ever lose more than the minimum it will be reset to 0;
Coins = minCoins;
}
}
}