Tegleg Records

Tegleg Records
Tegleg Records

Thursday, 24 March 2011

Dynamicaly changing the Player Pawn

How to Dynamicaly change the Player Pawn.
Put this in your Player Controller. The example here is using an 'exec' function so it can be triggered from a button press or console command.


exec function FPress()
{
//declare the variables
local pawn p;
local vector l;
local rotator r; 

//set the variables 
p = Pawn;
l = Pawn.Location;
r = Pawn.Rotation; 

// get rid of the old pawn
UnPossess();
p.Destroy(); 

//spawn a new pawn and possess it
p = Spawn(class'SomePawnClass', , ,l,r);

//use false if you spawned a character and true for a vehicle
Possess(p, false);
}

11 comments:

  1. I get an error at
    p = Spawn(class'name', , ,l,r);

    i tried using class'UTPawn', but i get same error.

    Bar or missing experssion in "="

    ReplyDelete
  2. change this variable to whatever class your using
    local pawn p;

    if you use UTPawn
    local UTPawn p;

    ReplyDelete
  3. what if you wanted to possess a pawn already in the level? (rather than Spawn()) ?

    ReplyDelete
    Replies
    1. you can do it simply by using the controller class and call:
      Possess(Pawn, bool); //call unpossess before aswell.

      to spawn a character in front of your character, you could do something as storing your offset, and then use the function getAxes.

      Just multiply the different axis with ur offset. Think that'll do it =).

      Delete
  4. you need to get a reference to the said pawn and possess it.

    ReplyDelete
  5. sorry its been a while, but ive come back to this, how do you actually reference a pawn in the level?
    ive never done this before - grabbing a variable from the current level to pass through some script

    ReplyDelete
  6. hi again, if you want a character to Spawn infront of the character how do i do this?
    im doing a side scroller
    at the moment i have:
    ----------------------
    local Vector PLoc;
    local Pawn Player2;

    PLoc = Pawn.Location; //player1
    PLoc.X = (Pawn.Location.X + 40);
    Player2 = spawn(class'Player2', , ,PLoc,);

    --------------
    this obviously just sticks the new player at +40 from the pawn along the X regardless of which was he is facing
    how do i change that so its always infront of him with which way he is facing?

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. Very useful - thanks for posting this!

    I've written a variant that accepts a string className as an argument, but unfortunately can't paste it intact here (angle braces get interpreted as html).

    To make the following code usable, correct the declaration of the NewClass variable:
    local class(actor) NewClass;
    // replace the parens with angle braces, and fix the NewClass assignment:
    NewClass = class(actor)(DynamicLoadObject(ClassName, class'Class'));
    again replacing the parens around actor with angel braces.

    /**
    * Removes this controller from the existing pawn and possesses a new one. (exec)
    *
    * @param ClassName The classname of the WMPawn to possess
    */
    exec function ChangePawn(string ClassName)
    {
    local Pawn P;
    local Vector PawnLocation;
    local Rotator PawnRotation;
    local class NewClass;

    P = Pawn;
    PawnLocation = Pawn.Location;
    PawnRotation = Pawn.Rotation;
    NewClass = class(DynamicLoadObject(ClassName, class'Class'));

    // Detach the current pawn from this controller and destroy it.
    UnPossess();
    P.Destroy();

    //spawn a new pawn and attach this controller
    P = Pawn(Spawn(NewClass, , ,PawnLocation,PawnRotation));

    //use false if you spawned a character and true for a vehicle
    Possess(P, false);
    }

    ReplyDelete
  9. I'm trying to get this hooked up when I Possess one of two pawns that will already be spawned inside the level. Basically, what I want is to be able to possess either of the spawned "hosts", destroy the default pawn ("ghost"), the Unpossess the host, thus destroying the host pawn and respawning the ghost. I have Possess and Dispossess functions working without this code, but I've been able to get it working fundamentally (though it's mapped to a separate input) to allow the Host body to be destroyed and respawn the Ghost pawn.

    I'd greatly appreciate any guidance you're able to give.

    ReplyDelete
  10. Thank you so much this helped a lot with my game. :3

    ReplyDelete

Loading...