View Full Version : Very Confused on this...
Taxxem
11-10-2009, 06:22 PM
As an earlier post I was attempting to create a sprint ability where you hold a key down and off you go. However in the process of me doing this I ran into a slight issue. As another forum member mentioned to look at walking and use it as a reference, I did.
What has happened is in the Pawn there is a function called SetWalking.
This is called inside of the PlayerController class by Pawn.SetWalking...
I created my own Pawn class called PBPawn and extended it to Pawn.
Inside there I created SetSprinting. However when I call Pawn.SetWalking inside my new PlayerController class it yells saying Pawn doesn't have this function... ok no problem. I changed it to PBPawn.SetWalking...
Then it yells at me saying PBPawn is a bad command or expression.
What exactly am I doing wrong?
BryanRobertson
11-10-2009, 08:34 PM
The problem is that PlayerController.Pawn is a reference to an object of type "Pawn", not your derived class.
The compiler has no way to know from looking at your code, that the PlayerController.Pawn variable is pointing to an instance of a derived class, so you can only access variables and functions declared in the Pawn class (or any of the classes it derives from). This is because UnrealScript is what's known as a "Statically typed language". You can store a reference to a PBPawn in a Pawn variable, but you can't access members that were declared in PBPawn, without casting (converting) the variable to the type PBPawn.
(This would work in a dynamically typed language like Lua or Python, because in these languages, the member is looked up at runtime as opposed to compile time)
The reason PBPawn.SetWalking doesn't work, is because PBPawn is a class name, not the name of a variable. The only reason Pawn.SetWalking works, is because Pawn is also the name of a variable in the PlayerController class.
The solution:
If you have a variable of type Pawn, which refers to an object of a derived class, you have to "cast" the variable to type PBPawn before you can access any members that were declared in the PBPawn class. Here's an example of how you would do this
local PBPawn myPBPawn;
myPBPawn = PBPawn(self.Pawn);
if ( myPBPawn != none )
{
// myPBPawn will be none if the cast failed.
// this would happen if Pawn isn't of type PBPawn
// Do whatever you want to myPBPawn here
}
This link might it explain it a bit better
http://udn.epicgames.com/Three/UnrealScriptReference.html#Converting%20object%20r eferences%20among%20classes
Taxxem
11-10-2009, 09:52 PM
I greatly appreciate that bit of help. I have another question maybe you can help or someone else can help with. I have a class that extends from PlayerController in the Engine file called PBPlayerControllerEngine. I also have a class that extends from the UTPlayerController class called PBPlayerController. If I create a variable inside the PBPlayerControllerEngine class (extends PlayerController) and I want to access it inside the PBPlayerController (extends UTPlayerController) class is that possible? The reason why I ask is because the heirarchy is broken when I step away from PlayerController and make a "new version" so to speak.
I already understand that UTPlayerController -> GamePlayerController -> PlayerController.
But it breaks because of the class I need extends PlayerController.
Any ideas?
Taxxem
11-10-2009, 10:31 PM
Nevermind my 2nd question it just hit me.
PuckerFactor
11-10-2009, 10:59 PM
Nevermind my 2nd question it just hit me.
wanna share that revelation?
Taxxem
11-11-2009, 01:21 PM
Sure. Basically I wasn't thinking of the heirarchy correctly. I created a class that extends PlayerController and a class that extends UtPlayerController. This was sorta silly since PlayerController is a pretty much a "grandparent" so to speak of UTPlayerController. So I honestly don't need to extend from PlayerController I can just extend from UTPlayerController and change whatever I need inside PlayerController from there...
UTPlayerController -> GameController -> PlayerController
A lot of back and forth... hope that helps
Powered by vBulletin® Version 4.2.0 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.