Hey, I have an actor with a static mesh component and box collision. If I drag it into the level looks fine. But when I spawn it into the level (GetWorld()->SpawnActor<>) it spawns in the correct location but there is no mesh or collision box.
When I go into the outliner while the game is running there is no static mesh for the spawned actor, choose the correct mesh and it appears as expected.
The collision box never appears though and is not hidden in game.
Anybody experienced something like this before?
Hey,
Can you send some screenshots of how this is setup to help get a better idea. Are the components added via Blueprints or C++ and if its C++ are they attached to a Root Component that is valid?
I’ve tried redoing the CreateDefaultSubobject and recreating the blueprint which has fixed some editor issues before but that didn’t work this time
Okay thats surprising it all looks fine to me there, where do you spawn the Actor? Is it using the Blueprint class or the C++ Class to spawn it?
a) with projectiles make sure your shape component is the root and not the mesh otherwise you will have a bad time when it comes to collision detection.
b) make sure you set the static mesh for the static mesh component in the actors default settings (it’s normally empty)
c) make sure you are either setting the mesh via c++ or spawning a blueprint version based on your c++ version with the mesh selected.
(spawning a pure c++ class of the projectile will have no mesh actor set by default!)
The function to spawn the actor is in c++, (but the actor that it is spawning is a blueprint class derived from the actor c++ class).
Sorry I’m wrong, just double checked its spawning the actor from the c++ class
Yep sorry, I double checked the outliner it is spawning the actor from c++ and not the blueprint class, how do i spawn the blueprint class from c++?
Most of the time you would expose the class property through a uproperty and set it to a TSoftClassPtr or you can use the old TSubclassOf (but it will cause a hard reference) and then use that passed class as the parameter in the getworld spawn actor command.
Note: you have to manually load TSoftClassPtr into memory at least once, but they do not cause hard references until then.
Then later you can set the exposed class variable to the BP version of the projectile
Ok great thank you for the help so far
I have the subclass in the actor class and the spawn function is in my player character class.
I haven’t used subclasses before and am struggling to cast to it.
You shouldn’t need to cast to it if you expose a TSubclassOf<ACharacter> MyBPClass
for example then when you want to spawn that actor you can call SpawnActor<ACharacter>(MyBPClass, Transform)
This then spawns it using the derived BP class you have provided and set in Editor
Again thank you for the help so far
I assume this isn’t exposed correctly if its undefined and the subclass is in the public section
And I’ve been trying to look into this
try forward declaring AChakram in the header and then do the full include inside of your cpp file.
So basically somewhere after your .generated file type in
class AChakram;
Still not working, and its not a visual studio issue, it wont compile
Just pass in ProjToSpawn
Does your cpp file where you are calling the spawn include the header for AChakram?
The variable
TSubClassOf<AChakram> ProjToSpawn
is in the header of your projectile emitter / weapon
and the calling of spawnActor is in the cpp of the same emitter / weapon correct?
You can also forward declare this way
URPOPERTY(EditAnywhere,BlueprintReadWrite, Category = "Weapon")
TSubClassOf<class AChakram> ProjToSpawn;
but without the header in the weapon cpp file it just like a promise that you will include the definition.
Subclass is in Actor class - AChakram
SpawnActor is in a separate, Character class
Sorry if this is a stupid problem, i know i can do it in blueprints and the blueprint class is just there but I’m trying to learn
header
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Weapon.generated.h"
UCLASS()
class YOUR_API AWeapon : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AWeapon();
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon")
TSubclassOf<class AChakram> ProjToSpawn;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Weapon")
class USceneComponent* SpawnLoc;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
UFUNCTION(BlueprintCallable)
void Fire();
};
.cpp
// Fill out your copyright notice in the Description page of Project Settings.
#include "Weapon.h"
#include "Chakram.h"
#include "Components/SceneComponent.h"
AWeapon::AWeapon()
{
PrimaryActorTick.bCanEverTick = true;
SpawnLoc = CreateDefaultSubobject<USceneComponent>(TEXT("SpawnLoc"));
}
void AWeapon::BeginPlay()
{
Super::BeginPlay();
}
void AWeapon::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
void AWeapon::Fire()
{
if(ProjToSpawn!=nullptr){
GetWorld()->SpawnActor<AChakram>(ProjToSpawn, SpawnLoc->GetComponentLocation(), GetActorRotation());
}
}
In game
Project
proj2.zip (38.7 KB)
Ok Thank you for your help and patience
I redid everything and used the alternate TSubclassOf declaration and it works now.
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.