New to Unreal. Want to reference the player camera

I’m just coming from using Unity and am finding the transition to this engine pretty confusing. Basicly all i want to do is reference the Player and the players camera in the weapon script but i keep getting a pretty serious error.

276980-u2.jpg

I may have fixed it with this

You want to do camera switching?

It could be the fix, in fact it’s just a check that Player is not a nullptr. Also, it’s better to use IsValid(Player) check – it will check both nullptr and if the actor is pending to kill.

This check can help or can just hide the real problem – it’s not clear since there is not much to see on the screenshots.
The most unclear thing is why are you trying to set the camera fro the Player from inside of the Weapon… I assume you may have some not really correct architecture for these classes…

Sorry for not really being clear. I’m trying to reference the player camera inside the weapon class to use it as the point the weapon line traces from. I’m trying to do this similar to they i i have done it in Unity.

I’m not sure if you have had any Unity experience but i hope this helps explain what i’m trying to do.

I’m trying to reference the player camera inside the weapon class to use it as the point the weapon line traces from.

I though i’ll also show the code i currently use for shooting.

then why you setting component in the player?

I must have understood what i’m doing completely wrong. I was under the impression that i was finding the player, getting the players camera component and then assigning that as the camera reference. Is there correct? If it is i feel a little silly.

277057-capture.jpg

Sorry for the dumb questions, there are quite a few differences between Unity and Unreal and i’m trying to teach myself.

Here are the shooting functions too

I would recommend not to store a pointer to the player camera at all,
wouldn’t even store a pointer to the player holding the weapon but only a pointer to the actor holding the weapon, the weapon holder should be of type Pawn or a child class of Pawn

then you can do something like this:

auto* controller = Cast<APlayerController>(WeaponHolder->GetController());
FVector PlayerViewLocation;
FRotator PlayerViewRotation;
controller->GetPlayerViewPoint(PlayerViewLocation, PlayerViewRotation);
FVector PlayerViewDirection = PlayerViewRotation.Vector();

I’m sorry, since i’m still pretty new to this i’m pretty confused. Are you saying i should make a weapon holder component for the player. The way i’m done the player is that i’ve written it using the pawn class instead of the character class because i’m trying to rewrite code from my unity project that has quake style movement. Would i then reference the players camera in the weapon holder? I’ll show you a bit of my player script

WeaponHolder can be a Pawn or a child class of Pawn

it just needs to have the GetController() function, which is implemented by Pawn

So it would be best to create the WeaponHolder inheriting from the pawn class and the add that as a component to the player and then reference the AmyPlayer classes controller?. Then when i spawn the weapon i attach the weapon to the root component of the WeaponHolder? Sorry for all the questions but would you be able to provide an example of how the WeaponHolder class would work?

Not exactly. You don’t need to make any additional WeaponHolder class. It’s about how the weapon will know who’s holding it.

You can add a new variable to the AWeapon class: APawn* WeaponHolder; (add the UPROPERTY macro for it as well, for proper garbage collection)

Then when you spawn an instance of the AWeapon class, set this variable to the pawn which is holding it (so in your case it will be the player’s pawn).

After that, you will be able to get the controller of the WeaponHolder and use it as Lardo suggested above

p.s I’d recommend you to look some basic tutorials or articles about c++ syntax, pointers etc, as well as the unreal basic classes. There are good stuff for free on the UE4 website and youtube; or a paid course on udemy.com/unrealengine

That makes a lot more sense. I’m still a little confused as to how the controller gets the player camera position and rotation.

It’s an internal UE4 method (AController::GetPlayerViewPoint | Unreal Engine Documentation)

Though you can look into the source code to find how exactly it’s working

Ah okay. So it detects the players camera component?

I’ve added what you’ve said to my code apart from getting the player’s veiw rotation and position.

277212-2.jpg

Does this all seem good so far?

the AmyPlayer bit looks fine

I wouldn’t do the part to include a BlueprintClass in C++
I would recommend:

  • Implementing a base AWeapon class in C++
  • Use that class in Blueprint if you want to
  • Spawn the weapon from a TSubclassof

which would allow you not to have absolute paths in C++