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?
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!
}
}
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
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.
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:
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 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.