Hello, I am facing a problem in my craft system, what it should do and: Show the crafts in the List: Functional; By clicking on the buttons for the craft, can test whether crafta it: Not Functional; So the problem, and that directly, it already says I can not crafting, and if I click the craft button, it keeps saying I can not do it. Here the script of the craft: Code (JavaScript): function OnGUI () { var CanCraft: boolean; if(OpenCraftMenu == true) { for(var i:int=0;i<Crafts.Length;i++) { if(GUI.Button(Rect(500,50*i,50,50),Inventory.Items[Crafts[i].ResultItem].Icon)) { CanCraft = TestRecipe(i); if(CanCraft == true) { for(var it:int=0;it<Crafts[i].Recipe.Length;i++) { Inventory.InvItems.RemoveAt(i); } Inventory.InvItems.Add(Crafts[i].ResultItem); print("The Item "+Crafts[i].Name+" Has Crafted!!!"); } } } if(CanCraft == false) { GUI.Box(Rect(Screen.width/2,Screen.height/2,100,30),"You cannot craft this item!"); } } } public function TestRecipe(RecipeIndex: int) { var Ind: int; var MaxInd: int; for(var i:int=0;i<Crafts[RecipeIndex].Recipe.Length;i++) { MaxInd = Crafts[RecipeIndex].Recipe.Length; if(Inventory.InvItems[i] == Crafts[RecipeIndex].Recipe[i]) { Ind += 1; } } if(Ind == MaxInd) { return true; } } So that and so part of the code, but it is enough to understand, you could help me with this problem? Ps: The script of the inventory itself, takes a class called Item, and a variable cahamada Items, which is a list / of items database, and a variable int [], which are the existing items in it, which is used to create the inventory.
your "if(CanCraft == false)" statement is after you test the recipe, so it looks like that function is always saying that you can't craft. Try this: Code (Javascript): function OnGUI () { var CanCraft: boolean; if(OpenCraftMenu == true) { for(var i:int=0;i<Crafts.Length;i++) { if(GUI.Button(Rect(500,50*i,50,50),Inventory.Items[Crafts[i].ResultItem].Icon)) { CanCraft = TestRecipe(i); if(CanCraft == true) { for(var it:int=0;it<Crafts[i].Recipe.Length;i++) { Inventory.InvItems.RemoveAt(i); } Inventory.InvItems.Add(Crafts[i].ResultItem); print("The Item "+Crafts[i].Name+" Has Crafted!!!"); } else if(CanCraft == false) { GUI.Box(Rect(Screen.width/2,Screen.height/2,100,30),"You cannot craft this item!"); } } } } }
Friend, I believe I have solved part of the problem was not what you said, the problem and the TestRepice () I test the items sequentially, there they have to be in order in the inventory, know how to make it test the items individually?
You can try to do a for each on the recipe and check if inventory contains that item. I can't write JS if my life depended on it, but this pseudo-code should help you get the idea: Code (CSharp): for each (ingredient in Recipe) { if (Not Inventory.Contains(ingredient)) { canCraft = false; } } if (canCraft) { craftItem; remove each ingredient from inventory; }
Friend, thank you for your attention, the problem was corrected, I decided to make a go and test the individual items, now if you can answer this other post (this in C #), I thank you very much, I do not know if a bug Unity or some programming error, can you help me? Link=http://forum.unity3d.com/threads/bug-of-unity-or-not.396634/