LuooYu
February 22, 2017, 6:05am
1
Hi, anyone can help?
When I pressed the Fire1 key, I want the game to spawn a projectile, but it never working.
UPROPERTY(VisibleAnywhere, BlueprintReadWrite, Category = "Turret")
TSubclassOf<AActor> Projectile;
// Handle input
const FTankInput & CurrentInput = Tank->GetCurrentInput();
if (CurrentInput.bFire1 && Projectile)
{
if (UWorld * World = GetWorld())
{
float CurrentTime = World->GetTimeSeconds();
if (Fire1ReadyTime <= CurrentTime)
{
FVector Loc = TurretSprite->GetSocketLocation(MuzzleSocketName);
FRotator Rot = TurretDirection->GetComponentRotation();
if (AActor* NewProjectile = World->SpawnActor(Projectile))
{
NewProjectile->SetActorLocation(Loc);
NewProjectile->SetActorRotation(Rot);
}
// Set the cooldown time.
Fire1ReadyTime = CurrentTime + Fire1Cooldown;
}
}
}
I was binding the Fire1 key with this function:
void ATank::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
Super::SetupPlayerInputComponent(InputComponent);
InputComponent->BindAxis("MoveX", this, &ATank::MoveX);
InputComponent->BindAxis("MoveY", this, &ATank::MoveY);
InputComponent->BindAction("Fire1", EInputEvent::IE_Pressed, this, &ATank::Fire1Pressed);
InputComponent->BindAction("Fire1", EInputEvent::IE_Released, this, &ATank::Fire1Released);
InputComponent->BindAction("Fire2", EInputEvent::IE_Pressed, this, &ATank::Fire2Pressed);
InputComponent->BindAction("Fire2", EInputEvent::IE_Released, this, &ATank::Fire2Released);
}
Sean_L
February 22, 2017, 4:38pm
2
Hey Dream_luo,
Instead of if (AActor* NewProjectile = World->SpawnActor(Projectile))
Try doing this::
AActor* NewProjectile = World->SpawnActor(Projectile)
if(NewProjectile)
{
.....
}
LuooYu
February 24, 2017, 3:35am
3
I’m sorry for the delay.It worked.And I want to ask another question.I have a class inherit from AActor class, But I can’t use SpawnActor() to spawn it, how to do this?
Sean_L
February 24, 2017, 2:05pm
4
Here’s a link to our documentation for this. It will show you how to set it up:
LuooYu
February 26, 2017, 4:48am
5
This documentation is very useful, but when I spawn an actor in another class, editor always crash, how to fix it?
Here’s my code:
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Projectile")
class AProjectile* Missile;
void ASpawnTestCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent)
{
// Set up gameplay key bindings
check(PlayerInputComponent);
PlayerInputComponent->BindAction("Attack", IE_Pressed, this, &ASpawnTestCharacter::Fire);
}
void ASpawnTestCharacter::Fire()
{
FVector Loc = GetActorForwardVector() * 50;
FRotator Rot = GetActorRotation();
AProjectile* NewMissile = GetWorld()->SpawnActor<AProjectile>(Missile->GetClass());
NewMissile->SetActorLocation(Loc);
NewMissile->SetActorRotation(Rot);
}
Sean_L
February 28, 2017, 2:46pm
6
Glad you were able to use the documentation to get something set up.
void ASpawnTestCharacter::Fire()
{
FVector Loc = GetActorForwardVector() * 50;
FRotator Rot = GetActorRotation();
AProjectile* NewMissile = GetWorld()->SpawnActor<AProjectile>(Missile->GetClass(), Loc, Rot);
if(NewMissile)
{
UE_LOG(LogTemp, Warning, TEXT("Actor spawned successfully));
}
}
So I added the SetActorLocation and SetActorRotation into the spawn parameters, which you can do if you take a look at the documentation. It just saves you a bit of writing.
Also, I did a check to see if the Missile spawned successfully, and logged a message so this will tell you if there is an issue with the spawning.
LuooYu
March 1, 2017, 3:09am
7
It’s very useful, and it worked.Maybe I often crash because there is no set Spawn Location and Rotation.Thank you very much.