- Home /
Crafting System Help
i need some help i came up with a crafting script that requires a craft table type thing and i need help turning this into a craft anywhere type thing also my current script dosnt work how i want it to since it only needs one of the reguired objects please help HERE IS THE SCRIPT:
pragma strict
var tool : GameObject;
function Start () { tool.SetActive(false); }
function OnTriggerEnter ( Col : Collider) { if(Col.tag == "Rock" ) { tool.SetActive(true); }
else
if(Col.tag == "Stone" )
{
tool.SetActive(true);
}
}
Answer by YoungDeveloper · Dec 08, 2013 at 07:39 PM
Every item in every game is located in some sort of list and has an unique index. If you follow these rules, creating any item manipulation should be piece of cake. What you could do is create possible recipe array.
[ExecuteInEditMode]
public class Recipes : MonoBehaviour {
[System.Serializable] //So you could input stuff from inspector
public class Item{
public int [] ingredients; //set array size and item indexes in inspector
public int resultItem; //item index which is crafted
}
public Item [] recipeList;
}
For this to work you should have other item lists, like for this example, lets have list with such information:
(array index) | (name)
1 apple
2 tomato
3 bread
4 orange
5 cookie
6 weirdMeal
In recipes inspector overall recipes list set to lets say 10, so now you have 10 possible variations. In first slot set ingredients array size to 3, which will mean that you will need 3 items to cook some item, and in result item set which item will be crafted. For example:
1;3;4 and result item 6
All you need now is script which searches/compares (in craft table) inputted indexes to this list, because item can be placed in different order, like 4;1;3. And if it finds recipe, run inventory check if that item can be placed in inventory (in you have such), if it succeeds, remove items inside craft table and spawn item 6, that is "weirdMeal" in your inventory.
Of course combinations can be stored in separate arrays..
i dont quit understand what you mean hope i dont sound like a total idiot. im used to javascript so i dont quit understand c#. seems good will use just need more of an explanation so i can understand better
To craft something you first must have those "items", and to identify every item in your game, every item should have a unique number. And to grant every item with a number you can store them in an array, similar as i showed before. When every item is saved in an array you can do with these items whatever you want.
The crafting system i mentioned just reads and searches through those numbers (indexes).
He just told you that, in an admirable display of leading-a-horse-to-water. You add them to an array or list. The numbers are the array index. If "array index" doesn't make sense to you, you don't need to be working on a crafting system, you need to be working on learning basic program$$anonymous$$g concepts. You don't have to assign them- although if you did want to manually assign numbers to objects for some reason, you would do so with a Dictionary.< GameObject, int >. )
@$$anonymous$$iloblargh Yeah, you are right, i pretty much wasted my time trying to explain these things, but i was hoping it would make sense for him. Person who asked the question surely should do some RTF$$anonymous$$ before doing anything near crafting systems.
Answer by HuskyPanda213 · Mar 09, 2014 at 04:48 AM
I have a working system. It is in c#
public void Craft(Craftable recipe){
bool b = CheckItems (recipe.InputItems);
if(b){//Craft...
}
}
public bool CheckItems(List<Item> check) {
List<Item> it = new List<Item> (check);
foreach(Item inventoryItem in MyItems) {
if (it.Contains(inventoryItem)) {
it.Remove(inventoryItem);
if(it.Count == 0) {
return true;
}
}
}
return false;
}
Item, and craftable...
using UnityEngine;
using System.Collections;
[System.Serializable]
public class Item {
public string itname;
public float itvalue;
public float itdur;
public string ittype;
public Texture2D iticon;
public override bool Equals(object other) {
if (other.GetType() != typeof(Item)) return false;
if (((Item)other).itname == this.itname && ((Item)other).ittype == this.ittype) {
return true;
}
return false;
//return ((Item)other).itname == this.itname;
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Craftable {
public List<Item> InputItems = new List<Item>();
public List<Item> Outputs = new List<Item>();
public bool RequiresWorkBench;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613123721im_/https://answers.unity.com/themes/thub/images/avi.jpg)