Search Unity

Inventory Craft System Help

Discussion in 'Scripting' started by Rhyusaky, Apr 3, 2016.

  1. Rhyusaky

    Rhyusaky

    Joined:
    Jan 22, 2016
    Posts:
    25
    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):
    1. function OnGUI () {
    2. var CanCraft: boolean;
    3. if(OpenCraftMenu == true) {
    4. for(var i:int=0;i<Crafts.Length;i++) {
    5. if(GUI.Button(Rect(500,50*i,50,50),Inventory.Items[Crafts[i].ResultItem].Icon)) {
    6. CanCraft = TestRecipe(i);
    7. if(CanCraft == true) {
    8. for(var it:int=0;it<Crafts[i].Recipe.Length;i++) {
    9. Inventory.InvItems.RemoveAt(i);
    10. }
    11. Inventory.InvItems.Add(Crafts[i].ResultItem);
    12. print("The Item "+Crafts[i].Name+" Has Crafted!!!");
    13. }
    14. }
    15. }
    16. if(CanCraft == false) {
    17. GUI.Box(Rect(Screen.width/2,Screen.height/2,100,30),"You cannot craft this item!");
    18. }
    19. }
    20. }
    21. public function TestRecipe(RecipeIndex: int) {
    22. var Ind: int;
    23. var MaxInd: int;
    24. for(var i:int=0;i<Crafts[RecipeIndex].Recipe.Length;i++)
    25.    {
    26.    MaxInd = Crafts[RecipeIndex].Recipe.Length;
    27.    if(Inventory.InvItems[i] == Crafts[RecipeIndex].Recipe[i]) {
    28.    Ind += 1;
    29.    }
    30.    }
    31.    if(Ind == MaxInd) {
    32.    return true;
    33.    }
    34. }
    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.
     
  2. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    514
    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):
    1. function OnGUI () {
    2.     var CanCraft: boolean;
    3.     if(OpenCraftMenu == true) {
    4.         for(var i:int=0;i<Crafts.Length;i++) {
    5.             if(GUI.Button(Rect(500,50*i,50,50),Inventory.Items[Crafts[i].ResultItem].Icon)) {
    6.                 CanCraft = TestRecipe(i);
    7.                 if(CanCraft == true) {
    8.                     for(var it:int=0;it<Crafts[i].Recipe.Length;i++) {
    9.                         Inventory.InvItems.RemoveAt(i);
    10.                     }
    11.                     Inventory.InvItems.Add(Crafts[i].ResultItem);
    12.                     print("The Item "+Crafts[i].Name+" Has Crafted!!!");
    13.                 }
    14.                 else if(CanCraft == false) {
    15.                     GUI.Box(Rect(Screen.width/2,Screen.height/2,100,30),"You cannot craft this item!");
    16.                 }
    17.             }
    18.         }
    19.     }
    20. }
     
    Last edited: Apr 5, 2016
    Rhyusaky likes this.
  3. Rhyusaky

    Rhyusaky

    Joined:
    Jan 22, 2016
    Posts:
    25
    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?
     
  4. Kalladystine

    Kalladystine

    Joined:
    Jan 12, 2015
    Posts:
    227
    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):
    1. for each (ingredient in Recipe)
    2. {
    3.     if (Not Inventory.Contains(ingredient))
    4.     {
    5.              canCraft = false;
    6.     }
    7. }
    8.  
    9. if (canCraft)
    10. {
    11.    craftItem;
    12.    remove each ingredient from inventory;
    13. }
     
  5. Rhyusaky

    Rhyusaky

    Joined:
    Jan 22, 2016
    Posts:
    25
    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/