Announcement

Collapse

The Infinity Blade Forums Have Moved

We've launched brand new Infinity Blade forums with improved features and revamped layout. We've also included a complete archive of the previous posts. Come check out the new Infinity Blade forums.
See more
See less

Typecasting tutorial?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Typecasting tutorial?

    Hi there,
    I have one particular problem and one general request:
    - Problem:
    I have a vector variable stored in my playercontroller class. I would like to access it from a class directly extending Actor. I've tried a lot of things to do this but none of them work. How do I do that?

    - Request:
    I see a lot of threads in these forums about typecasting: people want to access variables and functions of a class while coding in another one.
    Seems this thing isn't really easy in unrealscript as there are different solutions according to the particular situation you are. And, since I try to learn unrealscript, this is a thing I badly understand.
    I've read the "Using Objects" article in the Unreal Wiki Legacy, I've read the Epic Doc (which doesn't explain details) but this is not enough to fully understand this point of programming.
    So: is there, somewhere on the forums or on the net, a real good and complete introduction on typecasting in unrealscript?
    Thanks for any information and sorry for my english!

  • #2
    There is only one way to typecast a variable:

    Code:
    TypeToCastTo(VariableToCast)
    If a class contains a variable of a base type (PlayerController) and you want to access variables or functions in your subclass (MyPlayerController), it is simply a matter of casting that base type variable to your type:

    Code:
    MyPlayerController(ControllerVariable)
    Assuming the object referenced by the ControllerVariable is a MyPlayerController, you would then be able to access the variables or functions of it:

    Code:
    MyPlayerController(ControllerVariable).VariableName
    If the object is not a MyPlayerController, the result of the cast is None. This is why it is always a good idea to save the cast to another variable of the cast type and then check it before using it:

    Code:
    Local MyPlayerController MyPC;
    
    MyPC = MyPlayerController(ControllerVariable);
    
    If(MyPC != none)
    {
        //do stuff here
    }

    Comment


    • #3
      Regarding the problem:
      For a single player game, this can help
      Code:
      local Vector V;
      V = MyPlayerControllerClassName(WorldInfo.Game.GetALocalPlayerController()).MyVector;

      Regarding the request:

      Read this article, I hope it helps you.

      Comment


      • #4
        It sounds like you don't understand the different types of variables, rather than typecasting. Typecasting is simple, as ffejnosliw explains. Another way of restating it, would be something like ..

        Typecasting is used to return a subclass of a particular class.

        If you have an Actor variable , A, that happens to contain a Pawn .. by just accessing the Actor variable, you can only access things that are contained within Actor. By promoting it to a Pawn, you can then access things that are contained also within the Pawn class. Pawn(A) returns a reference to A that is of type Pawn, rather than type Actor. If A is not a Pawn, but rather some other kind of actor, such as a Controller, then Pawn(A) returns None.

        (yes, i'm trying to re-state what ffejnosliw said, because if the first two documents you pointed to didn't help you, maybe saying the same thing in different ways will .. and no, that's not a slight, i've worked in customer service for a very long time, if i don't find the right way to explain something to someone, they might never get it)

        Comment


        • #5
          FILIDAGREAT:
          It works!!! Thanks a lot!
          What does this almost magical line of code do? What is this: (WorldInfo.Game.GetALocalPlayerController())

          For the link to the article: acces non authorized. Maybe it is only for licencees?


          FFEJNOSLIW:
          Ok, I think I get it. In fact, as Blade said, typecasting is only used to TRANSFORM a variable referencing to an object of a class (transform it into a variable referencing to an object of a subclass). Am I right?
          I thought the word meant: "transforming or creating such a reference".
          So my next question is: how do you CREATE a reference to an object?
          I mean: for example, in the HUD.uc file, there is a variable "PlayerOwner", of type PlayerController. If you decide to write your own PlayerController class and want to access its new functions and variables from HUD, you will typecast PlayerOwner to do so. Right?
          But the "PlayerOwner" variable itself: how does it work? Once you've declared it:
          var PlayerController PlayerOwner
          what do you do so it works, so it really gives acces to the PlayerController functions and variables? Is there some code to add, further in the class? Didn't see it...

          Comment


          • #6
            Reading again the HUD.uc script, I found this:
            Code:
            event PostBeginPlay()
            {
            	super.PostBeginPlay();
            
            	PlayerOwner = PlayerController(Owner);
            }
            It is this way that "PlayerOwner" is given a value? Seems like a typecasting of a variable called "Owner". So, this variable should be declared in the super-class of HUD.
            So, I went to Actor.uc and found:
            Code:
            var const Actor	Owner;
            A variable named "Owner" of type Actor, inside the Actor class: ????

            To be continued....

            Comment


            • #7
              When a player is initialized in the GameInfo class, the PlayerController spawns the HUD passing itself to the Spawn() function as the Owner. That is how the Owner variable gets its value set. So now the HUD's Owner is the local PlayerController, but since Owner is an Actor and there are frequently things the HUD needs to do that require accessing the PlayerController's variables and functions, the HUD declares a new variable PlayerOwner that is a PlayerController and sets its value in the PostBeginPlay() function by casting the Owner to a PlayerController.

              Comment


              • #8
                ...and yes, as you've discovered, a class can contain a reference to any other class, including the same type as what it's declared in, just as long as the class is compiled before the class that is referencing it.

                Comment


                • #9
                  OK. These are precious information in order to navigate and code through unrealscript files. Thanks a lot!
                  So now, leaving aside relations between Actor, PlayerController and HUD, here is another case:
                  Suppose you have 2 classes extending from actor. Refering to the "Ambient creatures" tutorial in one of the Mastering Unreal chapters, let's say you have:
                  - class 1: AmbientDemons extending Actor.
                  - class 2: AmbientAngels extending Actor.

                  Once you have declared:
                  Code:
                  var AmbientDemons Demon;
                  in AmbientAngels.uc

                  and
                  Code:
                  var AmbientAngels Angel;
                  in AnbientDemons.uc

                  How do you set values of theses "Angel" and "Demon" variables so that
                  each class can acces variables and values of the other?

                  Comment


                  • #10
                    I mean: variables and functions of the other.

                    Comment


                    • #11
                      You would have to have instances of each of those spawned in the world to start off. Then, you would have to get a reference to each one and use that to set the value. It's hard to give a concrete way to do this without knowing the whole setup of your classes and how things are being done. One way that would work is using an iterator such as DynamicActors using the desired class as a filter, but that isn't the most efficient of things.

                      If there is an event that occurs (such as one touches the other) that is responsible for an Angel being set as the Demon's Angel, then you can usually use the function responsible for that event to get the reference and set the value. For instance, in Touch() you have the Actor Other parameter which references the Actor that did the touching. That could be typecast to the correct class and used to set the value. Again, it really just depends on game-specific things that make it hard to give generalized methods.

                      Comment


                      • #12
                        Again, it really just depends on game-specific things that make it hard to give generalized methods.
                        Yes, I can figure it out.
                        I'll try to come back with two simple classes of creatures with very simple behaviours for each one. That will be a good - and difficult exercice - for a young unrealscript padawan!

                        Comment


                        • #13
                          By the way, could someone explain the line of code given by FiliDaGreat:
                          Code:
                          local Vector V;
                          V = MyPlayerControllerClassName(WorldInfo.Game.GetALocalPlayerController()).MyVector;
                          What does this "WorldInfo.Game.Function()" mean?
                          Could I re-use such a syntax to acces my Custom Pawn Class or other classes?

                          Comment


                          • #14
                            Left to right:
                            • Assign to variable V
                            • MyPlayerControllerClassName presumably is a class name, so the expression in the following pair of parentheses is to be typecasted to that class.
                            • Inside the parentheses: Access variable WorldInfo, access the WorldInfo's Game, call the Game's GetALocalPlayerController() function.
                            • Of the typecasted result, access the MyVector variable. (this is the final result, so it is assigned to V)

                            Comment


                            • #15
                              Ok, this is a line of code to typecast a variable to MyPlayerController Type.
                              Going to "Engine" folder of UDK, I found a WorldInfo.uc.
                              In this script, I found a variable named "Game", of type GameInfo but didn't find a function called "GetALocalPlayerController()" and a variable named "WorldInfo". I found this variable declared in Actor.uc.

                              So, I have 2 questions:
                              where is the function located? In which script?
                              How the "WorldInfo" variable has a value assigned to it? Reading Actor.uc, I didn't understand this.

                              Comment

                              Working...
                              X