[Tutorial] Making a simple race
#1

Hello dear users, I browsed the forums a little bit and found out that there is only one race making tutorial,the problem is, it was in Portuguese D:

Well anyway, lets begin!

Firstly,I'm giving a code,and only then deeply explain what I made.

pawn Код:
#include <a_samp>
Probably the most important include you'll need,you cannot do 99% of the actions in PAWN(sa-mp's case) without it .

pawn Код:
#define winner_money 10000
This is the money that the winning player will be given when he reaches the last checkpoint.

pawn Код:
new CheckPoints[10][3] =
{
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z}
};
Well,this is the checkpoints variable.
It's the order of the checkpoints placement.
10 is the amount of checkpoints that would be in your race,3 is the amount of information each checkpoint includes, the 3 information pieces are of course,the X,the Y,and the Z.

pawn Код:
new Player_CheckPoint[MAX_PLAYERS];

new bool:IsRaceRunning = false;

new WinnerName[MAX_PLAYER_NAME];

new stringwin[64];
Player_CheckPoint will be the variable that defines how much checkpoints the player has picked up. (We will need that). Well,why MAX_PLAYERS? Because we need an individual variable for each player because every player is in a different checkpoint.

bool:IsRaceRunning is a Boolean( a variable that usually used on true/false statements) to check if the race is running. It will be used for the race cancellation when the first player picks up the final checkpoint.

WinnerName is just a variable to hold the winners' name.

stringwin will be the string that we'll use to format a message to all of the other competitors announcing them that the race has ended.

Moving on!

pawn Код:
public OnGameModeInit()
{
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            Player_CheckPoint[i] = 0;
            SetPlayerRaceCheckpoint(i,0,CheckPoints[0][0],CheckPoints[0][1],CheckPoints[0][2],CheckPoints[1][0],CheckPoints[1][1],CheckPoints[1][2],9.7);
            IsRaceRunning = true;
        }
    }
    return 1;
}
Now, we're using the OnGameModeInit() callback,which is called when the gamemode is loaded on 'samp-server.exe'.
We're looping through all the connected players,setting their current checkpoint to the first one,and setting them a race checkpoint with the race checkpoints' information, now we'll go trough every parameter in the SetPlayerRaceCheckpoint.
'i' - Making sure the 'playerid' parameter equals to the looped players.
'0' - The race checkpoint type. '0' is the normal type which includes a nice arrow directing you to the next checkpoint.
CheckPoints[0][0] - This means,we're setting to the players the FIRST checkpoint in the CheckPoints variable,the count starts from zero. The first [0] is the ID of the checkpoint,it's 0 because it's the first checkpoint. The second [0] is the first information piece of the first checkpoint ID. In our case,the X.

CheckPoints[0][1] - Again as the previous checkpoint,just this time it's the Y we're setting.

CheckPoints[0][2] - And again,this time, it's the Z of the first checkpoint.

CheckPoints[1][0] - As I explained before,this parameter is the nextx parameter,which is actually the X position of the following checkpoint,it's only used for the arrow pointing position.

CheckPoints[1][1] - Again,the nexty parameter.

CheckPoints[1][2] - And again,the nextz parameter.

'9.7' - The size of the race checkpoint.

pawn Код:
public OnPlayerEnterRaceCheckpoint(playerid)
A public that is called every time a player enters a race checkpoint.

pawn Код:
if(IsRaceRunning == true) { Player_CheckPoint[playerid]++; }
We're checking if the race is still running,then enlarging the player who entered the checkpoints' variable to +1.

pawn Код:
if(Player_CheckPoint[playerid] < 9 && IsRaceRunning == true)
    {
        DisablePlayerRaceCheckpoint(playerid);
        SetPlayerRaceCheckpoint(playerid,0,CheckPoints[Player_CheckPoint[playerid]][0],CheckPoints[Player_CheckPoint[playerid]][1],CheckPoints[Player_CheckPoint[playerid]][2],CheckPoints[Player_CheckPoint[playerid]+1][0],CheckPoints[Player_CheckPoint[playerid]+1][1],CheckPoints[Player_CheckPoint[playerid]+1][2],9.7);
        return 1;
    }
Okay,this part is a bit tricky, we're checking if the player is in any checkpoint that is previous to the tenth one,and then checking if the race is still running.
Sounds tricky? Well, remember that in here, 9, is actually the 10th checkpoint,since the count starts from 0.
And we're checking if he is not in the 10th checkpoint because if he is in the 10th checkpoint,he will be in one checkpoint before the last one,which means we'll need to set a different type of race checkpoint,which is 1.

