Objects spawn in editor, but not in packaged project.

Hey! I have a couple of objects that I spawn at runtime and attach to my character. It works in the editor, but if I try to package the project for playtesting, they don’t spawn, and if I try to do anything with it (I.E attach a weapon to the player’s hand) the game crashes.


 UObject* WeaponSpawn = Cast<UObject>(StaticLoadObject(UObject::StaticClass(), NULL, TEXT("/Game/Equipment/Blueprints/BaseWeaponBP.BaseWeaponBP")));
UBlueprint* WeaponGenBP = Cast<UBlueprint>(WeaponSpawn);
//WeaponSpawn = NULL;

if (WeaponSpawn)
{
//WeaponSpawn = WeaponBlueprint.Object->GeneratedClass;
UClass* SpawnClass = WeaponSpawn->StaticClass();

FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = this;
SpawnParams.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
CurrentWeapon = GetWorld()->SpawnActor<ATribesmenWeapon>(WeaponGenBP->GeneratedClass, GetActorLocation(), GetActorRotation(), SpawnParams);

if (CurrentWeapon)
{
const USkeletalMeshSocket* socket = GetMesh()->GetSocketByName(FName("SheathSocket1"));
socket->AttachActor(CurrentWeapon, GetMesh());
}
}

What am I doing wrong?

The referencing of the class you are trying to spawn is incorrect. Create a TSubclassOf<> variable, then make a blueprint of your current class and reference the asset that way.



.h
UPROPERTY(EditAnywhere)
TSubclassOf<ATribesmenWeapon> WeaponToSpawn;

.cpp
CurrentWeapon = GetWorld()->SpawnActor<ATribesmenWeapon>(WeaponToSpawn, GetActorLocation(), GetActorRotation(), SpawnParams);


Code like StaticLoadObject or refering to GeneratedClasses’ etc, isn’t required and shouldn’t be used in game code. There’s no reason to hard-code asset references also.

Ah, thank you! That was way cleaner than what I ended up with and just worked.
I was following an outdated guide and had do scavenge together various code examples until I got something that would work.

I’m also used to doing everything in code and didn’t think of making it basic and reusable, and setting the reference in blueprint instead.