Search Unity

Bag system and object placement

Discussion in 'Scripting' started by Dolmek, May 10, 2018.

  1. Dolmek

    Dolmek

    Joined:
    Jun 28, 2015
    Posts:
    12
    Hi guys, I'm making a videogame just for me and my wife and I'm stuck. I wanna do the following things:
    1) I wanna make a bag system similar to World of Warcraft, where in your toolbar you have 4 slots for bag and each bags have n slots. Can you link me some tutorial about how make it?
    2) I wanna make a system similar to Minecraft. I don't want to build the entire world, I just want to place on a "world grid" an object on the ground, let say for example a wall or a floor. There is any tutorial for that?

    My version of unity is 2017 and looking for tutorial and help about how to make it.


    P.S. the game is in single player.
     
    Last edited: May 10, 2018
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    this question reads very much like "please google this for me and send me the results", which isn't usually well received. The scripting forum is meant to be more "I've ran into this specific error/issue with my code <insert code> <insert error> can someone see something I've missed?" etc.
     
  3. Dolmek

    Dolmek

    Joined:
    Jun 28, 2015
    Posts:
    12
    So is my mistake. I searched a lot in the net but I didn't found anything useful. And I made my question in the wrong section.
     
  4. Dolmek

    Dolmek

    Joined:
    Jun 28, 2015
    Posts:
    12
    Where should I post this question?
    I don't want ppl telling me the script to copy and paste, I want to see a tutorial where I can learn how to make it.
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I've never played either of those games, but the first one sounds like you'd like to know how to build an inventory.. just that there are 4 of them. If you can learn about how to make one, expanding that shouldn't be too difficult.
    The second one just sounds like "I'd like to place an object in the world". I have to imagine that that question's been asked before.

    You should be able to look into those things and find some answers.

    Consider thinking about each task as smaller components, too. In a good tutorial, I'm sure it's covered, but for example with an inventory, you need to keep track of the items you have, display them, drag/drop, etc.. Solving each of those is much simpler, and you may be able to assemble what you seek if you think of it that way.

    I agree, though, it's better to use the forums to help with code/questions after you've looked into a topic. :)
     
  6. publiweb

    publiweb

    Joined:
    May 10, 2018
    Posts:
    14
    What you need to learn is called:
    polymorphism

    My code is more Java based but it is similar in C# (the below code will be very basic and "universal") just to make you an idea.

    1.
    Create a class called "Item"
    a) Inside "Item class" create a String variable called name.
    b) Inside "Item class" create a Integer variable called quantity.
    c) Inside "Item class" create a Constructor to set the data while creating a new class of this object.
    Code (CSharp):
    1. public class Item {
    2.  
    3. int id;
    4. String name;
    5. int quantity;
    6.  
    7. public Item() {
    8. this.id = -1;
    9. this.name = "nothing";
    10. this.quantity = 0;
    11. }
    12.  
    13. public Item(int idB, String nameB, int quantityB) {
    14. this.id = idB;
    15. this.name = nameB;
    16. this.quantity = quantityB;
    17. }
    18.  
    19.  
    2. Create a class called "Bag"
    a) Inside "Bag class" put an array object of Item class
    b) create a Constructor to set the data while creating a new class of this object.

    Code (CSharp):
    1. public class Bag {
    2.  
    3. Item[] itemsInBag;
    4. int size;
    5. int itemsCount;
    6.  
    7. public Bag(int size) {
    8. this.itemsInBag = new Item[size];
    9. this.size = items.length;
    10. this.itemsCount= 0;
    11. }
    12.  
    13. public void addItem(int id, String name, int quantity) {
    14. if (itemsCount < size) {
    15. this.itemsInBag[itemsCount++] = new Item(id, name, quantity);
    16. // a code to show the item in the graphical interface in game (render the item in the "bag").
    17. } else {
    18. //show a error message that you cant add more items in bag.
    19. }
    20. }
    21.  
    22. public void removeItem(int idLookingFor) {
    23. for (int i = 0; i < size; i++)
    24.    {
    25.       if (this.itemsInBag[i].id == idLookingFor) {
    26.            this.itemsInBag[i] = null;
    27. //a code to refresh the graphical interface in game. (to show that the item disapeared) // to show the actual items in bag is the same.
    28. return;
    29.       }
    30.    }
    31. //if not found then you have to show a message that you didnt found the item id.
    32. }
    33.  
    34. }
    35.  
    36.  
    3. When you create a new bag object you have to set the size of the bag and then you can add the items you like to.

    Code (CSharp):
    1. Bag aNewBag = new Bag(4); //creating a bag with 4 spaces.
    2.  
    3. aNewBag.addItem(1, "chicken", 1); //adding an item in the bag by the id, name, quantity.
    4.  
    5.