C++ to derived BP Class issues (and other newbie questions)

Greetings.

I am attempting to familiarize myself with the general Actor/Pawn/Component philosophy of the Unreal Engine’s game objects, and I’m at a bit of a loss here. I’m coming from Unity Engine so it may be the wrong mindset to start with, but anyway, if you could help me out with these few issues I have, I believe I can take it from there.

Let’s say that I have a Player class defined. It has walking, jumping, the usual. Now, what I would like to do, is to have a reference to a component class which will represent a weapon held by the player. I wish to assign the various variables of the weapon (such as particle systems etc) via Blueprint derived class, but manage the actual firing logic and math-heavy stuff underneath on C++ level.

Within my player class, “LightCharacter”, I have this public property:



	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Weapon")
	UBeamWeapon* BeamWeaponComponent;

Within LightCharacter.cpp constructor, I initialize it like so:


BeamWeaponComponent = CreateDefaultSubobject<UBeamWeapon>("BeamWeapon");
	BeamWeaponComponent->RegisterComponent();
	BeamWeaponComponent->AttachTo(GetMesh());

So far, so good. Within my BeamWeapon class, I have the following particle system defined as a public property:



	UPROPERTY(BlueprintReadWrite, EditAnywhere, Category = "Beam Particle")
	UParticleSystemComponent* ParticleComponent;


And within BeamWeapon.cpp constructor, I initialize it like so:


ParticleComponent = CreateDefaultSubobject<UParticleSystemComponent>("Beam Particle");

If I make a blueprint derived from this class as it is, it will look like this:

b5f4468b7357fe9255739ae1e47c85924515736f.jpeg

As you can see, it has an empty particle template field which I want to assign via blueprint (but still keep a pointer to the UBeamWeapon class within my C++).

So let’s say I assign this to my Beam particle:
f33f70dc0dc9d246f1a93a173571c83974f2b604.jpeg

From here, I have experienced several problems and I can’t seem to figure out what I’m doing wrong.

  1. The assigned particle field gets reset for some reason whenever I hit Save, or Play in Editor. You can also see how freshly dropped BP class into world:
    c003ede391a1c2b391a77505ee4038e5a32220d0.jpeg

Further more if I go back into my BP editor, it shows the value as reset (same as first screenshot, None as template)

  1. But OK, say I go into play mode right now, disregarding the fact that the Template of the particle system is not assigned, I encounter a completely new problem. If I enter play mode, move a bit, and then Eject out of my character, I see this:
    4a8fff14ec413bd8fc29c3138110d96c96fd8e58.jpeg

The particle system component for some reason initializes at 0,0,0 in World coords and is not attached to the player at all.

This is where it gets weird: If I manually assign the Template field of the particle system (which in last screenshot shows as None), the Beam weapon and it’s particle system somehow magically “wakes up” and attaches itself to the player - but keeping it’s world location. Which means, if I moved 3 meters away, and then assigned the particle Template, it will forever stay at 3 meters away from me (now it is properly attached for some reason), with locked in relative rotation and relative position from me.

So with these two problems, I am completely at a loss - I have apparently approached this completely wrong from the start.

Here’s a short outline of what I wish to achieve:

The basic hierarchy of my C++ code classes would be as follows:

  • Player class

    • Weapon class

    • Shield class (eventually)

Where Player class has basic inputs and control, maybe energy and HP levels or such. At input events, it would call up appropriate Weapon class or shield class functions, which they would further handle the spawning of VFX, particles, dynamic lights, and line-tracing for damage etc. Is this the correct way of thinking, should this be doable?

I am attaching the .h and .cpp files of my Player and Beamweapon I have so far.

So if somebody can either: a) Help me solve those two very big problems as outlined above, or b) Suggest a new way about going around the infrastructure and how to properly approach this problem and accomplish my goal, I would be very very grateful.

Thank you very much in advance!

I managed to solve my problem.

Step 1)
Up and including engine version 4.13.0 - Delete and remake any BP classes after any significant change to the underlying C++ class!
I was seeing some immense differences in BP component hierarchy even though I was toying around with different Attaching in C++ constructors. Deleting and re-making the BP class solved the issue immediately.

Step 2)
My Weapon class is now UActorChildComponent class. This seems to provide all functionality I need and plays nicely to being attached (and having stuff attached).

*Step 3) *
To solve some of the BP variables not saving, or editor crashes when editing BP variables in Blueprint editor, use the following workaround:

  • Drag a BP from Content browser into scene.
  • Make any BP changes on the actual instance of BP class there
  • Go to Edit Blueprint (big blue button right above your properties) and select -> Make Instance Changes to parent BP class (or something named as such, you get the point)
  • Remove BP instance from scene

If you go into actual BP editor now, you will see your changes properly set up there.

Step 4)
Attaching my code files if anybody needs the correct Constructor stuff (order of attachment and how to create subclasses and subobjects etc).

Big thanks to game-dev Slack chat, you guys are very helpful!

For anyone wondering it’s not UActorChildComponent, It’s UChildActorComponent