x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

How to create TSubobjectPtr of a blueprint class

I have a c++ class called "Inventory"

I extended it to a blueprint object.

I want to make a TSubobjectPtr of the blueprint class. How do I do it?

  UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Inventory)
          TSubobjectPtr<class UInventory> CharacterInventory;
Product Version: Not Selected
Tags:
more ▼

asked Aug 11 '14 at 10:13 AM in C++ Programming

avatar image

envenger
428 77 118 148

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

Why do you want it to be subobject ?

In anycase TSubobjectPtr is used to create well, sub objects. Subobjects are created on object construction. I don't think that creatading Subobject out of blueprint class is possible. Though I might be wrong.

Why not create normal pointer, and then spawn object from blueprint to it ?

Eventually why not create ActorComponent, and create Subobject ?

more ▼

answered Aug 11 '14 at 10:26 AM

avatar image

iniside
1.6k 105 97 185

avatar image envenger Aug 11 '14 at 10:54 AM

I have a inventory which is extended from an actor component,

The inventory stores the current weapons a character is using so I made the inventory TSubObjectPtr in the character class

 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Inventory)
         TSubobjectPtr<class UInventory> CharacterInventory;

Now the problem is many things I am assigning in the inventory are blueprints. So I thought it would be better if I made them inventory a blueprint. But if I made it a blueprint, I cant make it a TSubObjectPtr So i think of making it a normal pointer and storing the values in it. Not sure if it would give much problem.

avatar image iniside Aug 11 '14 at 11:10 AM

ActorComponenets can't be blueprints. Yet. You might try, but it will probably either crash or won't compile at all.

If you want to assign some default items to your inventory, you will have to do it trough either actor that owns inventory component, game mode (and just assign something to all actors with inventory), or by some other means.

In your component, you can create array of

 TSubclassOf<Item> DefaultItemClasses

Add items to it (if you expose it as UPROPERT, it will show in Actor defaults), and then just add item clases to it.

Then in InitializeComponent() override, you might just spawn items from this array.

you will need to add:

bWantsToInitialize, to InitializeComponent() work.

Whatever option you choose, actor which owns inventory will be needed, to access component.

avatar image envenger Aug 11 '14 at 11:55 AM

This was all unnecessary as I had forgotten I could change the variables of the actor component directly through the parent actor using blueprints.

The blue printing the actorcomponent is not needed thanks...

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

If you could tell us a bit more about what you want to achieve, that would be nice (:

Like iniside said, why don't you use a normal Pointer? You can get the Blueprint with the ObjectFinder and Cast the Class to that Pointer.

more ▼

answered Aug 11 '14 at 10:32 AM

avatar image

eXi
6.8k 333 185 470

avatar image envenger Aug 11 '14 at 10:54 AM

I have a inventory which is extended from an actor component,

The inventory stores the current weapons a character is using so I made the inventory TSubObjectPtr in the character class

 UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = Inventory)
         TSubobjectPtr<class UInventory> CharacterInventory;

Now the problem is many things I am assigning in the inventory are blueprints. So I thought it would be better if I made them inventory a blueprint. But if I made it a blueprint, I cant make it a TSubObjectPtr So i think of making it a normal pointer and storing the values in it. Not sure if it would give much problem.

avatar image eXi Aug 11 '14 at 11:02 AM

Hm, i don't know how your inventory is structured. I got an inventory myself. Its made of a few Arrays in the Character Blueprint. An Array of a Struct that stores all information of an Item. An Array of Class Type to manualy store all ItemBlueprints and spawn them. An Array to store the Index of the Class Type Array. Thats the main Inventory. And an Array to store the StackCount of the Items.

Class Array got for example:

 [0] Stone
 [1] Stick
 [2] Chair

The third array(Inventory)got a stone at the second slot and a chair and the 5th for example. So i save it this way:

 [0] Empty
 [1] 0
 [2] Empty
 [3] Empty
 [5] 2

later i can just say i want to spawn the Item in of the second slot and with the Number in this i can get the matching BlueprintClass.

I don't know if this helps you restructuring your Inventory system or is completely useless information.

I guess making a pointer to your inventory would work too :X

avatar image envenger Aug 11 '14 at 11:55 AM

This was all unnecessary as I had forgotten I could change the variables of the actor component directly through the parent actor using blueprints.

The blue printing the actorcomponent is not needed thanks...

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question