Search Unity

Very basic Slot, Inventor and Item..

Discussion in 'Scripting' started by MedalHellWay, May 24, 2018.

  1. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, so this is just basically the same idea from the other day then.

    You go through the inventory and when you find the first item that matches, you remove it. After that's done, you want to move any item that exists so it fills in the missing slot.

    You can accomplish that by looping through the inventory from the index of the remove item, towards the end (or until an empty is found), and move each one (SetItem, I think.).

    I don't know what get component has denied you means lol, sorry.
     
    MedalHellWay likes this.
  2. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Ok, the script "Test5" tells me this:

    Code (CSharp):
    1. int GetFreeIndex()
    2.     {
    3.         for(int i = 0; i < slots.Length; ++i)
    4.         {
    5.             if (slots[i].item == null) return i;
    6.         }
    7.         return -1;
    8.     }
    Counting the number of slots, if the slot is empty addItem (i) and then start the loop minus one turn...right?

    If I write...

    Code (CSharp):
    1. if (slots[i].item != null) return i;
    ...counting the number of slots, if the slot is full, removeItem(i) but the loop don't minus one turn... so what happens ????arghhhhh :confused:
     
  3. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    I'm trying this, I hope I can!

    Good idea :) I hope I understand how to do it :confused:

    When I try to go directly into the TestS01 script with "GetComponent" metod , I'm not allowed to...
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I'm not sure what you're saying here, sorry...

    Oh, okay.. ya you can't get that as a component. But you shouldn't need it. What were you trying to do/use?
     
  5. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    I tried to create a loop to determine the full slots by taking your example, but it stops at the first full slot ...
    (extract script)

    Code (CSharp):
    1. int GetFreeIndex()
    2.     {
    3.         for(int i = 0; i < slots.Length; ++i)
    4.         {
    5.             if (slots[i].item != null) return i;
    6.         }
    7.         return -1;
    8.     }


    I'm trying to get the nameItem part since I can't extract the name for the comparison in any way. I do not know why when I debug everything is ok, but then nothing happens ... I can't understand where I'm wrong ... With script "test6" get image and string name but then nothing works. I reviewed your scripts 200 times to understand the links, but obviously I get lost on the road...
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    So, if you had a TestSO1 variable, you can get the itemName directly..
    Code (csharp):
    1. TestSO1 anItem;
    2. // get the name?
    3. string someString = anItem.itemName;
    I'm not sure if that's what you meant. You don't have to compare by name, either. You could compare 'anItem' to 'someOtherItem' if you wanted to -- depends on what you need.

    When you see you want to find the full slots - do you mean you want to count up all the # of used slots?
    Or something else?
    If it's counting, you could do :
    Code (csharp):
    1. public int SlotsUsed() {
    2.    int cnt = 0;
    3.    for(int i = 0; i < slots.Length; ++i)
    4.    {
    5.       if (slots[i].item != null) ++cnt;
    6.    }
    7.    return cnt;
    8.  }
     
    MedalHellWay likes this.
  7. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks methos5k, I'll try with your advice :) I'll keep you up-to-date!!!!!
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, hope something I said helps. Sometimes I just don't understand your question(s), so it's hard to answer better.. Good luck :)
     
  9. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Basically I wanted to associate the name of the item with the name of the sprite to be deleted. Make a comparison between string name and sprite name to delete the slot.
    Your advice is to compare items ... do you have a basic example to show me?

    Thanks again!!
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ya, sure, it's just a comparison like any other. Use the '==' operator.

    Let's say you have this method in your inventory class.
    I am calling items 'GameItem' in the script. The example code I wrote before had silly numbers for every script, and it's not so easy to remember which was which (or have to look back at it*).
    Code (csharp):
    1.  // this method will return true/false based on whether it found & removed the item or not.
    2. public bool RemoveItem(GameItem item) {
    3.    for(int i = 0; i < slots.Length; ++i)
    4.    {
    5.       if (slots[i].item == item)
    6.       {
    7.           slots[i].Clear();
    8.           return true;
    9.    }
    10.    return false;
    11.  }
    12.  
    13. // the slots script 'Clear' might be something like:
    14. public void Clear() {
    15.    item = null;
    16.    // here, maybe you call 'UpdateSlot' instead of setting the image yourself.
    17.    GetComponent<Image>().sprite = null; // of course you should cache the 'Image' instead.
    18.   }
    So, I tried to comment it a little bit. Something like that I think can work for you.
     
    MedalHellWay likes this.
  11. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks methos5k! :) Already I wandered around a similar solution, but this is much clearer!

    Thank you so much for your patience and endless support for my silly questions, but I'm learning a lot from this discussion. :) :)

    I will try to find the right way, since I'm practically still with the construction of the game if I do not solve this blessed inventory first!

    Thanks again!!!!!
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Right, sure.. well, I'm glad it could help you some.

    Even if you get the inventory working okay, and it's not perfect, maybe you can put it aside for a little while and build up your game in other areas to make it more complete and practice in general. You can always come back to it. Sometimes looking at an issue with fresh eyes (after a break) can help, too. :)

    Take care.
     
    MedalHellWay likes this.
  13. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Ok, in the game all script inventory don't work! Arghhhhhhhhhh!!!! Why? Whyyy???? :( :( :(
    If I check the script with debug is all ok, but the image don't show in the canvas UI.... :( :( I call all the scripts using a macro interaction script attached to Player. Repeat, all debug check work well..

    I have transformed the inventory scripts into normal invokable functions so that they can be called within the player's Interact script (check collisions eliminated)....

    test.jpg
     
    Last edited: Jun 15, 2018
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, I am really not sure what happened.

    Can you create a small unity package with just the code you're talking about? Something simple just to show whatever issue you're having without having to enclose your entire game.

    You could post the unity package in this thread. Then, maybe myself or someone can look at it...

    If there's any way you can try to explain your issue & question more clearly in the thread, that might help also. :)
     
  15. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks again methos5k for your support!!

    Yes I attach the file game test (Attention there is a bit of confusion :) )
     

    Attached Files:

  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, the game looks kinda cool. But I was missing some prefabs..and that doesn't matter as much as I do not know what I should be looking for.

    I was hoping you could send a small package of just something you're having a problem with.

    I think maybe you want your raycast to check if the game object has 'TestInteractObject'. Then, you can interact/add that item. I can only see the key as one of such game objects with that script. You shouldn't need to compare the tag for the key object, for example.. just finding the TestInteractObject should be enough.

    Also note that you try to get the inventory script from the interacting object, rather than the script that does 'Interact' on the player. The inventory is on the player, so you should get it there. Maybe pass the inventory to the script, or return what you need for the inventory to the Interact script.

    Okay, so I tested that and it's working, I believe. However, there is another issue. After we talked about the inventory, I now see that your inventory "design" is pretty different. There is either just 1 inventory with 1 slot.
    You hadn't assigned the proper slot to the basic inventory manager. You had 2 empty, null slots.
    So, I referenced the key inventory slot in the inventory manager, then picked up the key, after using the above mentioned changes, and it works.

    That's a few things to change. :) I hope that made sense.
     
    MedalHellWay likes this.
  17. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    The packgage is too big for to post it here :D So I had to reduce it to the bone to present it...

    Yes. the package have the problem inside..

    I thought of inserting the image of the key into the inventory when the key is destroyed. So I thought of inserting it in the "Interact" script . Well, however, your advice :)

    I inserted the scripts in the same order as the test scene !!! So I was wrong in its inclusion ... hihihhihihi

    Nice, How can I try your advice and see if it works !! The slot is only one because I was doing a test to show!!!

    Thanks again @methos5k for valuable support!!!!!! Haaaargghhhh, how can I do without you ???? Hahahahahahah

    :)
     
  18. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    I've just imported this package as well. It is really quite big- too big for someone to dig through to locate a problem that is already a little unclear. :)

    Can you just create a new scene that contains only your inventory scripts including, at most, 2 items to put in the inventory. Then just hard code a small piece of driver code that demonstrates the problem you are having.

    Then export only that (very, very small) scene. You need to try and make this as easy as possible for someone to try and help you.
     
  19. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    @Doug_B !!! nice to read you :)

    The problem is simple. In package you've the folder script with a sub-folder called "Inventory". In this folder there are the scripts of the example of methos5k. The problem is when you grab the "Key", in the last CanvasUI the image don't is shown. Nothing problematic regarding the size of the package ;) In the scene test, all work well, in my game no..

    However, below I attack a compressed folder with a small video of my game in the design phase, to understand the mecchanica and what I'm looking for ...

    test.png
     

    Attached Files:

  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I wasn't sure what to add, but I read your previous post.

    Did you try to fix the parts I mentioned, using some suggestions I made?

    Did you reference the 1 inventory slot in the inventory manager (and set the slots count to 1 in there, too)?

    Did you try to send a reference to the inventory to the script so it could call the inventory AddItem , or return the Item from the method to the interact script (and the interact script adds the item)?

    Those were the 2 major things to get it working. The other part I wrote about checking for the component just sounds like a good idea to me, but it's not necessary to actually make it work. Maybe you could try that part after it's working, if you want. :)
     
    MedalHellWay likes this.
  21. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Not yet methos5k !!!
    Today I worked all day and I do not have my reference pc to try out your tips! However I imagine that everything already works :D
    I reply to @Doug_B because he asked me to do another test scene, but the attached package is sufficient ;) And last, the video posted was just to show what I plan to do. I imagine that built the basic inventory I can safely continue (at least I hope)..

    Thanks again!
     
  22. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    No man, don't work. It's me who did not understand how to do it?? So I insert the scripts as follows:

    Game object "Player" -> "Interact" script and "BasicInventoryManager" script
    Game Object "Key01" -> "TestInteractObject" script
    GameObject "Slot" -> "TestSlot" script

    On the test scene everything works perfectly, on the scene of my job no.

    @methos5k I did not understand this part:

    "Did you try to send a reference to the inventory to the script so it could call the inventory AddItem , or return the Item from the method to the interact script (and the interact script adds the item)?"

    a practical example?
     
  23. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, so here are the two options I was thinking of. This code won't be exact, but hopefully it's enough to show you the idea..
    Option #1 (send the inventory reference):
    Code (csharp):
    1. // Interact Script
    2. [SerializeField]
    3. BasicInventoryManager inventoryManager; // drag n drop in the inspector
    4.  
    5. public void Interact() {
    6.    // some code that let you find the key
    7.    key.GetComponent<TestInteractObject>().IntObj(inventoryManager);
    8.  }
    9.  
    10. // on the key (TestInteractObject)
    11. public void IntObj(BasicInventoryManager invManager) {
    12.     invManager.AddItem(theItem);
    13.  }
    The other idea is like this (return object from TestInteractObject to Interact script):
    Code (csharp):
    1. [SerializeField]
    2. BasicInventoryManager inventoryManager; // drag n drop
    3.  
    4. public void Interact() {
    5.    // some code to get the reference to the key
    6.    ItemObj obj = key.GetComponent<TestInteractObject>().IntObj();
    7.    inventoryManager.AddItem(obj);
    8.  }
    9.  
    10. // script: TestInteractObject
    11. public ItemObj IntObj() {
    12.    return theItem;
    13.  }
    So, I didn't remember the names of all of your scripts/classes. :)
    So, if for any reason it's not obvious, then:
    IntObj -> Method on the test interact object script
    ItemObj -> the scriptable object 'item' type.

    Does that make sense like that?
     
    MedalHellWay likes this.
  24. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Yes, everything makes sense.... but my question is, because in the test file it works and in my game no ?? Mysteries of the universe I imagine :D :D :D
    Now I try these your solutions and see where they take me! :) Thank you again methos!!
     
  25. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    If you're referring to how I said it's working, then that's because I actually made those changes and then it worked.
    If you meant something else, like your own test file.. I'm not sure.

    Anyways, yes, test the examples I post. Just pick one of them.
    Also make sure you referencing the slot image to the basic inventory manager as I noted before. Set that array/list size to 1, also. (when I looked at your project before the size was 2, but the image/slot wasn't referenced at all).
     
    MedalHellWay likes this.
  26. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Ok, the Option #1 work well :) Later I show you a test !!
     
  27. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Video test of simple image inventory UI :)

    P.S: Now there is another problem that is not connected with the UI objects system. The canvas of the numbers of the pad does not work anymore, before everything was ok because everything worked well (script, reference etc.) .. :( :( (last seconds of video) These things of Unity really make me angry ....
     

    Attached Files:

  28. j04milan

    j04milan

    Joined:
    Feb 6, 2019
    Posts:
    44
    Wowwww a month of support on someone's issue for free that is impressive.
    I'm right now going through the same issue it's good to know there are great people out there able to help
     
    MedalHellWay likes this.