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:
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:
From here, I have experienced several problems and I can’t seem to figure out what I’m doing wrong.
- 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:
Further more if I go back into my BP editor, it shows the value as reset (same as first screenshot, None as template)
- 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:
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!