Use instantiated variable from BeginPlay() in functions?

Hello all! Still new to C++ and Unreal as far as C++ goes.

I am trying to get the reference of a weapon to get a Static Mesh Component from it. I am able to spawn the weapon and attach it to my character within BeginPlay():


Super::BeginPlay();

    TArray<USkeletalMeshComponent*> SKMesh;  //Creates Skeletal Mesh Array = SKMesh

    AActor* ActorRef = GetOwner();   //Gets Actor Reference
    ActorRef->GetComponents<USkeletalMeshComponent>(SKMesh); //Appends Skeletal Mesh Array = SKMesh
    USceneComponent* SceneComp = Cast<USceneComponent>(SKMesh[0]); //Get SkeletalMesh SceneComponent

    FActorSpawnParameters SpawnParams; //Sets Spawn Parameters
    static AWEA_Base* WeaponRef = GetWorld()->SpawnActor<AWEA_Base>(Weapon, FTransform(), SpawnParams); //Spawn Weapon and set Reference
    WeaponRef->AttachToComponent(SceneComp, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true), "weaponSocket"); //Attach Weapon to SKMesh Socket

    

However adding static to the variable “WeaponRef” does not allow me to use it within a function that I have declared in the same class. Any help here?

I think you might be misunderstanding the purpose of static variables. They should be used rarely and only for things that you only ever want one of, regarldess of how many objects of that class are created. If you have a class ANPC and have a static variable within that class (e.g. static float Health), then Health is only created once and is shared by all NPCs, which isn’t normally desired behaviour but can be very useful in some cases.

To fix your problem, you’ll probably want to create the WeaponRef variable in your .h class

e.g.



private:
UPROPERTY(Transient)
class AWEA_Base* WeaponRef;


Then within your BeginPlay method you simply use:



WeaponRef = GetWorld()->SpawnActor<AWEA_Base>(Weapon, FTransform(), SpawnParams);


Whenever you want to then access WeaponRef within this class, you can do so with WeaponRef->WhateverMethodYouWantToCall()

Thank you so much for the response!

I had a feeling it may have been due to needing to use a UPROPERTY but I had no idea what to use! I will do some studying on them tonight.

One last question if you don’t mind though, do you know how I can get this to work? I am trying to spawn projectiles at the WeaponRef’s child static mesh (a static mesh I have added in bp)

I have already done something similar with attaching the weapon to the characters hand previously… But I am not sure what is going on here.




        FActorSpawnParameters SpawnParams; //Sets Spawn Parameters
        TArray<UStaticMeshComponent*> ShootMesh;  //Creates Static Mesh Array = ShootMesh
        WeaponRef->GetComponents<UStaticMeshComponent>(ShootMesh);
        UStaticMeshComponent* NewComp = ShootMesh[0];
        FTransform ShootLocation = NewComp->GetTransform();
        AWEA_Projectile* ProjectileRef = GetWorld()->SpawnActor<AWEA_Projectile>(Projectile, FTransform(ShootLocation), SpawnParams); //Spawns Projectile
        

I am getting “pointer to incomplete class type not allowed” on the NewComp->GetTransform()

Nevermind! Needed the header.