[Tutorial] Creating A Simple Mini Game Base
#1

Hello everyone, welcome to my first tutorial on this account. Before i start i want to say just one thing, the methods i use may not be up to date, or they may not be the simplest way. I am teaching you, the SA-MP community, how to create a basic mini game base. This is more for learning purposes then to create a mini game, indeed we are making one, but i want you to learn from it and one day be able to create a more advanced mini game for your servers.
Introduction
In this tutorial i will be teaching you how to create a simple DM mini game base, i will be using the following techniques in this tutorial:
  • dcmd
  • player values
If you do not own the dcmd define i will provide it to you now.

pawn Код:
#define dcmd(%1,%2,%3) if (!strcmp((%3)[1], #%1, true, (%2)) && ((((%3)[(%2) + 1] == '\0') && (dcmd_%1(playerid, ""))) || (((%3)[(%2) + 1] == ' ') && (dcmd_%1(playerid, (%3)[(%2) + 2]))))) return 1
With that we can start learning how to make a simple mini game.

NOTE: This mini game is just a base, so the spawn position will be only one coordinate, you can change it up when you make the mini game.
Creating Player Values, Setting It Up
Before we can get started on making the mini game base, we will need to make some player values that will setup the player and allow them to play the mini game. First we will have to create a way to save/check if the player is in a mini game, we do this by creating a new variable and storing a special value in it.

pawn Код:
new IsInMini[MAX_PLAYERS];
Explanation:

This line creates a new variable called "IsInMini" and stores a special value, this value is MAX_PLAYERS. As indicated MAX_PLAYERS means the max players the server at max can hold regarding the SA Limits. You will understand the use of this a bit later in the tutorial, but for now just ignore it.

Now we want the server to get the players name right? We will achieve this by creating yet another variable, and we will place this just below the #include <samp>.

pawn Код:
new name[129]
Explanation:

This line is used to store the value of the players name, if you haven noticed yet, their is a weird block of numbers next to the word name, this is called an array. I will not get into this in this tutorial, in fact their is a tutorial on the forums explaining arrays, i will put a link at the end of the tutorial so you can veiw it. Anyways lets get back on topic, the reason we are using this is because we want it to store the players name we achieve this by creating an array, you probably noticed the 129 next to name, basically this means the array can carry 128 characters(Cells). You are probably wonder why 128 when it has 129, its 128 because, for every array you need to have one spare cell for a null or in other words, a " ", or 0. We will use this array in a function that lets us retrieve the players name easily.

pawn Код:
GetPlayerName(playerid,const string[],len);
Explanation:

This is the basic outline of the function, it will retrieve the players id, then it will store the name in the array we created earlier. Then we get the size of the array using, sizeof(). This is basically self explainable so i will not go into serious depth with it. Now we have all the basic variables needed to start our mini game.

Substituted with our information:

pawn Код:
GetPlayerName(playerid,name,sizeof(name));
we will place this in our command so that when a player uses the command to join it instantly retrieves the players name, and we can use it to display that the player connected.
Command Creation Unit
[INDENT]In this section we will be creating the command that allows the player to join the mini game, we will be using dcmd to create it, just so you can get used to

dcmd's structure. We will start off by creating the basic layout of the cmd

pawn Код:
dcmd_mini(playerid,params[])
{
     GetPlayerName(playerid,name,sizeof(name));
     new msg[129];
     IsInMini[playerid] = 1;
     format(msg,sizeof(msg),"%s has joined the mini game!",name);
     return 1;
}
Explanation:

Here we created a new array called msg and set the size to 129 like we did before, you can set this number to anything more than the length of the message plus one.

Then we formated the message using format

pawn Код:
format(output[], len, const string[], float{}....);
output = array that we created
len = the size of the array
cont string = the text we want to format, SPECIAL CHARACTERS: %s = string, %d = single number, %i = integer, %% = regular %.
float = what every you want the special characters to say, This can be virtually anything.

