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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

When to create Sub-Objects of a blueprint class

I am trying to code a modular weapon system. The weapon is an Actor with a number of components that are responsible for different parts of the weapon.

 class ARangedWeapon : public AWeapon
 {
 protected:
     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Ammo, meta=(AllowPrivateAccess = "true"))
     class UWeaponVisualsComponent* VisualsComponent;
 
     UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category=Ammo, meta=(AllowPrivateAccess = "true"))
     class UWeaponAmmoComponent* AmmoComponent;
     ....
 }

Each component would have events that are fired when, say the weapon is fired, or reloaded etc. Ideally I would like to be able to subclass each component in blueprints to allow for custom behaviour and then just put them together in the Weapon blueprint, but I know that you cannot change the class of a Component.

Instead I decided that each component would have a assignable sub-object that actually did the heavy lifting of the class.

 class UWeaponAmmoComponent : public UActorComponent
 {
 public:
     UPROPERTY(EditAnywhere)
     TSubclassOf<UWeaponAmmoManager> AmmoManagerClass;
 
 protected:
     UPROPERTY(BlueprintReadOnly)
     class UWeaponAmmoManager AmmoManager;
 }

That way I could just subclass the manager in blueprints, and set the "AmmoManagerClass" variable to be the blueprint that I want to use.

I now have two questions:

  1. Is this the best way of doing this? Is there a way that I can bypass having to have another sub-object, and just subclass the ActorComponents?

  2. How can I spawn an object of the AmmoManagerClass?

I can't spawn it in the constructor as the value of AmmoManagerClass wouldn't have been set in the editor yet. I tried creating the Manager in the PostInitProperty() and InitializeComponent() functions, but the normal ways of creating UObjects (ConstructObject()) caused the engine to crash with the error: "class object none was created in UWeaponAmmoComponent instead of package".

I'm sort of all out of ideas at the moment, any help would be much appreciated.


Edit:

The two errors I'm getting are:

 Object BlueprintGeneratedClass None created in WeaponAmmoComponent instead of Package

Which happens when I use this in InitializeComponent() in the AmmoComponent:

  NewObject<UWeaponAmmoManager>(this, AmmoManagerClass->GetClass())

And:

 Assertion failed: ClassAddReferencedObjects != NULL

Which happens when I use this in the same function:

 ConstructObject<UWeaponAmmoManager>(AmmoManagerClass->GetClass())

But this one happens after I stop the test whereas the first one is a soon as I start it.

Product Version: Not Selected
Tags:
more ▼

asked Jan 09 '15 at 02:22 PM in C++ Programming

avatar image

WavedClover497
6 2 3 5

avatar image Justin.Dooley Jan 09 '15 at 02:45 PM
  1. seems pretty sound. you can make as many subclasses as you like which is kind of the beauty of c++. If you don't want an ammo manager, some more information on what it does would let us provide better answers on how to approach this.

  2. Are you trying to create it with blueprints or C++? If it is going to have a position in the world it should be an actor in which case you would add a child component. You should be able to create an object normally though. From the error it looks like it could not find the class you where telling it to construct. What did you have when trying to create the object?

avatar image WavedClover497 Jan 09 '15 at 03:50 PM

Thanks for the help.

  1. The AmmoComponent/AmmoManager are meant to handle the weapon's ammo. When the weapon shoots, one bullet will be taken out of the clip, and when the clip is empty, they will tell the weapon actor that it can no longer shoot. I wanted to be able to subclass AmmoComponent or AmmoManager in blueprints so I could add other behaviors to the ammo (like an energy ammo etc.).

  2. I'm trying to create the object in c++ as the AmmoComponent is spawned or initialized.

I also added some more information in the original question that describes the errors that I am getting.

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

1 answer: sort voted first

I managed to fix the problem, or more like I just changed it. I added the EditInlineNew specifier to the AmmoManager, and just had a pointer to it from the weapon actor that was specified as Instanced. This let me create an instance of any subclass of AmmoManager directly from the defaults tab and I could do away with the AmmoComponent altogether.

more ▼

answered Jan 11 '15 at 04:14 AM

avatar image

WavedClover497
6 2 3 5

(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