I used a pointer APawn->FaceRotation(); in my playercontroller class but it says missing ; before ->? What is that supposed to mean?
Hello
Unlike Unrealscript that uses the dot operator for accessing public data or protected (if derived) from a class, C++ has the option to use the arrow operator provided that the left operand is a pointer to a class/structure or union but also has the option to use the dot operator. The reason for using the arrow operator is due to operator precedence, the dot operator has a higher precedence than the dereference operator (the star) so essentially the pointer is not being dereferenced first unless parenthesis are used, hence PointerToClass->Variable is equivalent to ( *PointerToClass ).Variable, the missing semicolon error occurs because you are trying to dereference and access a variable from the class name but not a pointer to the class and declaring the class name with a semicolon is legal in C++ i.e. APawn; is legal, though it’s not something that will help you in this case. As said above using ‘Pawn’ the pointer to APawn should enable you to access FaceRotation.
I should also add that Pawn is private and not protected hence in this case, you’ll have to use a getter, GetPawn() is what you’re looking for.
Thanks.
Ah you’re right. I totally missed that the pawn variable had a private specifier! Just out of curiosity: why does the pawn require a getter for it now? That doesn’t make sense to me, especially because all it does is return the reference to pawn anyways (according to the controller header file).
I imagine there’s more to this than meets the eye, but it’d be nice to understand the rationale. Is it just coding convention? Or is there more going on/planned under the hood than what the header files reveal?
Private variables are mostly used to enforce encapsulation, the reason for using getters and setters is usually to add validation before actually modifying the variable or returning it to the caller, you may also have noticed that GetPawn is a constant member function which essentially makes it read-only, that assures that this function does not modify any member variables but only reads them, private variables are also used to ensure that the variable itself does not accidentally get modified by a derived or non-derived class and if it’s been modified and has caused issues, it can be traced back to the class where it was declared.
As for the last question, I am not aware of that.
I figured as much about the encapsulation part. Just figured there was more to it than that.
Usually with unrealscript, you had getters and setters for a specific variable instance because there was some additional native backend that had to do stuff to the variable first before it was updated. So I thought the same logic would apply here as well. Bah.
Thanks for the insight. I won’t hijack Mr. Eshwar’s thread anymore.
Thank you guys it worked now but of course i need to optimize because im spinning like a top in the game.
Probably because you’re referencing the pawn class instead of referencing your pawn as an instance.
Just like in unrealscript, PlayerController derives from Controller. So look in controller.h and you will see a member variable called “Pawn” of type APawn*. So you use that to access FaceRotation.
TLDR? In your custom playercontroller, try this instead:
Pawn->FaceRotation();
Yes that worked thanks. Next problem is that FaceRotation() has too few arguments so I put the parameters DeltaTime and NewRotation but it says NewRotation undeclared identifier
Well, the issue in that case is that C++ doesn’t know what NewRotation is because you either:
- Did not declare the variable NewRotation in your header file, or
- Did not create the variable within the scope of the function, so you can have something to pass to FaceRotation.
Since you’re just trying to get this stuff working right now (as opposed to optimization), I would recommend just declaring NewRotation in your header file:
customplayercontroller.h:
FRotator NewRotator;
Then in customplayercontroller.cpp, define your custom function, like this:
void CustomClass::YourCustomFunction(float DeltaTime)
{
// In UE3, Rotators always took three integer arguments -- Pitch/Yaw/Roll, in that order. (Think: PYR, synonymous with "fire"). I imagine it's the same here as well, so assign the FRotator like this:
NewRotator = FRotator(0,32768,0)
Pawn->FaceRotation(NewRotator, DeltaTime);
}
I don’t know exactly what you’re trying to do at this point, so the issue of variable scope is something you need to play around with to suit your own ends. But just experiment with the code and see what results from it.
What i am trying to do is call UpdateRotation function. In it i want to call FaceRotation to face the direction it is moving to. FaceRotation has to parameter one is NewRotation. To call it i first did just facerotation() without anything but i got error cannot have 0 arguments so i put Newrotation and Deltatime and this is wat i get.
So my answer again is… you need to declare “NewRotator” somewhere (or if there’s a name conflict in the file, just name “NewRotator” something else).
Either declare it in your header file, or as a variable with a local scope in the overridden UpdateRotation() function. DeltaTime is a parameter passed in through UpdateRotation, so you’re at least covered there.
As for turning based on input, I can’t help you there. That’s something pretty specific and involved; however, you might be able to get something decent on your own by looking at these questions:
Typecasting Fvector to FRotator, and FRotator to FVector
Look for a way to analyze the input variables, and assemble a directional vector that you can convert to a rotator that you can pass into FaceRotation.
Good luck.
Ok so i did this similar to wat u said and i get two errors:
treated as error- no ‘object’ file generated in the line where the pointer is P->FaceRotation
and uninitialized local variable P used?
here is the code
void AShooterPlayerController::UpdateRotation(float DeltaTime)
{
FRotator NewRotation, ViewRotation;
APawn* P;
NewRotation = ViewRotation;
ViewRotation = GetActorRotation();
P->FaceRotation(NewRotation, DeltaTime);
}
Why are you declaring a local pawn variable “P”? There is a global pawn instance from controller already! Just use
Pawn->FaceRotation( NewRotation, DeltaTime);
like I said in my very first reply, and remove
APawn* P;
P->FaceRotation(NewRotation, DeltaTime);
from updaterotation.
Pawn is also a global variable declared in controller.h, so that variable can be used ANYWHERE in its own class and subclasses.
I recommend you look for a good C++ primer before you get started coding, because this is not a “quick question” anymore. It’s becoming an endless barrage of questions – evidence that you need to understand the language first before attempting any coding with Rocket’s architecture. Here is a beginning C++ primer to help you get you started.
It’s imperative to learn the language on your own to get a better handle on it. Either that or perhaps somebody else on here can help answer your basic C++ questions from this point. Good luck.
Oh i tried that before but i think i got an error i will try again.
There you go it worked thanks