Cast does not work?

I have been trying to cast and actor class to a pawn class or a player controller class. Here are my results



void AFUOBVolume::ActorEnteredVolume(class AActor* Other)
{
    UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %s is *Other = "), Other);
    UE_LOG(LogVolume, Log, TEXT("AOutOfBoundsVolume::ActorEnteredVolume() = *Other = %s"), Other ? TEXT("TRUE") : TEXT("FALSE"));

    //is there a touching pawn in here
    class ATestPawn* const ToucherPawn = Cast<ATestPawn>(Other);
    UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %s is *ToucherPawn = "), ToucherPawn);
    UE_LOG(LogVolume, Log, TEXT("AOutOfBoundsVolume::ActorEnteredVolume() = *ToucherPawn = %s"), ToucherPawn ? TEXT("TRUE") : TEXT("FALSE"));

    class ATestPlayerController* const ToucherPc = Cast<ATestPlayerController>(Other);
    UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %s is *ToucherPc = "), ToucherPc);
    UE_LOG(LogVolume, Log, TEXT("AOutOfBoundsVolume::ActorEnteredVolume() = ToucherPc = %s"), ToucherPc ? TEXT("TRUE") : TEXT("FALSE"));
}


Here are my log results.
[2020.11.10-22.13.52:282][709]LogVolume: AFUOBVolume::ActorEnteredVolume ʠ鶓翹 is *Other =
[2020.11.10-22.13.52:282][709]LogVolume: AOutOfBoundsVolume::ActorEnteredVolume() = *Other = TRUE
[2020.11.10-22.13.52:306][709]LogVolume: AFUOBVolume::ActorEnteredVolume (null) is *ToucherPawn =
[2020.11.10-22.13.52:307][709]LogVolume: AOutOfBoundsVolume::ActorEnteredVolume() = *ToucherPawn = FALSE
[2020.11.10-22.13.52:307][709]LogVolume: AFUOBVolume::ActorEnteredVolume (null) is *ToucherPc =
[2020.11.10-22.13.52:307][709]LogVolume: AOutOfBoundsVolume::ActorEnteredVolume() = ToucherPc = FALSE

Also why do i not have a memory address in the first log line. Why is it in Chinese and referring to a kind of bird? Where this code get called from?
ActorEnteredVolume(class AActor* Other)

i get the same results on leaving the volume.
[2020.11.10-22.13.54:758][986]LogVolume: AFUOBVolume::::ActorLeavingVolume() ʠ鶓翹 is *Other =
[2020.11.10-22.13.54:758][986]LogVolume: AOutOfBoundsVolume::ActorLeavingVolume() = Other = TRUE
[2020.11.10-22.13.54:758][986]LogVolume: AFUOBVolume::::ActorLeavingVolume() (null) is *ToucherPawn =
[2020.11.10-22.13.54:758][986]LogVolume: AOutOfBoundsVolume::ActorLeavingVolume() = *ToucherPawn = FALSE
[2020.11.10-22.13.54:758][986]LogVolume: AFUOBVolume::::ActorLeavingVolume() (null) is *ToucherPc =
[2020.11.10-22.13.54:758][986]LogVolume: AOutOfBoundsVolume::ActorLeavingVolume() = ToucherPc = FALSE

thanks for reading, Anyone have any ideas?

To print the memory address use %p instead of %s. Try something like this:



UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %p is *Other = "), Other);


or:



UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %p is *Other = "), (void*)Other);


Greetings.

Unrelated tip, you can use FUNCTION in your log messages so you don’t have to type the function name.



 UE_LOG(LogVolume, Log, TEXT("%s() - ")
 , TEXT(__FUNCTION__)
 );

hey thanks for the tips.
ʠ鶓翹 = bird up or is it fk up because i made fk up in collision settings?

Normally, to print a string using the %s variant, you need to precede the actual string with a *

If an FString is returned, true. FUNCTION returns a const char

@gamepainters If you want the name of the object use *Other->GetName();

thanks guys for all the tips appreciated.

Sweet i got it solved. i was extending Apawn to my ATestpawn. I just realized the character class extends the pawn. I moved my pawn code into the character class file. Now it works



