There are two ways to get what you are after. I got all the code done to find out about the both ways. You can use a function
EncompassesPoint() or you can use ActorEnteredVolume(). I tested both and both react when spawned in the volume, Which would you like to use? Are you after a spawn protection?
I had to extend the physic volume to get both to work. If you use EncompassesPoint() In the APhysicsVolume i added 3 bools, as i was checking 3 volumes at the same time using that code.
bool bIsACheckPoint;
bool bIsAOBVolume;
bool bIsAWeatherVolume;
Then set each volume to true in its own constructor. If you want to use EncompassesPoint() then you will want the bool for which ever volume you add.
Let me know which you want to use then i can post its code.
Edit: let me get both, i will post both then you can choose what you want to use
This will be for using EncompassesPoint().
In your gametype.h file add in this
#include "GameFramework/PhysicsVolume.h"
DECLARE_LOG_CATEGORY_EXTERN(LogGameType, Log, All);
UCLASS(minimalapi, config=Game)
/** Tries to spawn the player's pawn, at the location returned by FindPlayerStart */
virtual void RestartPlayer(AController* NewPlayer) override;
UFUNCTION()
void CheckIfInVolume(AActor* StartPlace, AController* NewPlayer);
UFUNCTION()
void YouAreInVolume(const APhysicsVolume* VolumeYourIn, AController* NewPlayer);
UPROPERTY(config)
bool bSpawnProtectionOn;
Then in the gametype.cpp file add, and do not super this function, NEW CODE IN RED
void AYourGameMode::RestartPlayer(AController* NewPlayer)
{
UE_LOG(LogGameType, Log, TEXT("AYourGameMode::RestartPlayer() bSpawnProtectionOn = %s"), bSpawnProtectionOn ? TEXT("TRUE") : TEXT("FALSE"));
if (NewPlayer == nullptr || NewPlayer->IsPendingKillPending())
{
return;
}
AActor* StartSpot = FindPlayerStart(NewPlayer);
// If a start spot wasn't found,
if (StartSpot == nullptr)
{
// Check for a previously assigned spot
if (NewPlayer->StartSpot != nullptr)
{
StartSpot = NewPlayer->StartSpot.Get();
UE_LOG(LogGameType, Warning, TEXT("RestartPlayer: Player start not found, using last start spot"));
}
}
CheckIfInVolume(StartSpot, NewPlayer);
UE_LOG(LogGameType, Log, TEXT("AYourGameMode::RestartPlayer() bSpawnProtectionOn = %s"), bSpawnProtectionOn ? TEXT("TRUE") : TEXT("FALSE"));
if (bSpawnProtectionOn)
{
class AYourCharacter* const SPCharacter = Cast<AYourCharacter>(NewPlayer->GetPawn());
UE_LOG(LogGameType, Log, TEXT("APerfectHunt2GameMode::RestartPlayer(); SPCharacter = %s"), *SPCharacter->GetName());
SPCharacter->StartSpawnProtection();//added a bool to control it all
}
RestartPlayerAtPlayerStart(NewPlayer, StartSpot);
}
And add also in gametype.cpp
/*Check if in a volume at spawn time.*/
void AYourGameMode::CheckIfInVolume(AActor* StartPlace, AController* NewPlayer)
{
bool bYouAreInVolume = false;
//scan volumes to see if you are in one
for (auto VolumeIter = GetWorld()->GetNonDefaultPhysicsVolumeIterator(); VolumeIter; ++VolumeIter)// <thanks **[](https://forums.unrealengine.com/member/155-)** for the help.
{
const APhysicsVolume* OurVolume = VolumeIter->Get();
if (OurVolume != nullptr)
{
bYouAreInVolume = OurVolume->EncompassesPoint(StartPlace->GetActorLocation());
}
//you are in a volume go edit the character now
if (bYouAreInVolume)
{
YouAreInVolume(OurVolume, NewPlayer);
break;
}
}
return;
}
And add also in gametype.cpp
void AYourGameMode::YouAreInVolume(const APhysicsVolume* VolumeYouAreIn, AController* NewPlayer)
{
//you are in a volume go edit the character now
UE_LOG(LogGameType, Log, TEXT("YouAreInVolume(); OurVolume = %s"), *VolumeYouAreIn->GetName());
GEngine->AddOnScreenDebugMessage(-1, 1.1f, FColor::Green, FString::Printf(TEXT("You are IN The Volume")));
class AYour2Character* const SPCharacter = Cast<AYour2Character>(NewPlayer->GetPawn());
UE_LOG(LogGameType, Log, TEXT("YouAreInVolume(); SPCharacter = %s"), *SPCharacter->GetName());
UE_LOG(LogGameType, Log, TEXT("YouAreInVolume() bIsAWeatherVolume = %s"), VolumeYouAreIn->bIsAWeatherVolume ? TEXT("TRUE") : TEXT("FALSE"));
UE_LOG(LogGameType, Log, TEXT("YouAreInVolume() bIsACheckPoint = %s"), VolumeYouAreIn->bIsACheckPoint ? TEXT("TRUE") : TEXT("FALSE"));
UE_LOG(LogGameType, Log, TEXT("YouAreInVolume() bIsAOBVolume = %s"), VolumeYouAreIn->bIsAOBVolume ? TEXT("TRUE") : TEXT("FALSE"));
if (VolumeYouAreIn->bIsACheckPoint)//Are you in a check point
{
//in Check Point do stuff
GEngine->AddOnScreenDebugMessage(-1, 1.1f, FColor::Green, FString::Printf(TEXT("Your IN The Check Point Volume")));
//SPCharacter->CPStartSomething();//set Something
}
else if (VolumeYouAreIn->bIsAWeatherVolume)//Are you in a Weather Volume
{
//in indoor Weather Volume do stuff> shut off rain and snow your indoors
GEngine->AddOnScreenDebugMessage(-1, 1.1f, FColor::Green, FString::Printf(TEXT("Your IN The Weather Volume")));
SPCharacter->WeatherStop();//stop the weather
}
else if (VolumeYouAreIn->bIsAOBVolume)//Are you in a Out of Bounds Volume
{
//get the ob volume so we can set the kill time parameter
//const AOutOfBoundsVolume* YourVolume = Cast<AOutOfBoundsVolume>(VolumeYouAreIn);
GEngine->AddOnScreenDebugMessage(-1, 1.1f, FColor::Green, FString::Printf(TEXT("Your IN The Out of Bounds Volume")));
//Start countdown timer to kill player
//do not use, as Enteredthevolume() runs also;> use that Enteredthevolume();
//SPCharacter->OBStartCountDown(YourVolume->killTime, true);
}
return;
}
If you want to use the Enteredthevolume(); In your CheckPoint.h file add
YOUR_API DECLARE_LOG_CATEGORY_EXTERN(LogCPVolume, Log, All);
public:
ACheckPointVolume();
~ACheckPointVolume();
// Called when actor enters a volume
virtual void ActorEnteredVolume(class AActor* Other) override;
// Called when actor leaves a volume, Other can be NULL
virtual void ActorLeavingVolume(class AActor* Other) override;
in CheckPoint.cpp file add
#include "Player/YourCharacter.h"
DEFINE_LOG_CATEGORY(LogCPVolume);
ACheckPointVolume::ACheckPointVolume()
{
bIsACheckPoint = true;
}
//actor touchs the volume
void ACheckPointVolume::ActorEnteredVolume(class AActor* Other)
{
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorEnteredVolume %p is *Other"), Other);
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorEnteredVolume %s is *Other"), *Other->GetName());
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorEnteredVolume() = *Other = %s"), Other ? TEXT("TRUE") : TEXT("FALSE"));
AYourCharacter* const ToucherCharacter = Cast<AYourCharacter>(Other);//ARE WE A WALKER
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorEnteredVolume %p is *ToucherCharacter"), ToucherCharacter);
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorEnteredVolume %s is *ToucherCharacter"), *ToucherCharacter->GetName());
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorEnteredVolume() = *ToucherCharacter = %s"), ToucherCharacter ? TEXT("TRUE") : TEXT("FALSE"));
//keep out projectiles
if (ToucherCharacter == nullptr)//TYPE_OF_NULLPTR
{
return;
}
else if (ToucherCharacter != nullptr)//true that we are in the volume//TYPE_OF_NULLPTR
{
ToucherCharacter->CPStartSomething();
}
}
//actor untouchs the volume
void ACheckPointVolume::ActorLeavingVolume(class AActor* Other)
{
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorLeavingVolume %p is *Other"), Other);
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorLeavingVolume%s is *Other"), *Other->GetName());
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorLeavingVolume() = *Other = %s"), Other ? TEXT("TRUE") : TEXT("FALSE"));
AYourCharacter* const ToucherCharacter = Cast<AYourCharacter>(Other);//ARE WE A WALKER
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorLeavingVolume %p is *ToucherCharacter"), ToucherCharacter);
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorLeavingVolume %s is *ToucherCharacter"), *ToucherCharacter->GetName());
UE_LOG(LogCPVolume, Log, TEXT("ACheckPointVolume::ActorLeavingVolume() = *ToucherCharacter = %s"), ToucherCharacter ? TEXT("TRUE") : TEXT("FALSE"));
//keep out projectiles
if (ToucherCharacter == nullptr)//TYPE_OF_NULLPTR
{
return;
}
else if (ToucherCharacter != nullptr)//true that we are in the volume//TYPE_OF_NULLPTR
{
ToucherCharacter->CPStopSomething();
}
}
Use which ever you want they both will detect that you have spawned in a volume. I tested both ways yesterday in all 3 volumes and all worked perfect. You will need to add what you want it to do to the character. Then you will have it finished up. If you are after spawn protection let me know, i can post that also if interested.