ProjectileMovement does not work at second spawning - c++

I'm doing a simple paddle game with Blueprints at first and C++ after.
I have the current workflow in my blueprint game mode:
And this is my code base on:
void APaddleGameGameMode::SpawnBall()
{
UWorld *world = GetWorld();
if (world)
{
Ref_GameBall = world->SpawnActorDeferred<APaddleGameBall>(APaddleGameBall::StaticClass(), FTransform(FRotator::ZeroRotator, FVector::ZeroVector, FVector::OneVector), NULL, NULL, ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButDontSpawnIfColliding);
if (Ref_GameBall) world->GetTimerManager().SetTimer(DelayBallMovement, this, &APaddleGameGameMode::SetVelocity, 0.2f);
}
}
void APaddleGameGameMode::SetVelocity()
{
if(Ref_GameBall) Ref_GameBall->ProjectileMovement->Velocity = FVector(Direction * Speed, 0.f, 0.f).RotateAngleAxis(FMath::RandRange(-45.f, 45.f), FVector(0.f, 1.f, 0.f));
Ref_GameBall->ProjectileMovement->Activate();
}
The problem here is, every time the player make a point, the ball is destroyed and a new ball is spawned:
void APaddleGameGameMode::UpdateScore(bool bIsAI)
{
UWorld *world = GetWorld();
if (bIsAI)
{
SPScore++;
Direction = 1.f;
}
else
{
FPScore++;
Direction = -1.f;
}
if (world) world->GetTimerManager().SetTimer(DelaySpawningBall, this, &APaddleGameGameMode::SpawnBall, 1.f);
}
It works fine at the first, the initial, spawnin: ball is spawned and it takes movement; but it does not work at the second spawn, i don't understand why, the ball is spawned but it does not move.
Can anyone to explain me it and help me to solve it?
Thanks.

SpawnActorDeferred function defers the BeginPlay, giving a opportunity to the caller to set parameters beforehand, it means, the spawned actor if in a half-way to being full constructed(is a half actor) until the FinishSpawningActor function is called, wherever it is put; it means, a valid actor instance that didn't receive BeginPlay yet and so it is not fully initialized.
Here, Ref_GameBall ->FinishSpawning( Transform ) can be put before to activate the projectile movement, or replacing SpawnActorDeferred by SpawnActor:
FActorSpawnParameters SpawnParameters;
SpawnParameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
Ref_GameBall = world->SpawnActor<APaddleGameBall>(APaddleGameBall::StaticClass(), FTransform(FRotator::ZeroRotator, FVector::ZeroVector, FVector::OneVector), SpawnParameters);
That's all
Thanks to #KnownBugg, from Discord, to help me with this.

Related

Error with ue4 c++ fps tutorial section 3.2, what should I do?