in our case we made the message display "(PLAYERSNAME) has joined the mini game". Players name will be substituted by the player that typed the command.

next we will have to make it display the message, we can achieve this by using

pawn Код:
SendClientMessageToAll(playerid,const string[]);
Explanation:

This basically will send a message to every player playing on the server.

so our code so far should look like this:

pawn Код:
dcmd_mini(playerid,params[])
{
     GetPlayerName(playerid,name,sizeof(name));
     new msg[129];
     IsInMini[playerid] = 1;
     format(msg,sizeof(msg),"%s has joined the mini game!",name);
     SendClientMessageToAll(playerid,msg);
     return 1;
}
We put msg where a string should be, because it is basically what we want the server to tell the players, and its value is the string.

Now we want to put the player in a different world so they dont get disturbed while playing, we also want them to spawn at the correct positions. We achieve this by

using 2 functions,

pawn Код:
SetPlayerPos(playerid,1958.3783, 1343.1572, 15.3746);
and..

pawn Код:
SetPlayerVirtualWorld(playerid,50);
Remeber, you do not need to use the same worldid as me, or the same coordinates. I am using these as an example, there are endless number possibilities you can

choose from. So be creative and use your own.


Our code should look like this now:
pawn Код:
dcmd_mini(playerid,params[])
{
     new msg[129];
     IsInMini[playerid] = 1;
     format(msg,sizeof(msg),"%s has joined the mini game!",name);
     SendClientMessageToAll(playerid,msg);
     SetPlayerPos(playerid,1958.3783, 1343.1572, 15.3746);
     SetPlayerVirtualWorld(playerid,50);
     return 1;
}
Guess what, we are now done with the creation of the command to join the mini game, next we will create the exit command. This is very simple, we will just set the

value of IsInMini to 0.

and return them back to world 0


pawn Код:
dcmd_exit(playerid,params[])
{
     IsInMini[playerid] = 0;
     SetPlayerVirtualWorld(playerid,0);
     return 1;
}
See how simple that was?

Note: you will have to put the dcmd function in your onplayercommandtext call back, what i mean is, you will have to put this into that call back:

pawn Код:
dcmd(mini,4,cmdtext);
dcmd(exit,4,cmdtext);
Explanation:

This will enable the function to be able to be treated like a command, it basically is setup like this

pawn Код:
dcmd(name of the command created, the size of the command, and cmdtext to tell SA its a command);

Now its time to create what happens when a player dies :P




Finishing Touches


Now its the final moment, the finishing touches, in this section we will be creating what happens when the player dies if their in the mini game.


We will do this by checking if they are in the mini game, and if they are it will spawn them back at the spawn location, and make the killer gain one dollar each kill, and it will display who has died, and who killed them. I will put this in a bulk since i am getting a little to close to the limit i believe, if there is one.


pawn Код:
public OnPlayerDeath(playerid, killerid, reason)
{
     new killer[129],killed[129];
     GetPlayerName(killerid,killer,sizeof(killer));
     GetPlayerName(playerid,killed,sizeof(killed));
     if(IsInMini[playerid] == 1)
     {
           new killmsg[129];
           format(killmsg,sizeof(killmsg),"%s has killed %s",killer,killed);
           SendClientMessageToAll(0xFFF000FF,killmsg);
           GivePlayerMoney(killerid,1);
     }
     return 1;
}

This just basically sends a message to everyone saying someone killed someone, and gives the killer 1 dollar, you can change the amount. If they are not in the minigame it does nothing.



Thats about it, hope this tutorial helped you learn how to create a simple mini game. Thank you for reading this, hope you have a great day/night/evening.


Tips
  • You can add more things to what the player earns just by adding another function of your choice to the death portion.
  • If you get an error like so: (252) : warning 203: symbol is never used: "params", just do #pragma unused params in bothe of the commands


NOTE: If you find any errors in the script post them here, i compiled it and it compiled with two errors, this is told how to be fixed in the tips section, also if you find anything wrong with the tutorial please tell me, something happened with it when i was almost done and i had to put it back together. So things might sound weird to you, if they do tell me.



