- Home /
Help making premade Items for farming game
Hi, I've been trying to make items for my farming simulation game (such as seeds, tools etc) but I've hit a snag when trying to make pre-made items the player can get. Originally, the Item class inherited from MonoBehaviour and attached to prefabs, which I could then drag into the Inventory class in the inspector of whatever would contain the item (e.g. a shop).
However, I ran into problems creating a new instance of the object (MonoBevahiour doesn't like using 'new Item()') and copying the prefab would create problems with the 'amount' variable, where every item copied from the same prefab would have the same value for amount. Uninheriting Monobehaviour from the Item class allows me to create a new Instance of the Item class, but I can no longer attach the Item class to a prefab.
Is there a way I can create a list of Items, which could then be assigned in the Inventory of whatever would have the Item? Or is there a way I can make a new Instance of a prefab (Item class inheriting from MonoBehaviour), with each copy of an item being independent to another. The classes I am using are below.
class Item //Item Class
{
var ID : int;
var price : int;
var stackability : int;
var image : Texture2D;
var amount : int = 1;
var selected : boolean = false;
var worldObject : GameObject;
function PassItem(newItem : Item)
{
price = newItem.price;
stackability = newItem.stackability;
image = newItem.image;
worldObject = newItem.worldObject;
selected = false;
}
}
//Inventory Class
import System.Collections.Generic;
var inventoryItems : Item[];
var size : int;
function Awake()
{
if (size != 0)
inventoryItems = new Array(size);
}
function AddItem(newItem : Item) : boolean
{
if (CheckItem(newItem)) return true;
else if (inventoryItems.Count == size) return false;
else
{
for (var i = 0; i < inventoryItems.length; i++)
{
if( inventoryItems[i] == null )
{
var item = new Item();
item.PassItem(newItem);
inventoryItems[i] = item;
inventoryItems[i].amount = 1;
return true;
}
}
}
return false;
}
function CheckItem(desiredObject : Item)
{
for (var i = 0; i < inventoryItems.length; i++)
{
if (inventoryItems[i] != null)
{
if (inventoryItems[i].ID == desiredObject.ID)
{
if(inventoryItems[i].amount < desiredObject.stackability)
{
inventoryItems[i].amount += 1;
return true;
}
}
}
if (i == inventoryItems.length - 1)
return false;
}
return false;
}
Answer by Nachtmahr · Apr 04, 2013 at 03:21 PM
[Serialize]
public class Item{
....
}
public class Inventory : MonoBehaviour{
public Item[] items;
...
}
;P Try it if you dont have any Complex Types it will Serialize very Well.
Edit: Sry I see you use JS this is the C# Solution. I dont know how Serialize on JS works.
Thanks anyway. I was looking into Serializing by inheriting the Item Class from Object/ScriptableObject, but I'm not sure on where to go from there.
Answer by Dezzy · Apr 04, 2013 at 06:43 PM
I think I have found the answer. I made the Item Class inherit from ScriptableObject and wrote a custom EditorWindow script that makes a new instance of the Item Class and creates an asset of it in the Project directory. This way allows me to drag and drop the ScriptableObject into anywhere in the scene that takes an Item object.
If anyone wants the script for this, the JavaScript version is below (shouldn't be too hard to convert to C#).
import UnityEngine;
import UnityEditor;
import System.Collections;
public class ItemDatabaseManager extends EditorWindow
{
@MenuItem("RPG/Item Database Manager")
static function Init()
{
var window:ItemDatabaseManager = EditorWindow.CreateInstance(ItemDatabaseManager);
window.Show();
}
var newItemName:String = "";
var newItemID:int = 0;
var newItemPrice:int = 0;
var newItemStackability:int = 1;
var newItemImage:Texture2D;
var newItemAmount:int = 1;
var newItemPrefab:GameObject;
function OnGUI()
{
newItemName = EditorGUILayout.TextField("Name: ", newItemName);
newItemID = EditorGUILayout.IntField("ID: ", newItemID);
newItemPrice = EditorGUILayout.IntField("Price: ", newItemPrice);
newItemStackability = EditorGUILayout.IntField("Stackability: ", newItemStackability);
newItemPrefab = EditorGUILayout.ObjectField("Prefab: ", newItemPrefab, GameObject);
newItemImage = EditorGUILayout.ObjectField("Image: ", newItemImage, Texture2D);
if (GUILayout.Button("Add New Item"))
{
var newItem : Item = ScriptableObject.CreateInstance(Item);
newItem.ID = newItemID;
newItem.price = newItemPrice;
newItem.stackability = newItemStackability;
newItem.worldObject = newItemPrefab;
newItem.image = newItemImage;
AssetDatabase.CreateAsset(newItem, "Assets/ItemDatabase/" + newItemName +".asset");
AssetDatabase.SaveAssets();
}
}
}
Your answer
Follow this Question
Related Questions
Adding Item to an Inventory. 1 Answer
Unable to usem item from inventory 1 Answer
Invoking Effects of (Random) Items (C#) 0 Answers
[C#]Inventory script help. 3 Answers