[2020.11.11-18.44.22:956][914]LogVolume: AFUOBVolume::ActorEnteredVolume 0000020ED2DDDD00 is *Other
[2020.11.11-18.44.22:956][914]LogVolume: AFUOBVolume::ActorEnteredVolume FirstPersonCharacter_C_0 is *Other
[2020.11.11-18.44.22:956][914]LogVolume: AOutOfBoundsVolume::ActorEnteredVolume() = *Other = TRUE

[2020.11.11-18.44.22:956][914]LogVolume: AFUOBVolume::ActorEnteredVolume 0000020ED2DDDD00 is *ToucherPawn
[2020.11.11-18.44.22:956][914]LogVolume: AFUOBVolume::ActorEnteredVolume FirstPersonCharacter_C_0 is *ToucherPawn
[2020.11.11-18.44.22:956][914]LogVolume: AOutOfBoundsVolume::ActorEnteredVolume() = *ToucherPawn = TRUE

[2020.11.11-18.44.22:956][914]LogVolume: AFUOBVolume::ActorEnteredVolume 0000020ED2D14F00 is *ToucherPc
[2020.11.11-18.44.22:956][914]LogVolume: AFUOBVolume::ActorEnteredVolume TestPlayerController_0 is *ToucherPc
[2020.11.11-18.44.22:956][914]LogVolume: AOutOfBoundsVolume::ActorEnteredVolume() = ToucherPc = TRUE


Thanks for all the info everyone.

How do you get an int to show in logs what %letter do you use or where are these at in code, so i can see what i have to use for my needs.

This should help Logging | Unreal Engine Community Wiki

Thanks for the info. I tried %i and it showed my ints in log :slight_smile:

Edit: After reading that page its saying %d for ints. I used an %i, did it get changed?

Edit: I have a question about null. I see that you can use null or nullptr but it says to use TYPE_OF_NULLPTR to be compatible with new gaming systems coming. If i use TYPE_OF_NULLPTR it causes error and will not compile. I see in my above log it logs null. So should i use null not the other 2? kind of confused on which to use?

One more question, this is a weird one.
If i have code that looks like this



void AFUOBVolume::ActorEnteredVolume(class AActor* Other)
{
UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %p is *Other"), Other);
UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %s is *Other"), *Other->GetName());
UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume() = *Other = %s"), Other ? TEXT("TRUE") : TEXT("FALSE"));


AUnrealCharacter* const ToucherCharacter = Cast<AUnrealCharacter>(Other);//ARE WE A WALKER
UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %p is *ToucherCharacter"), ToucherCharacter);
UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume %s is *ToucherCharacter"), *ToucherCharacter->GetName());
UE_LOG(LogVolume, Log, TEXT("AFUOBVolume::ActorEnteredVolume() = *ToucherCharacter = %s"), ToucherCharacter ? TEXT("TRUE") : TEXT("FALSE"));

//keep out projectiles
if(ToucherCharacter == nullptr)//TYPE_OF_NULLPTR
{
    AUnrealVehicle* const ToucherVehicle = Cast<AUnrealVehicle>(Other);//ARE WE A VEHICLE

   if((ToucherCharacter == nullptr) && (ToucherVehicle == nullptr))
     return;

    //have to use  ToucherVehicle here or wont work out side if,   wth?
}

//ToucherCharacter WILL WORK HERE AS IT WAS INTIATILIZED  ABOVE THE IF STATEMENT.  LIKE IT SHOULD ANY WHERE IN THIS FUNCTION
    if (ToucherCharacter != nullptr)//true that we are in the volume//TYPE_OF_NULLPTR
    {
      ToucherCharacter->OBStartCountDown(killTime, true);
    }

//TOUCHERVEHICLE WILL NOT WORK HERE AS IT WAS INTIATILIZED IN THE ABOVE IF STATEMENT,   WHY? SHOULD IT NOT WORK ANYWHERE IN THIS FUNCTION?
    if (ToucherVehicle != nullptr)//true that we are in the volume//TYPE_OF_NULLPTR
    {
        ToucherVehicle->OBStartCountDown(killTime, true);
    }

}


ToucherVehicle > will not work in the last if statement. If i put the bottom if in the if (ToucherCharacter == nullptr) <it was set in(blue if), then it will work. Should it not work everywhere in that function as it was set in the function?

As if this go against the rule> if its defined and initialized in a function it can be used anywhere in that function?