Game Development Community

Change Player Model in Gameplay

by Andrew Van Orden · in Technical Issues · 02/02/2008 (8:48 pm) · 5 replies

Is there a way to change the player model while in gameplay with either the press of a button or with a trigger? Basically, I want to start off as one model and then when I do a certain action (button or trigger) I want to change the shapeFile from what it is originally to a completely different model. Is this possible? Thanks.

#1
02/02/2008 (9:38 pm)
You just have to change the control of the player (like in game.cs when spawning):

// Give the client control of the player
%this.player = %player;
%this.setControlObject(%player);


But if you want to change the shapeFile, and not muck around with the rest I think you'll have to do some code changes/
#2
02/03/2008 (3:40 am)
You could look at assigning the player a new datablock. Assuming you have two (or more) datablocks setup for your player, with the 2nd one having a different shape file (and perhaps other tweaked parameters) then you'd just do

%this.player.setDataBlock( SomeOtherDataBlock );
#3
02/03/2008 (1:28 pm)
I've seen in other places that some people recommend using a second data block with the different parameters so I think that is the route I'm going to go. However, I'm not quite sure how to link a button or trigger with the statement

%this.player.setDataBlock( SomeOtherDataBlock );

I'm thinking maybe it would be a function in the default.bind.cs (as far as buttons go) but I am unsure. Can you suggest how I may do this? Thank you for your help.
#4
02/04/2008 (7:00 pm)
Success! I was able to change out the player model successfully with the press of a button. If anyone here is interested in how I did it, then please read ahead.

I did end up using a second player datablock. I just did a straight copy underneath the PlayerData(PlayerBody) code with the only difference being:

datablock PlayerData(PlayerBody2)
{
existing code
shapeFile = "NewShapeFile";
existing code
}

Next thing that I needed was a global variable to use my character model. That was done by going into game.cs and adding a line of code:

function GameConnection::createPlayer(%this, %spawnPoint)
{
existing code
$myGlobal = %player;
}

I also had to add this line towards the top for my functions:

exec("./shapeshift.cs");

Next I setup my code to do the actual switching of the character models. This was in my shapeshift.cs file and had this code:

function changeShape()
{
if ($shapeForm == 0)
{
$myGlobal.setDataBlock(PlayerBody);
$shapeForm = 1;
}
else
{
$myGlobal.setDataBlock(PlayerBody2);
$shapeForm = 0;
}
}

In this way, it would be operated with the same keystroke. Finally, I had to bind it to a key, so in config.cs I added:

moveMap.bind(keyboard, "q", changeShape);

With all of that, you should be able to change your character model back and forth with no problem.
#5
02/04/2008 (7:17 pm)
Success! I was able to change out the player model successfully with the press of a button. If anyone here is interested in how I did it, then please read ahead.

I did end up using a second player datablock. I just did a straight copy underneath the PlayerData(PlayerBody) code with the only difference being:

datablock PlayerData(PlayerBody2)
{
existing code
shapeFile = "NewShapeFile";
existing code
}

Next thing that I needed was a global variable to use my character model. That was done by going into game.cs and adding a line of code:

function GameConnection::createPlayer(%this, %spawnPoint)
{
existing code
$myGlobal = %player;
}

I also had to add this line towards the top for my functions:

exec("./shapeshift.cs");

Next I setup my code to do the actual switching of the character models. This was in my shapeshift.cs file and had this code:

function changeShape()
{
if ($shapeForm == 0)
{
$myGlobal.setDataBlock(PlayerBody);
$shapeForm = 1;
}
else
{
$myGlobal.setDataBlock(PlayerBody2);
$shapeForm = 0;
}
}

In this way, it would be operated with the same keystroke. Finally, I had to bind it to a key, so in config.cs I added:

moveMap.bind(keyboard, "q", changeShape);

With all of that, you should be able to change your character model back and forth with no problem.