Reference to PlayerController

Hey,

I am completely new to Unreal Engine and C++ programming, although I have a couple of years experience with different Game Engines and C#.
Until my C++ Book arrives, I just wanted to play arround with code.

I created a simple Powerup Class and connected it to a mesh via Blueprint. I made it collectable in c++, and now I want to change some of the PlayerControllers Variables. Therefore I need a reference to it. How do I get that?

Greetings,
Tobi

This depends on how you made them collectable in C++. If you collect them via the ReceiveActorBeginOverlap event or an OnComponentBeginOverlap delegate, you should have been given a pointer to the actor that caused the event.

If you have that pointer (lets call it “TouchingActor”), you can do this.

// Make sure the actor is a Pawn (the player's pawn is the one touching the powerup)
APawn* Pawn = Cast<APawn>(TouchingActor);
if ( Pawn )
{
    // Make sure the Pawn has a player controller. This could be an enemy or something.
    APlayerController* PC = Cast<APlayerController>(Pawn->Controller);
    if ( PC )
    {
        // ... now you can do something to the player controller via the PC pointer!
    }
}

Take a look at this tutorial from Epic Games.

For getting the player that picked up your powerup you can use Cast and CastChecked. Use one of these function for casting the actor that touched your powerup to your character type.

Well, thanks for that, but I wanted to do it in c++. I now have a Pointer to my Charakter, because the overlap event returns it, but how can I access its variables? I guess it is just a c++ Syntax problem. But I cant figure it out by myself :confused:

#Object Iterator

TObjectIterator<APlayerController> ThePC;
If(!ThePC) return;

//~~~~ PC is Valid ~~~
ThePC->Function();

#Note

You can use your custom PC class directly if you have one

TObjectIterator<AMyPCClass>

#Getting Your Character

TObjectIterator ThePC;
    If(!ThePC) return;

//~~~~ PC is Valid ~~~
AYourCharClass* MyChar = Cast<AYourCharClass>(ThePC->GetPawn());
if(!MyChar) return;
MyChar->SetJumpVelocity(...);

#:heart:

Rama

You will have to cast the actor that overlapped your powerup to your character type. I assume there might be a function for doing this, however I don’t have access to UE4, thus I cannot tell you which it is and if it exists.

@Zer0 I’ve found this: Cast< TargetType >(item) this would cast item to TargetType.

I have edited my answer with information on how to get your player character by casting the actor that overlapped your powerup (remember that other actors may overlap your powerup, thus I advise you to check if the overlapping actor is a player before performing your powerup actions). Don’t forget to up vote the answer if it helps you, otherwise do not.

That looks like it should work, unfortunately I only have a Pointer to my Character. I think I can get the reference with foo(), unfortunately the compiler says that it doesn’t knows what it is.

Wow, now that’s confusing. I totally understand what it does, but I have no idea how to implement it. Maybe you can help me out here.
I got this in my Character header file:

void SetJumpVelocity(float NewVelocity);

This in my Character cpp file:

void ACodeTestCharacter::SetJumpVelocity(float NewVelocity)
{
	CharacterMovement->JumpZVelocity = NewVelocity;
}

And this in my PowerUp.cpp file:

void APowerUp::OnOverlap(AActor* OtherActor) {
	Destroy(false, false);
}

How can I use your code to call SetJumpVelocity?

foo() is a name given to functions in examples (together with bar()). Where did you get that foo() from?

Yeah, that was really stupid… I misread an example. So now I got this:

ACodeTestCharacter MyPC = Cast<ACodeTestCharacter>(*OtherActor);

but that doesn’t seem to work either. (ACodeTestCharacter is my custom Character class)

MyPC should be of type ACodeTestCharacter *. After that to access variables and functions of MyPC you use the dereference operator like so:
MyPC->foo().

That actually worked :smiley: Thanks alot!
Although I now think, that I should wait until my c++ book arrives, before trying to play around with the code again. My whole c# knowledge seems to be completely useless for this.

In the meantime I suggest you look at some C++ tutorials on variables and pointers. Just so you know the -> operator is the equivalent of . for variables.

Ah, I figured it out. Thanks alot. It seems like I have to learn a lot until I can use c++ properly ^^