[Tutorial] Creating a "Death Camera" spectating screen when a player dies.
#1

Hello and welcome to my tutorial.

I am here to explain and give a tutorial about "Death Camera's".

What I will be showing you today:
  • How to add a death camera to a player when they kill somebody.
  • How to set a timer for the death camera to set the player to spawn
Note: This will be for use in a Filterscript, you must edit it to work in your own system.

I will be making clear comments about what I am doing, so if you struggle you should be able to read them and then you should be good to continue. Although, if you have any problems, please don't hesitate to post on here!

Let's get started!

At the top of the script, make sure that you have got this included:
pawn Code:
#include <a_samp>
This here, will allow you the usage of the "Pawn" scripting for SA-MP. Without this, you will struggle to script anything.

Now that we have got that part done, we need to define some colours into the script!

We are going to be defining a colour, "White".

To do this, underneath where you have done the "#include" section, add the following piece of script:

pawn Code:
#define WHITE 0xFFFFFFAA
This is defining the colour, so that when we use it in a SendClientMessage; it will work.

Example:
pawn Code:
SendClientMessage(playerid, WHITE, "This will show the colour in WHITE.");
Second section:
(Player variables and forwards)

Let's start by explaining to you the function that involves "forward".

To create a timer, for example. What we are doing is when a player dies, the deathcam will spectate the killer, so the timer will start and 10 seconds later; we will respawn.

A forward, will define a "public" function.

So, to define our public function, we are going to need to add something along the lines of this:

pawn Code:
forward EndDeathCam(playerid);
The function is now called: EndDeathCam. The "(playerid)" section of this forward means that it will be used for a player who set the timer off.

Next, we need to add our player variables!

To do this, all we need to do is add "new WhatOurVariableIs[MAX_PLAYERS];", that would define it for the player's that are in the game.

So, to add ours we will need to add this to the top of the script:
pawn Code:
new Died[MAX_PLAYERS], KilledBy[MAX_PLAYERS];
If you try to compile when you have just added these; you are going to get 2 warnings. Not errors, but warnings because none of the "new's" have been used as of yet. Here is what the warning will look like:
Code:
warning 203: symbol is never used: "Died"
warning 203: symbol is never used: "KilledBy"
Don't worry about these warnings though, because they will all be removed by you in time, along more of the script.

Under "OnPlayerConnect(playerid)", you will want to notify the script to clear all player variables, so then the player doesn't have any items such as a fishing rod or bait. More is explained below.

pawn Code:
public OnPlayerConnect(playerid)
{
    //You will want to add the following:
    Died[playerid] = -1;
    KilledBy[playerid] = -1;
    return 1;
}
The reason for this is, to reset all player variables that we have just added.

Having: "Died[playerid] = 0;" will make it so the script can see that the player hasn't died
Having: "KilledBy[playerid] = 0;" will make it so the script can see that the player hasn't been killed yet.

Also, now if you tried to compile. You should have noticed that the warning codes have gone. This is because you have now used the variables in the script, so they aren't "Not being used".

The thing that I have show you above with OnPlayerConnect(playerid), you need to do the same for OnPlayerDisconnect(playerid, reason).

pawn Code:
public OnPlayerDisconnect(playerid, reason)
{
    //You will want to add the following:
    Died[playerid] = 0;
    KilledBy[playerid] = 0;
    return 1;
}
That will ensure the script that when a player disconnects, it will remove all of the variables that have been stored in the script, whilst the player has been connected.

Now that we have the defines, includes and player variables out of the way, we can crack on with the rest of the script!

Third section:
(Script functions and timers(with public function))

This section of the tutorial, will show you the basics of OnPlayerDeat, also how to create timers with a public function to display the outcome, also how to create the actual Death Cam.

pawn Code:
public OnPlayerDeath(playerid, killerid, reason)
{
    Died[playerid] = 1;
    if(Died[playerid] == 1)
    {
        Died[playerid] = 0;
        if(KilledBy[playerid] != INVALID_PLAYER_ID)
        TogglePlayerSpectating(playerid, 1);
        PlayerSpectatePlayer(playerid, killerid);
        SendClientMessage(playerid, WHITE, "You have been killed, you are now spectating your killer.");
        SetTimerEx("EndDeathCam", 10000, 1, "i", playerid);
    }
    else
    {
        TogglePlayerSpectating(playerid, 0);
    }
    return 1;
}
Now to explain:

if(Died[playerid] == 1) - If the player who has died's variable = 1, then the script will continue.

Died[playerid] = 0; - This sets the script's variable to "0", telling the script that he has already died and no need to call this function again.

if(KilledBy[playerid] != INVALID_PLAYER_ID) - This is checking that the player who killed him isn't an invalid player.

TogglePlayerSpectating(playerid, 1); - This sets the player to spectating.

PlayerSpectatePlayer(playerid, killerid); - This sets the player who got killed, to spectate the player who killed him/her.

SetTimerEx("EndDeathCam", 10000, 1, "i", playerid); - This sets the timer as to when the player should stop watching the camera and re-spawn.

Now, onto the public funtion.

This is called when the 10 seconds are up with the camera timer.

This is what it would look like:

pawn Code:
public EndDeathCam(playerid)
{
    TogglePlayerSpectating(playerid, 0);
    Died[playerid] = 0;
}
Time to explain:

TogglePlyerSpectating(playerid, 0); - This sets the player who was spectating the killer, to stop spectating anyone and to spawn.

Died[playerid] = 0; - This is re-assuring the script that the playerid has not died again.

That comes to an end of my tutorial, thank you for reading and if you have got any questions, please post them here!

I know that there is a lot of writing, but it all will help newbie's in scripting. I hope that people will learn something from this because it will make me feel better :3.

Otherwise, good luck with your scripting!

- iGetty out.
Reply
#2

I'm sure gonna use this thx.
Reply
#3

It would be cool if sa-mp was able to somehow constantly record gameplay, then be able to show the 'Killcam' like in all FPS games.
Reply
#4

Another great release buddy! Keep at 'em.
Reply
#5

Thanks everyone!

Anymore requests, then tell me and I'll do them!
Reply
#6

I like this!
normally im hard on the tutorial writers! today im in a hurry!

It is a good idea and i like that part of it!

as far as request go Id say
make a tutorial showing how to rotate the camera around a player while in class selection.
SHowing how to adjust the speed
cam height, distance all that.

just trying to throw out some ideas for ya.
keep it up~
Reply
#7

Thanks Jonny!

Also, I'll have a see about that tutorial!

Thanks once again!
Reply
#8

any screenshots?!
Reply
#9

Tutorials don't have screenshots mate.

It's simple, all that happens is when you get killed, you spectate the person who killed you.

Check it, others do.
Reply
#10

Quote:
Originally Posted by iGetty
View Post
Thanks everyone!

Anymore requests, then tell me and I'll do them!
This is my request dude,

Make it better.

replace this with textdraw.
Code:
 SendClientMessage(playerid, WHITE, "You have been killed, you are now spectating your killer.");
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)