Thanks again for reading this and i hope you enjoyed it
Reply
#2

Reserved just incase :P
Reply
#3

Nice tutorial. Useful for those people requesting it.
Reply
#4

Quote:
Originally Posted by Lorenc_
Посмотреть сообщение
Nice tutorial. Useful for those people requesting it.
Thank you, i worked hard on this tutorial following ******'s tutorial on how to make a tutorial. I followed as many rules he posted, as i possibly could.
Reply
#5

I have a few suggestions:
pawn Код:
new name[24];
There is no need to set the length of the name to 129, as SAMP only handles playernames of maximum 20 characters.

pawn Код:
dcmd_exit(playerid,params[])
{
     IsInMini[playerid] = 0;
     SetPlayerVirtualWorld(playerid,0);
     return 1;
}
In the exit-command, you forgot to put the player back in the normal world.
Reply
#6

Even better, use the MAX_PLAYER_NAME definition in case a newer version comes which supports more characters for the player name. That way you wouldn't be forced to change all the variables in your script that store the player's names.

Also, I suggest switching your command processor to ZCMD - A quicker and more efficient way of processing commands. It also provides more features compared to DCMD. You can also look into Y_Command, which offers a vast array of features.

Finally, I believe you messed up this piece of code here:
pawn Код:
GetPlayerName(killerid,killer,sizeof(killer));
GetPlayerName(playerid,killer,sizeof(killed));
You get and store the names of both players into the same variable. It should look like so:
pawn Код:
GetPlayerName(killerid,killer,sizeof(killer));
GetPlayerName(playerid,killed,sizeof(killed));
You could also add in a statement to check whether or not the killer is a valid player. Currently, if the player falls to their death, it will send an invalid killer name (most likely a space or nothing).

Please, do not take any of these suggestions as rude comments, they are simple things I'm trying to point out to you. Other than that, it looks like a good tutorial. Very well constructed and in-depth explanation, good job.
Reply
#7

nice tutorial.
Reply
#8

Quote:
Originally Posted by PowerPC603
Посмотреть сообщение
I have a few suggestions:
pawn Код:
new name[24];
There is no need to set the length of the name to 129, as SAMP only handles playernames of maximum 20 characters.

pawn Код:
dcmd_exit(playerid,params[])
{
     IsInMini[playerid] = 0;
     SetPlayerVirtualWorld(playerid,0);
     return 1;
}
In the exit-command, you forgot to put the player back in the normal world.
Quote:
Originally Posted by Grim_
Посмотреть сообщение
Even better, use the MAX_PLAYER_NAME definition in case a newer version comes which supports more characters for the player name. That way you wouldn't be forced to change all the variables in your script that store the player's names.

Also, I suggest switching your command processor to ZCMD - A quicker and more efficient way of processing commands. It also provides more features compared to DCMD. You can also look into Y_Command, which offers a vast array of features.

Finally, I believe you messed up this piece of code here:
pawn Код:
GetPlayerName(killerid,killer,sizeof(killer));
GetPlayerName(playerid,killer,sizeof(killed));
You get and store the names of both players into the same variable. It should look like so:
pawn Код:
GetPlayerName(killerid,killer,sizeof(killer));
GetPlayerName(playerid,killed,sizeof(killed));
You could also add in a statement to check whether or not the killer is a valid player. Currently, if the player falls to their death, it will send an invalid killer name (most likely a space or nothing).

Please, do not take any of these suggestions as rude comments, they are simple things I'm trying to point out to you. Other than that, it looks like a good tutorial. Very well constructed and in-depth explanation, good job.
Thank you for the help guys, and i take criticism well so you dont have to worry, the reason i used an array of 129 is because i wanted to teach a little bit about arrays, so this is a double lesson :P
Reply
#9

Nice tutorial and it is very usefull
Reply
#10

Excellent for begginers, well done and informative.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)