I am up to section 3.2 of the fps tutorial on unreal engine 4. In this section I'm supposed to implement shooting into the code via the fire function so that i can shoot projectiles in-game. However, after typing the code the tutorial provided, I got one error when compiling it. The error is as follows
/Users/apple/Documents/Unreal Projects/FPSProject/Source/FPSProject/FPSCharacter.cpp:120:38: 'Instigator' is a private member of 'AActor'
When I remove this line and compile the code it does so successfully, but when I test it out on unreal, I can't shoot projectiles at all.
Here is the code
FPSCharacter.cpp
include "FPSCharacter.h"
include "FPSProject.h" #include "FPSCharacter.generated.h" #include "FPSProjectile.h" #include "GameFramework/Actor.h"
// Sets default values AFPSCharacter::AFPSCharacter() { // Set this character to call Tick() every frame. You can turn this off to improve performance if you don't need it. PrimaryActorTick.bCanEverTick = true;
// Create a first person camera component.
FPSCameraComponent = CreateDefaultSubobject<UCameraComponent>(TEXT("FirstPersonCamera"));
// Attach the camera component to our capsule component.
FPSCameraComponent->SetupAttachment(GetCapsuleComponent());
// Position the camera slightly above the eyes.
FPSCameraComponent->SetRelativeLocation(FVector(0.0f, 0.0f, 50.0f + BaseEyeHeight));
// Allow the pawn to control camera rotation.
FPSCameraComponent->bUsePawnControlRotation = true;
// Create a first person mesh component for the owning player.
FPSMesh = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("FirstPersonMesh"));
// Only the owning player sees this mesh.
FPSMesh->SetOnlyOwnerSee(true);
// Attach the FPS mesh to the FPS camera.
FPSMesh->SetupAttachment(FPSCameraComponent);
// Disable some environmental shadowing to preserve the illusion of having a single mesh.
FPSMesh->bCastDynamicShadow = false;
FPSMesh->CastShadow = false;
// The owning player doesn't see the regular (third-person) body mesh.
GetMesh()->SetOwnerNoSee(true);
}
// Called when the game starts or when spawned void AFPSCharacter::BeginPlay() { Super::BeginPlay();
if (GEngine)
{
// Put up a debug message for five seconds. The -1 "Key" value (first argument) indicates that we will never need to update or refresh this message.
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, TEXT("We are using FPSCharacter."));
}
}
// Called every frame void AFPSCharacter::Tick( float DeltaTime ) { Super::Tick( DeltaTime );
}
// Called to bind functionality to input void AFPSCharacter::SetupPlayerInputComponent(class UInputComponent* PlayerInputComponent) { Super::SetupPlayerInputComponent(PlayerInputComponent);
// Set up "movement" bindings.
PlayerInputComponent->BindAxis("MoveForward", this, &AFPSCharacter::MoveForward);
PlayerInputComponent->BindAxis("MoveRight", this, &AFPSCharacter::MoveRight);
// Set up "look" bindings.
PlayerInputComponent->BindAxis("Turn", this, &AFPSCharacter::AddControllerYawInput);
PlayerInputComponent->BindAxis("LookUp", this, &AFPSCharacter::AddControllerPitchInput);
// Set up "action" bindings.
PlayerInputComponent->BindAction("Jump", IE_Pressed, this, &AFPSCharacter::StartJump);
PlayerInputComponent->BindAction("Jump", IE_Released, this, &AFPSCharacter::StopJump);
PlayerInputComponent->BindAction("Fire", IE_Pressed, this, &AFPSCharacter::Fire);
}
void AFPSCharacter::MoveForward(float Value) { // Find out which way is "forward" and record that the player wants to move that way. FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::X); AddMovementInput(Direction, Value); }
void AFPSCharacter::MoveRight(float Value) { // Find out which way is "right" and record that the player wants to move that way. FVector Direction = FRotationMatrix(Controller->GetControlRotation()).GetScaledAxis(EAxis::Y); AddMovementInput(Direction, Value); }
void AFPSCharacter::StartJump() { bPressedJump = true; }
void AFPSCharacter::StopJump() { bPressedJump = false; }
void AFPSCharacter::Fire() { // Attempt to fire a projectile. if (ProjectileClass) { // Get the camera transform. FVector CameraLocation; FRotator CameraRotation; GetActorEyesViewPoint(CameraLocation, CameraRotation);
// Transform MuzzleOffset from camera space to world space.
FVector MuzzleLocation = CameraLocation + FTransform(CameraRotation).TransformVector(MuzzleOffset);
FRotator MuzzleRotation = CameraRotation;
// Skew the aim to be slightly upwards.
MuzzleRotation.Pitch += 10.0f;
UWorld* World = GetWorld();
if (World)
{
FActorSpawnParameters SpawnParams;
SpawnParams.Owner = this;
SpawnParams.Instigator = Instigator;
// Spawn the projectile at the muzzle.
AFPSProjectile* Projectile = World->SpawnActor<AFPSProjectile>(ProjectileClass, MuzzleLocation, MuzzleRotation, SpawnParams);
if (Projectile)
{
// Set the projectile's initial trajectory.
FVector LaunchDirection = MuzzleRotation.Vector();
Projectile->FireInDirection(LaunchDirection);
}
}
}
}

Atached Actor Mesh dont stick to Parent Actor

Context:
I'm creating some Actors (Shields) using C++ in ActorComponent (ShieldComponent) which is attached to Actor (Ship).
When I move my Ship, Actors (Shields) stay in place on world (don't move with Ship).
What can be important: I don't call CreateShieldActor in Constructor, so I can't use ConstructorHelpers::FObjectFinder instead of StaticLoadObject/LoadObject to load Mesh/Material.
My C++ code for creation looks like this:
AShieldPart* UShieldComponent::CreateShieldActor(AActor* Owner, FString MeshName)
{
//Create Shield Actor
FVector Location = Owner->GetActorLocation();
FRotator Rotator(0, 0, 0);
FVector Scale(10, 10, 10);
FActorSpawnParameters SpawnInfo;
SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AdjustIfPossibleButAlwaysSpawn;
SpawnInfo.Owner = Owner;
FTransform Transform(Rotator, Location, Scale);
UClass * MeshClass = AShieldPart::StaticClass();
AShieldPart* ShieldPart = GetWorld()->SpawnActor<AShieldPart>(MeshClass, Transform, SpawnInfo);
//Attach Mesh to Shield Actor
ShieldPart->AttachToActor(Owner, FAttachmentTransformRules(EAttachmentRule::KeepRelative, false));
//Set Mesh for Shield
ShieldPart->SetNewMesh(MeshName);
//Set Material
UMaterial* Material = Cast<UMaterial>(StaticLoadObject(UMaterial::StaticClass(), NULL, TEXT("/some/path")));
ShieldPart->MeshComponent->SetMaterial(0, Material);
return ShieldPart;
}
AShieldPart::AShieldPart()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
RootComponent = CreateDefaultSubobject<USceneComponent>(TEXT("RootComponent"));
SetRootComponent(RootComponent);
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
MeshComponent->SetupAttachment(RootComponent);
}
void AShieldPart::SetNewMesh(FString MeshName)
{
UStaticMesh* Mesh = Cast<UStaticMesh>(StaticLoadObject(UStaticMesh::StaticClass(), NULL, *MeshName));
//UStaticMesh* Mesh = LoadObject<UStaticMesh>(nullptr, *MeshName);
MeshComponent->SetStaticMesh(Mesh);
MeshComponent->SetCollisionProfileName(TEXT("BlockAll"));
MeshComponent->SetNotifyRigidBodyCollision(true);
MeshComponent->SetSimulatePhysics(true);
MeshComponent->SetEnableGravity(false);
MeshComponent->RegisterComponent();
}
That it would be more interesting, when I create that Actors (Shields) using BP all works fine...
The solution turned out to be eliminating the USceneComponent as a RootElement. The main element must be UStaticMeshComponent, then it can attached to the parent element (Ship).
AShieldPart::AShieldPart()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
// Creating MeshComponent and attach to RootComponent
MeshComponent = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MeshComponent"));
RootComponent = MeshComponent;
MeshComponent->AttachToComponent(RootComponent, FAttachmentTransformRules(EAttachmentRule::SnapToTarget, true));
...

LineTraceSingleByChannel() not hitting enemies

I'm currently trying to build a tank game in Unreal Engine 4.20. I'm trying to aim my tank turret and barrel onto the player's pointer, by using method LineTraceSingleByChannel(). However, when I log my FHitResult to the console, I only get the landscape, even when pointing to ennemy tanks, which inherit from Pawn Class.
Here is my code :
FHitResult HitResult;
auto StartLocation = PlayerCameraManager->GetCameraLocation();
auto EndLocation = StartLocation + (LookDirection * LineTraceRange);
if (GetWorld()->LineTraceSingleByChannel(
HitResult,
StartLocation,
EndLocation,
ECollisionChannel::ECC_Visibility
)
)
{
UE_LOG(LogTemp, Warning, TEXT("Hit %s"), *HitResult.Actor->GetName())
HitLocation = HitResult.Location;
return true;
}
Please help me if you know the answer to my problem!
You need to block the Channel: 'Visibility', in the Character(Capsule Component) you want to hit.

getGravity not working?

I am new to c++ as well as game development. I coded a function using bullet physics which returns the gravity of the world and it seems to be not working. It always returns 0. Though, I've initialized the world, solver, dispatcher, broadphase and collision configuration.
Here's the code:
void _setGravity(btScalar gravity) //sets the gravity of the world
{
if(gravity > 0.f)
gravity = -gravity;
_dynamicsWorld->setGravity(btVector3(0, gravity, 0));
}
btScalar _getGravity(void) //returns the gravity of the world
{
return ((btScalar*)_dynamicsWorld->getGravity());
}
Is there something that I am doing wrong ?
Thank you.
The getGravity function returns a btVector3 by value, so you need to use resulting vectors getY to get the gravity:
return _dynamicsWorld->getGravity().getY();

How do you stop OgreBullet Capsule from falling over?

I've just started implementing bullet into my Ogre project. I follows the install instructions here: http://www.ogre3d.org/tikiwiki/OgreBullet+Tutorial+1
And the rest if the tutorial here:
http://www.ogre3d.org/tikiwiki/OgreBullet+Tutorial+2
I got that to work fine however now I wanted to extend it to a handle a first person camera. I created a CapsuleShape and a Rigid Body (like the tutorial did for the boxes) however when I run the game the capsule falls over and rolls around on the floor, causing the camera swing wildly around.
I need a way to fix the capsule to always stay upright, but I have no idea how
Below is the code I'm using.
(part of) Header File
OgreBulletDynamics::DynamicsWorld *mWorld; // OgreBullet World
OgreBulletCollisions::DebugDrawer *debugDrawer;
std::deque<OgreBulletDynamics::RigidBody *> mBodies;
std::deque<OgreBulletCollisions::CollisionShape *> mShapes;
OgreBulletCollisions::CollisionShape *character;
OgreBulletDynamics::RigidBody *characterBody;
Ogre::SceneNode *charNode;
Ogre::Camera* mCamera;
Ogre::SceneManager* mSceneMgr;
Ogre::RenderWindow* mWindow;
main file
bool MinimalOgre::go(void)
{
...
mCamera = mSceneMgr->createCamera("PlayerCam");
mCamera->setPosition(Vector3(0,0,0));
mCamera->lookAt(Vector3(0,0,300));
mCamera->setNearClipDistance(5);
mCameraMan = new OgreBites::SdkCameraMan(mCamera);
OgreBulletCollisions::CollisionShape *Shape;
Shape = new OgreBulletCollisions::StaticPlaneCollisionShape(Vector3(0,1,0), 0); // (normal vector, distance)
OgreBulletDynamics::RigidBody *defaultPlaneBody = new OgreBulletDynamics::RigidBody(
"BasePlane",
mWorld);
defaultPlaneBody->setStaticShape(Shape, 0.1, 0.8); // (shape, restitution, friction)
// push the created objects to the deques
mShapes.push_back(Shape);
mBodies.push_back(defaultPlaneBody);
character = new OgreBulletCollisions::CapsuleCollisionShape(1.0f, 1.0f, Vector3(0, 1, 0));
charNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
charNode->attachObject(mCamera);
charNode->setPosition(mCamera->getPosition());
characterBody = new OgreBulletDynamics::RigidBody("character", mWorld);
characterBody->setShape( charNode,
character,
0.0f, // dynamic body restitution
10.0f, // dynamic body friction
10.0f, // dynamic bodymass
Vector3(0,0,0),
Quaternion(0, 0, 1, 0));
mShapes.push_back(character);
mBodies.push_back(characterBody);
...
}
You can lock the angular motion of the capsule in order to prevent it from tipping over. Ogre doesn't expose this functionality directly, but you should be able to access the underlying bullet rigid body to accomplish what you need:
characterBody->getBulletRigidBody()->setAngularFactor(btVector3(0.0f,1.0f,0.0f));
This would lock rotation on the xaxis and zaxis, preventing the capsule from tipping over, but allowing rotation around the yaxis, so that the character can still turn.
At some point you may want to looking into using a kinematic character controller, but that's an answer to a different question.