21.11.2012, 15:53
Today i will show you how to use a variable.
//////////////////////////////////////
Firstly we will need to create one, We also want to make it a Global variable as we will be using it in two functions. So go into your gamemode and before your first function we want to add:
In this example of using a variable, i will be making the server tell us how many players are logged in after each new player connects.
So i will name my variable: playersonline
We will be using an integer variable, And set the value to 0. (We want the server to count how many players are online so we wouldn't set it as 3.)
So now we have created our variable, Lets make some use of it. Since we are checking how many players have connected, We will go to the function
Now inside the function we will want to add either:
#1
or we can use
#2
Personally i think #1 looks more presentable.
What this will do is add a value of +1 to the variable every time someone connects to the server.
Now we want to add this data to a string, So below that add:
it is not recommended to use a higher value then 128 as a cell value.
now we will want to format the string, So again below that add:
Okay so what this has done is basically added the data from the playersonline variable to a string of text (currently the text won't be sent to anyone we will add that in a minute)
We added %d so it will grab the data from whatever variable is after the comma, If we wanted to add two bits of data from two different variables we would do this
When you format a string, It will grab the data from the first variable called and will enter it at the first %d, Then the next at the next. I color coded it to show how it will call it in which order.
Now we want to notify every player how many players are online so we will add this below:
Or if you want it to only get sent to the player who just logged in you could use:
Now that is done, But what if a player leaves? The data will be inaccurate. So we will need to add this variable to our OnPlayerDisconnect function:
As you can see alls we changed was
playersonline++;
to
playersonline--;
But thats pretty self explanatory.
/////////////////////////////////
That is the end of my tutorial, Thank you for reading and i hope it helps.
//////////////////////////////////////
Firstly we will need to create one, We also want to make it a Global variable as we will be using it in two functions. So go into your gamemode and before your first function we want to add:
pawn Код:
new variablename;
So i will name my variable: playersonline
pawn Код:
new playersonline=0;
So now we have created our variable, Lets make some use of it. Since we are checking how many players have connected, We will go to the function
pawn Код:
public OnPlayerConnect
#1
pawn Код:
playersonline++;
#2
pawn Код:
playersonline +=1;
What this will do is add a value of +1 to the variable every time someone connects to the server.
Now we want to add this data to a string, So below that add:
pawn Код:
new string[128];
now we will want to format the string, So again below that add:
pawn Код:
format (string, sizeof(string), "There is now %d players logged in.", playersonline);
We added %d so it will grab the data from whatever variable is after the comma, If we wanted to add two bits of data from two different variables we would do this
Код:
format (string, sizeof(string), "There is now %d players logged in.%d", playersonline, examplevariable);
Now we want to notify every player how many players are online so we will add this below:
pawn Код:
SendMessageToAll(0xFF0000FF, string);
pawn Код:
SendClientMessage(playerid, 0xFF0000FF, string);
pawn Код:
playersonline--;
new string[128];
format(string, sizeof(string), "There is now %d players logged in", playersonline);
SendClientMessageToAll(0xFF0000FF, string);
playersonline++;
to
playersonline--;
But thats pretty self explanatory.
/////////////////////////////////
That is the end of my tutorial, Thank you for reading and i hope it helps.