x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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. alt text

alt text

Product Version: UE 4.22
Tags:
u1.jpg (17.7 kB)
u2.jpg (14.3 kB)
u3.jpg (48.7 kB)
more ▼

asked May 16 '19 at 01:35 PM in C++ Programming

avatar image

Headhunter
1 1 2

avatar image Headhunter May 16 '19 at 01:53 PM

I may have fixed it with this alt text

u4.jpg (19.0 kB)
avatar image norlin May 16 '19 at 07:48 PM

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...

avatar image Headhunter May 17 '19 at 05:57 AM

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. alt textalt text

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

capture.jpg (102.1 kB)
2.jpg (80.3 kB)
avatar image Headhunter May 17 '19 at 06:37 AM

I though i'll also show the code i currently use for shooting.alt text alt text alt text

shoot1.jpg (90.4 kB)
shoot-2.jpg (83.0 kB)
shoot3.jpg (57.8 kB)
avatar image Shadowriver May 16 '19 at 07:03 PM

You want to do camera switching?

avatar image Headhunter May 17 '19 at 05:58 AM

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

avatar image Shadowriver May 17 '19 at 10:12 AM

then why you setting component in the player?

avatar image Headhunter May 17 '19 at 10:20 AM

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. alt text

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

capture.jpg (15.1 kB)
avatar image Headhunter May 17 '19 at 10:47 AM

Here are the shooting functions too alt text

capture.jpg (95.3 kB)
avatar image Lardo Deepdelver May 17 '19 at 11:34 AM

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();

avatar image Headhunter May 17 '19 at 12:00 PM

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 alt text alt text alt text

avatar image Lardo Deepdelver May 17 '19 at 03:02 PM

WeaponHolder can be a Pawn or a child class of Pawn

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

avatar image Headhunter May 18 '19 at 06:14 AM

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?

avatar image norlin May 18 '19 at 06:43 AM

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

avatar image Headhunter May 18 '19 at 07:14 AM

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

avatar image norlin May 18 '19 at 07:17 AM

It's an internal UE4 method (https://api.unrealengine.com/INT/API/Runtime/Engine/GameFramework/AController/GetPlayerViewPoint/index.html)

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

avatar image Headhunter May 18 '19 at 07:27 AM

Ah okay. So it detects the players camera component?

avatar image Headhunter May 18 '19 at 07:49 AM

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

Does this all seem good so far?

capture.jpg (19.1 kB)
2.jpg (23.5 kB)
avatar image Lardo Deepdelver May 20 '19 at 02:24 PM

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++

avatar image Headhunter May 20 '19 at 02:31 PM

Ah okay. I just found it easier to make a blueprint from the c++ class as then i could easily change weapon types etc.

avatar image Lardo Deepdelver May 20 '19 at 03:10 PM

You can still create a blueprint from C++ which I wanted to say with

Use that class in Blueprint if you want to

The only thing I suggested is that your player gets a variable

 UPROPERTY(EditAnywhere)
 TSubclassOf<AWeapon> WeaponClassToSpawn;


this value can then be set in the blueprint of your actor to the weapon blueprint. (the weapon blueprint has to inherit from AWeapon)

and then spawn with:

 AWeapon* NewActor = GetWorld()->SpawnActor<AWeapon>(WeaponClassToSpawn, NewLocation, FRotator::ZeroRotator);  

if you do it that way, you don't need to define your blueprint class in C++ and don't need any absolute paths

also if you ever want different weapons you just change your WeaponClassToSpawn to a different class and that's that

avatar image Lardo Deepdelver May 20 '19 at 02:26 PM

as a side note, AnswerHub has a Code Style options to post code in posts. Should make it easier to post and read :P

alt text

codestyle.png (27.3 kB)
avatar image Headhunter May 20 '19 at 02:31 PM

Thanks for letting me know

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question