23.12.2011, 06:56
(
Последний раз редактировалось Jack_Leslie; 10.04.2012 в 09:11.
)
A tutorial by Jack Leslie
An example of HTTP: Update log with TextDraws
In this tutorial I will be trying to teach you how to use HTTP in pawn to GET information from a file, and post it in-game, I will be using TextDraws to display the information as it's easier to make new lines with it. This took me about 1 minute to code, it's small but it gives you an example of using the HTTP function in pawn. You can do a lot of other things with this.
Step 1:
We need to make a new text so we can set the string of it, display it and hide it. I've called the text "WhatsNewText", how ever we only want to display it to the player who types a command, so we will need to assign a playerid to it further on.
Start off with:
pawn Код:
new Text:WhatsNewText[MAX_PLAYERS];
We want the textdraw to auto hide after the player types in the command to look at the update log. For this, we will need to set a timer after he/she enters the command. So we need to make a public that hides the textdraw. So first, we need a forward for the public callback.
pawn Код:
forward HideTextDraw2(playerid);
pawn Код:
public HideTextDraw2(playerid) {
TextDrawHideForPlayer(playerid, WhatsNewText[playerid]);
return 1;
}
Now comes the HTTP part. If your new to this, I suggest reading the sa-mp wiki (https://sampwiki.blast.hk/wiki/HTTP), as I'm not an expert so I cannot explain it the best, but I'll try my hardest. When you call the HTTP code, you need to make a callback with the response, so we can store the information, or do whatever we wanna do with it. So, we need a forward.
pawn Код:
forward UpdateResponse(playerid, response_code, data[]);
pawn Код:
public UpdateResponse(playerid, response_code, data[])
{
new
string[ 1028 ];
if(response_code == 200)
{
format(string, sizeof(string), "%s", data);
TextDrawSetString(WhatsNewText[playerid], string);
TextDrawShowForPlayer(playerid, WhatsNewText[playerid]);
SetTimerEx("HideTextDraw2", 15000, false, "i", playerid);
}
else
{
format(string, sizeof(string), "The request failed! The response code was: %d", response_code);
SendClientMessage(playerid, 0xFFFFFFFF, string);
}
}
Step 4:
Now we have the callbacks that we need, we need to create the textdraw so it will display. I said before, that we only want the player who types the command to see the textdraw. So, instead of putting the textdrawcreate code under OnGameModeInit, place it under OnPlayerConnect:
pawn Код:
public OnPlayerConnect(playerid) {
WhatsNewText[playerid] = TextDrawCreate(36, 198, " ");
TextDrawFont(WhatsNewText[playerid], 1);
TextDrawLetterSize(WhatsNewText[playerid], 0.2, 1.4);
TextDrawColor(WhatsNewText[playerid], 0xFFFFFFFF);
TextDrawSetOutline(WhatsNewText[playerid], 0);
TextDrawSetProportional(WhatsNewText[playerid], 1);
TextDrawSetShadow(WhatsNewText[playerid], 0);
TextDrawHideForPlayer(playerid, WhatsNewText[playerid]);
return 1;
}
We need to destroy the textdraw when the player disconnects so we can safely re-create it when a player with the same playerid connects.
pawn Код:
public OnPlayerDisconnect(playerid, reason) {
TextDrawDestroy(WhatsNewText[playerid]);
return 1;
}
The command is very simple, it only involves 1 line, which is the HTTP code. I'll give you the code, then try and explain it.
pawn Код:
public OnPlayerCommandText(playerid, cmdtext[]) {
if(strcmp(cmdtext, "/new", true) == 0)
{
HTTP(playerid, HTTP_GET, "//LINK TO .TXT FILE HERE", "", "UpdateResponse");
return 1;
}
return 0;
}
playerid is the playersid (it is also used to differentiate requests that are sent to the same callback, normally known as "index", but to not confuse 'noobs', i changed it to playerid)
HTTP_GET is the type, "GET" sends a regular HTTP request. If we wanted to write something to a file on a webserver, we would use HTTP_POST
"devine-gaming.com/update_test.txt" is the location of the file (without http://) which I'll explain at the end
"" is the data (which we would use if we done HTTP_POST, we would write the required data to write there)
"UpdateResponse" is the public we want to be called after the HTTP request is sent.
Extra Steps:
Make sure to use the HTTP include, put this at the top of your script under "#include <a_samp>":
pawn Код:
#include <a_http>
pawn Код:
HTTP(playerid, HTTP_GET, "new url here", "", "UpdateResponse");
Did you know, since we are using TextDraws, we can use such characters as ~r~, ~w~ and ~n~ in the text file, for an example, here is what my text file looks like:
Quote:
~r~List of updates ~w~(23/12/2011 6:27pm):~n~/new has been implemented~n~we have also tested shit bro |
End of tutorial!
Well that's the end. I know I'm not good at explaining things, but the reason for making this tutorial was because I was just messing around with HTTP and figured out how to do this, it's useful for servers who update regularly, you don't have to script in the actual gamemode, what's new, if you have /new (/whatsnew etc.) command, you can just write to a text file and save it, and best of all, you can change the file and there's no need to GMX the server to update the information in-game.
Like I said, there's a lot you can do with HTTP, and this type of code. You could have a page in your ACP (if you have one) where you can edit the text file with a WYSIWYG editor or something similar.
Everytime I do a tutorial, I script as I write, to make sure it works, so I don't get no "OMG IT NOT WORKING WTF", because if it's not, it's your fault for not reading (or my fault for not explaining?), so here's the pastebin:
http://pastebin.com/sCxQF2J8