I am attempting to create a object that deletes itself 5 seconds after spawning.
I am using this code:
void ABulletActor::BeginPlay()
{
Super::BeginPlay();
GetWorldTimerManager().SetTimer(this, &ABulletActor::Destroy, 5.0f,false);
}
But it fails to compile with this error:
D:\Libaries\My Documents\Unreal Projects\SpaceInvaders\Source\SpaceInvaders\BulletActor.cpp(20): error C2664: 'void FTimerManager::SetTimer(FTimerHandle &,float,bool,float)' : cannot convert argument 2 from 'bool (__cdecl AActor::* )(bool,bool)' to 'void (__cdecl ABulletActor::* )(void)' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
What have I done wrong?
trutty
(Christian Schulz)
2
Hi laserbeam897,
try creating a function which then handles the destruction of your actor instead of trying to call Destroy
right in the SetTimer
function.
Like so:
void ABulletActor::DestroyActor()
{
// check if actor is valid
if (this->IsValidLowLevel())
{
this->Destroy();
}
}
This will destroy your actor (if true
is returned) at the end of the Tick. You could also use something like:
this->ConditionalBeginDestroy();
this = NULL;
GetWorld()->ForceGarbageCollection(true);
but I guess the above solution is fine. Then set your timer like you already did (see SetTimer)
void ABulletActor::BeginPlay()
{
Super::BeginPlay();
GetWorldTimerManager().SetTimer(this, &ABulletActor::DestroyActor, 5.0f, false);
}
Note that you could also do this in the PostActorCreated function, which is
Called when an actor is done spawning into the world
Let me know if this worked for you