Ok… I’m a bit stumped but I have a hunch.
You are getting the error when trying to reference the AHavenCharacter
or Parent
variable in the AttachTo
method. Also, you are getting the error in the old code when you try to reference the character->mesh
. I would think that there is some issue reading the character
variable… in other words, trying to access the AHavenCharacter
. I have never seen attaching done like this before so again… I’m a bit stumped.
I do have a suggestion though… and equipping things the way the developers of the engine do in their samples might be the way to go. Read on if you are interested in a test that might fix this issue and also help you learn a better way to equip items and weapons at the start of the game.
I’m sorry I don’t know why it’s currently causing the error for you. Another method of implementing the same thing is not a great fix but it might help =/
I think proper implementation of weapon equipping is needed here. If you have no desire to use this method of equipping items just ignore the rest of this post… but this is the way it’s done in all the examples and in 2 of my current projects. Not only is it extendable, but it works perfectly.
You are trying to start the game and have the AHavenWeapon
attach itself to a AHavenCharacter
you get by querying the UWorld
and grabbing a reference to the first ULocalPlayer
in the list of GamePlayers
and then casting it to AHavenCharacter
.
I know nothing of your implementation or other classes but I would suggest this…
Control your weapon attaching from your AHavenCharacter
class…
To do this, in your AHavenCharacter
class you create a variable that you will assign your weapon Blueprint to in the editor. Then, in your AHavenCharacter
class you spawn that weapon from the class variable and assign it to a CurrentWeapon
pointer variable. You then call a function in that CurrentWeapon
to attach that weapon to yourself, the current AHavenCharacter
. This seems to be the default way to equip weapons and it always seems to work.
Steps:
In your AHavenCharacter
class header file, create a variable for the weapon class and the CurrentWeapon
in the header file.
// weapon class
UPROPERTY(EditDefaultsOnly, Category = Inventory)
class AHavenWeapon WeaponClass;
// weapon
UPROPERTY()
class AHavenWeapon* CurrentWeapon;
In your AHavenWeapon
class create a method for attaching and call it whatever you want… I’ll call it TestAttachWeapon
.
void AHavenWeapon::TestAttachWeapon(class AHavenCharacter* MyPawn){
if(!MyPawn) return;
weaponMesh->AttachTo(MyPawn->YOUR_MESH_COMPONENT_NAME);
}
So, now back to your AHavenCharacterClass
. In the BeginPlay
method…
void AHavenCharacter::BeginPlay(){
Super::BeginPlay();
// if WeaponClass not null
if(WeaponClass){
// Create some parameters for spawning
FActorSpawnParameters SpawnInfo;
SpawnInfo.bNoCollisionFail = true;
// Spawn the weapon from the class
CurrentWeapon = GetWorld()->SpawnActor<AHavenWeapon>(WeaponClass, SpawnInfo);
// if CurrentWeapon not null [spawn successful]
if(CurrentWeapon){
// Attach it to myself
CurrentWeapon->TestAttachWeapon(this);
}
}
Again, to do this you will need a Blueprint of your AHavenCharacter
and optionally your AHavenWeapon
. You just create those Blueprints in the editor from the classes.
In the editor, you simply set the AHavenWeapon WeaponClass
variable in the Default values of the Character blueprint.
Now, I have mine set up for a TArray
of Weapons. You will have the “Inventory” tab, but you will only see one spot with a drop down to put your AHavenWeapon
.
So what you do is select your AHavenWeapon
class or AHavenWeapon
Blueprint there and it will be used to spawn one in the game and attach it to your character.