CheckPoints[Player_CheckPoint[playerid]][0 - 2] Again,setting his checkpoint to the next one. Why do we have [Player_CheckPoint[playerid]] as the checkpoint ID? Because we've increased the variable previously,remember?

CheckPoints[Player_CheckPoint[playerid]+1][0 -2] Okay,this one is simpler than it looks.
We're adding '+1' to set the following checkpoint AFTER the next checkpoint,again,for arrow pointing directions.
pawn Код:
if(Player_CheckPoint[playerid] == 9 && IsRaceRunning == true)
    {
        DisablePlayerRaceCheckpoint(playerid);
        SetPlayerRaceCheckpoint(playerid,0,CheckPoints[Player_CheckPoint[playerid]][0],CheckPoints[Player_CheckPoint[playerid]][1],CheckPoints[Player_CheckPoint[playerid]][2],CheckPoints[Player_CheckPoint[playerid]+1][0],CheckPoints[Player_CheckPoint[playerid]+1][1],CheckPoints[Player_CheckPoint[playerid]+1][2],9.7);
        return 1;
    }
Again,now,checking if the players' checkpoint counter counted 10 picked up checkpoints,and if the race is still running,and then setting his checkpoint to the last one,just with type 1,which is a race checkpoint with a finish line drawing in it.
pawn Код:
if(Player_CheckPoint[playerid] == 10 && IsRaceRunning == true)
    {
        for(new i = 0; i < MAX_PLAYERS; i++)
        {
            DisablePlayerRaceCheckpoint(i);
        }
        GetPlayerName(playerid,WinnerName,sizeof(WinnerName));
        format(stringwin,sizeof(stringwin),"[RACE] The race is over, %s has won and achieved $%d!",WinnerName,winner_money);
        SendClientMessageToAll(0xFF0000FF,stringwin);
        GivePlayerMoney(playerid,winner_money);
        IsRaceRunning = false;
        return 1;
    }
Now,checking if the player is has picked up the last checkpoint.

Then loops trough all players,and disabling their checkpoints.
Then we're getting the players' name using the WinnerName variable from the start,remember?
Then,formatting to the winstring we've also declared at the start,the message with the winners' name,and the amount of money he won.

Then,sending everyone the formatted string,and giving the winner the money we've defined in the begging,and canceling the race.

Ending up,your script should look something like this:
http://pastebin.com/rfVyvfDb

If something was misunderstood, just ask, if they are bugs, I'll fix them right away.
Reply
#2

new winnermoney = rand(15000)+5000;

GivePlayerMoney(playerid, winnermoney);

the +5000 is for the fastness

nice tutorial too

This forum requires that you wait 120 seconds between posts. Please try again in 6 seconds.
Reply
#3

Nice tut
Reply
#4

Thanks, it's my first tutorial
Reply
#5

This is actually nice, I used to do it alot different, but this is ALOT easier of a way. I used to make a Checkpoint variable, and then do Checkpoint[playerid]++ when they reached the checkpoint, and add all the effects into OnPlayerEnterCheckpoint, but this makes it so it's alot shorter, and simpler of a code.
Nice job!
Reply
#6

Quote:
Originally Posted by Sky4D
Посмотреть сообщение
This is actually nice, I used to do it alot different, but this is ALOT easier of a way. I used to make a Checkpoint variable, and then do Checkpoint[playerid]++ when they reached the checkpoint, and add all the effects into OnPlayerEnterCheckpoint, but this makes it so it's alot shorter, and simpler of a code.
Nice job!
Thanks,that's actually why I wrote this tutorial
Reply
#7

Nice tut
Reply
#8

Lookin' good, checking now to see if you could do it better :P (Yarp, I'm an asshole just like C.....)
Reply
#9

Quote:
Originally Posted by Hiddos
Посмотреть сообщение
Lookin' good, checking now to see if you could do it better :P (Yarp, I'm an asshole just like C.....)
Good luck with that XD
Reply
#10

Quote:
Originally Posted by [XST]O_x
Посмотреть сообщение
Good luck with that XD
Here's sumthin:

pawn Код:
new CheckPoints[10][3] =
{
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z},
    {x,y,z}
};
They're supposed to be float values :P
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)