I have created an inventory system in js using dictionary definitions and guis but I cant figure out how to work this in with a crafting system, so if you know any good tutorials or over people that have a working crafting system plz link it …
I have put the script below for the inventory so you have an idea of what I am using, I have also placed an image below too show how I want the inventory to work.
//----------
var customSkin : GUISkin;
var Player : Transform;
var weaponHolder : Transform;
var playerCamera : Camera;
static var itemNum : int;
static var gotItem : boolean;
static var usingPick : boolean;
static var inInventory : boolean;
static var stackItem : boolean;
static var items = new System.Collections.Generic.Dictionary.<String,GameObject>();
//Create the dictionary full of a name and a GameObject associated with it.
static var curItem : System.Collections.Generic.KeyValuePair.<String, GameObject>;
static var curItemKey;
static var curWeapon : GameObject;
static var destroyWeapon : boolean;
//This will store the current item.
public var buttonHeight = 50;
public var width = 100;
function AddItemToInventory (item : GameObject) {
items.Add(item.name, item);
itemNum++;
Destroy(item);
}
function OnGUI() {
var curIndex = 0;
GUI.skin = customSkin;
GUI.BeginGroup (new Rect (400, 500, 400, 120));
for(var item : System.Collections.Generic.KeyValuePair.<String, GameObject> in items) {
//iterate through all the items in the dictionary.
var rect : Rect = new Rect((width+2) * curIndex, 0, width, buttonHeight);
var customStyle : GUIStyle;
var itemModel : GameObject;
var itemIndex : int;
var btnToggled : boolean;
var crafting : boolean;
var stacking = GameObject.Find(item.Key+("_model")).GetComponent(stacking);
itemIndex = curIndex;
GUI.skin = customSkin;
customStyle = customSkin.GetStyle("icon_"+item.Key);
btnToggled = GUI.Toggle(rect, btnToggled, curIndex+1+") x"+stacking.stackAmount, customStyle);
if(Event.current.Equals (Event.KeyboardEvent (curIndex+1+""))) {
curItem = item;
curItemKey = curItem.Key;
}
if (curItem.Key == item.Key){
itemModel = GameObject.Find(curItem.Key+"_model");
btnToggled = true;
GUI.Toggle(rect, btnToggled, curIndex+1+") x"+stacking.stackAmount, customStyle);
CreateWeapon(itemModel);
}else{
btnToggled = false;
GUI.Toggle(rect, btnToggled, curIndex+1+") x"+stacking.stackAmount, customStyle);
}
curIndex++;
}
GUI.EndGroup();
}
function CreateWeapon(weaponModel : GameObject){
if (curWeapon){
Destroy(curWeapon);
}
curWeapon = Instantiate(weaponModel,weaponHolder.position,weaponHolder.rotation);
curWeapon.transform.parent = weaponHolder.transform;
gotItem = true;
}
function Update(){
if(curWeapon){
var stacking = GameObject.Find(curItem.Key+("_model")).GetComponent(stacking);
}
if (itemNum < 0){
itemNum = 0;
}
if(gotItem && Input.GetKeyUp("q")){
if(stacking.stackAmount >1){
stacking.stackAmount--;
ObjNew(curWeapon);
}else{
stacking.stackAmount--;
ObjNew(curWeapon);
itemNum--;
RemoveItem(curItem);
destroyWeapon = true;
gotItem = false;
}
if (curWeapon && Input.GetMouseButton(0)){
swingWeapon(curWeapon);
}
}
if(destroyWeapon){
Destroy(curWeapon);
}
if(!curWeapon){
destroyWeapon = false;
}
}
function ObjNew(newItem:GameObject){
var object = Instantiate(newItem,weaponHolder.position,weaponHolder.rotation);
object.name = curItem.Key;
object.transform.parent = null;
object.AddComponent(Rigidbody);
object.AddComponent(BoxCollider);
object.AddComponent(Interaction);
}
function swingWeapon(weapon : GameObject){
weapon.animation.Play("use");
}
function RemoveItem (item : System.Collections.Generic.KeyValuePair.<String, GameObject>) {
yield WaitForEndOfFrame;
items.Remove(item.Key);
}
i realise it is a lot of code but the main bit us under the OnGui function, thanks in advance for the help and I realise I am asking a lot, so I really appreciate the help
[829-Crafting+Image.jpg|829]
the idea is that the player will put items onto the crafting table, say a log and an axe head and make a weak axe, but I also want it so that if they put a log, axe head and another log, it would show everything the could be made and when they select it the resources that it requires disappear. so in the image on the left thats where the items would be placed and on the right that would show the list of what they can make
– anon77680935Are you familiar with the minecraft crafting table? Usually it's better to require the user to put the ingredients in a certain pattern. This allows much more combinations. Like @jacek-wesolowski said some kind of use-case would be helpful how you imagine the system should work. How does a crafting recipe look like? Do you want a pattern or not. How do you identify an item. By some kind of ID or via name?
– Bunny83btw. Usually when you use classes from a namespace frequently you should import that namespace to avoid those long classnames. I don't use UnityScript, but it should be: import System.Collections.Generic; at the top. In C# it's using System.Collections.Generic;
– Bunny83With the crafting system I want it like mine craft where you interact with the table to bring up the crafting UI, but more like terraria for the crafting where it shows you a list of items that you can make with your current items, but you have to place items onto the crafting UI first for it too show you what can be made. Its this way because some recipes produce multiple items. also thanks for the coding advice as I am a bit of a noob to it.
– anon77680935