Announcement

Collapse
No announcement yet.

Inventory System / Polymorphism

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Inventory System / Polymorphism

    Hey all, I am trying to create an inventory system but wondering how is the best way to go about this, I was trying to structure blueprints but I can't find a way to polymorph them. basically I want a blueprint 'Inventory' that has 40 slots, so will have an array of 40 'Inventory Slot' blueprints which contain 2 variables, int quantity and 1 'Item'. this is where I am not sure what to do, I want 'Item' to be a class / struct / blueprint with variables and functions as an abstract that other items will implement. such as If I have 10 sticks in my inventory it will be structures like... Inventory location[0] will be an InventorySlot with Stick as the Item object and 10 as the qty, and Stick inherits Item.

    what blueprint should I use for the Inventory and Inventory Slots, and how should I do the polymorphism relationship between Item and Stick?

    #2
    I'll see if I can find a youtube tutorial on this when I get home (unless someone beats me to it) but the base premise is that you have a base class which all other items are derived from so you can cast back and forth. Inventory would then be an array of struct f.ex. 1) int id 2) int amount 3) BP_MasterItem Item

    I'll try to make it clearer when I get home, if needed

    Comment


      #3
      My Inventory System looks similar to this (snippet)
      Click image for larger version

Name:	Unbenannt.JPG
Views:	1
Size:	165.1 KB
ID:	1126707



      I recommend looking at how other games implement their inventory.It's always a help to visualize class hierarchies with UML Diagrams. This can show problems with references etc.

      Comment


        #4
        video is unavalible, I have started working on it however my item 'ItemStick' is saying 'wrong class' when trying to create an instance of it in game, no idea what's wrong here, why is there no simple Constructor to use? I am trying to add an instance of ItemStick class into the inventory... this should be so simple yet I can't find anything that works.

        Click image for larger version

Name:	player blueprint.png
Views:	1
Size:	232.0 KB
ID:	1126812

        Item stick is an actor that has the parent class Item which is also an actor.
        Last edited by statphantom; 04-23-2017, 11:28 PM. Reason: added picture

        Comment


          #5
          ItemStick seems to be an Actor Derived class. Actor Derived Classes need to use "Spawn Actor Class" node and not "Construct Object"

          Comment


            #6
            I use a ray trace to detect objects that the player looks at, it is more accurate than a collisions box around the object. Plus when I look at it I can change the icon to a pickup hand which adds a nice touch to it like in the game Far cry 3. Also you will notice that arrays of stucts can be somewhat of an issue if you get complex with it.

            I used this tutorial and expanded on it greatly, now my inventory system is legit haha....

            https://wiki.unrealengine.com/Umg_In...art_1_Overview

            Comment


              #7
              these items aren't items that are spawned in the world, these are created in your inventory when you start cutting a tree down. I'll try spawn actor but I think I did try that and it didn't work.

              Comment


                #8
                ok guys, so I've got a few ideas but still need help. should I change the blueprint type from actor to something else? (i would like to do this but no idea what to do since structs can't have methods or polymorphism) or should I spawn them invisibly and use the UI to access the variables such as sprites etc from it?

                Comment


                  #9
                  No idea why this is happening... I have changed Inventory to be an actor component. inventory slot to be a structure. Item to be an actor, and ItemStick a child of Item. however when debugging it, it is never creating the actor it seems. Item is none, but quantity is correct at 10. the pics bellow show the flow in reverse order.

                  Click image for larger version

Name:	null item.png
Views:	1
Size:	223.5 KB
ID:	1126886

                  Click image for larger version

Name:	Add Item Function.png
Views:	1
Size:	244.9 KB
ID:	1126887

                  Click image for larger version

Name:	construction script.png
Views:	1
Size:	193.6 KB
ID:	1126888

                  Comment


                    #10
                    ok turns out this is because you can't call 'spawn actor' from a construction script.... there really needs to be a message or something telling people this.

                    however now it's not getting added to the inventory at all, the data is being created and made into a struct correctly, but it wont add to the inventory.. why is this?

                    this shows the data is correctly being passed into this function.
                    Click image for larger version

Name:	variables working.png
Views:	1
Size:	206.9 KB
ID:	1126932

                    this is the inventory after the add is called... no change. I have also tried using 'insert' to overwrite one of the 'none' item slots but this doesn't work either.
                    Click image for larger version

Name:	blueprint after the add.png
Views:	1
Size:	223.7 KB
ID:	1126933

                    Comment


                      #11
                      BUMP. Still can't figure out what is wrong

                      Comment


                        #12
                        Are you using Construction Script to add to the Inventory for testing?
                        If so I suggest using BeginPlay.

                        Comment

                        Working...
                        X