#include <a_samp>
new dimarray[][] =
{
{10, 69, 27},
{22, 33, 44},
{15, 16, 9}
};
main() {}
public OnGameModeInit()
{
PrintArray(dimarray, 0, sizeof (dimarray));
return 1;
}
stock PrintArray(array[][], index, size)
{
for (new i; i != size; ++i) printf("%i", array[i][index]);
return 1;
}
|
A little coding question...
Just wanted to ask which format type a player's IP is. I mean something like an integer. Cause I tried to make /banip cmd, which failed. I checked it and thought everything was correct, and defined the IP as a Float, which I think is atual mistake. |


new vehS = 0;
public OnGameModeInit()
{
...
vehS = AddStaticVehicle(429,-1538.4653,596.7906,6.8617,38.9806,13,13);
...
return 1;
}
public OnPlayerRequestClass(playerid,classid)
{
SetPlayerPos(playerid,-1538.8669,594.3477,7.1813);
SetPlayerFacingAngle(playerid,130.4796);
SetPlayerVirtualWorld(playerid, 0);
SetPlayerCameraPos(playerid, -1543.0037,590.2316,7.1875);
SetPlayerCameraLookAt(playerid, -1538.5853,596.4193,9.4897);
SetVehicleVirtualWorld(vehS, GetPlayerVirtualWorld(playerid));
LinkVehicleToInterior(vehS, GetPlayerInterior(playerid));
return 1;
}
|
strings are actually literal words made by humans, which are represented by numbers when the script reads it |
CMD:test1(playerid, params[])
{
new Float:X, Float:Y, Float:Z, Float:ang, obj, Float:Xx, Float:Yy, Float:Zz, Float:fX, Float:fY, str[128];
GetPlayerFacingAngle(playerid, ang);
GetPlayerPos(playerid, X, Y, Z);
GetXYInFrontOfPlayer(playerid, 45, fX, fY);
obj = CreateObject(345, fX, fY, Z, 0, 0, 0);
GetObjectPos(obj, Xx, Yy, Zz);
format(str, sizeof str, "X = %f, Y = %f, Z=%f Your pos : %f ,%f, %f", Xx, Yy, Zz, X, Y, Z);
SendClientMessage(playerid, -1, str);
return 1;
}
CMD:spawntest(playerid)
{
new Float: fX, Float: fY, Float: fZ, ob;
GetXYInFrontOfPlayer(playerid, 45.1, fX, fY);
MapAndreas_FindZ_For2DCoord(fX, fY, fZ);
ob = CreateObject(345, fX, fY, fZ, 0, 0, 0);
return 1;
}
GetXYInFrontOfPlayer(playerid, Float: distance, &Float: rX, &Float: rY)
{
new Float: x, Float: y, Float: z, Float: angle;
GetPlayerPos(playerid, x, y, z);
GetPlayerFacingAngle(playerid, angle);
rX = (x + (distance * floatcos(-angle, degrees)));
rY = (y + (distance * floatsin(-angle, degrees)));
}

CMD:hedit(playerid, params[])
{
new bizid, string[128], input;
if(!IsPlayerLoggedIn(playerid)) return SendClientMessage(playerid, COLOR_GREY, "You need to login first before using any command.");
if(PlayerInfo[playerid][pAdmin] < 4 && !PlayerInfo[playerid][pHouseMod]) return SendClientMessage(playerid, COLOR_GREY, "You are not an authorized to use this command.");
// if(!aDuty[playerid]) return SendClientMessage(playerid, COLOR_GREY, "You are not on admin duty.");
if(sscanf(params, "s[32]", params))
{
SendClientMessage(playerid, COLOR_WHITE, "USAGE: /hedit [option] [houseid]");
SendClientMessage(playerid, COLOR_GREY, "OPTIONS: location | price | level");
return 1;
}
if(!strcmp(params, "location", true, 8))
{
if(sscanf(params, "s[32]i", params, bizid)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /hedit location [house]");
new idx = bizid;
if(!HouseInfo[bizid][hLevel]) return SendClientMessage(playerid, COLOR_GREY, "Invalid house id.");
GetPlayerPos(playerid, HouseInfo[idx][hX], HouseInfo[idx][hY], HouseInfo[idx][hZ]);
DestroyDynamicPickup(HouseInfo[idx][hPickup]);
HouseInfo[idx][hPickup] = CreateDynamicPickup(1273, 1, HouseInfo[idx][hX], HouseInfo[idx][hY], HouseInfo[idx][hZ], 0);
DestroyDynamic3DTextLabel(HouseInfo[idx][hText]);
if(!strcmp("The State", HouseInfo[idx][hOwner])) format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" For Sale
"CWE"$%d", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], HouseInfo[idx][hPrice]);
else format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" %s", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], RHS(idx));
HouseInfo[idx][hText] = CreateDynamic3DTextLabel(string, COLOR_WHITE, HouseInfo[idx][hX], HouseInfo[idx][hY], HouseInfo[idx][hZ]+0.3, 15);
format(string, sizeof(string), "{FF0000}[Admin Warn]{FF6347} %s has changed house ID %d\'s location.", RPN(playerid), bizid);
SendAdminMessage(COLOR_DARKRED, 1, string);
Log("logs/house.log", string);
}
else if(!strcmp(params, "price", true, 5))
{
if(PlayerInfo[playerid][pAdmin] < 4 && !PlayerInfo[playerid][pHouseMod]) return SendClientMessage(playerid, COLOR_GREY, "You are not an authorized to use this command.");
if(sscanf(params, "s[32]ii", params, bizid, input)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /house price [house] [price]");
new idx = bizid;
if(!HouseInfo[idx][hLevel]) return SendClientMessage(playerid, COLOR_GREY, "Invalid house id.");
if(strcmp("The State", HouseInfo[bizid][hOwner])) return SendClientMessage(playerid, COLOR_GREY, "You can\'t edit the price of owned businesses.");
HouseInfo[bizid][hPrice] = input;
format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" For Sale
"CWE"$%d", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], HouseInfo[idx][hPrice]);
UpdateDynamic3DTextLabelText(HouseInfo[bizid][hText], COLOR_WHITE, string);
format(string, sizeof(string), "{FF0000}[Admin Warn]{FF6347} %s has set house ID %d\'s price to $%d.", RPN(playerid), bizid, input);
SendAdminMessage(COLOR_DARKRED, 1, string);
Log("logs/house.log", string);
}
else if(!strcmp(params, "level", true, 5))
{
if(PlayerInfo[playerid][pAdmin] < 4 && !PlayerInfo[playerid][pHouseMod]) return SendClientMessage(playerid, COLOR_GREY, "You are not an authorized to use this command.");
if(sscanf(params, "s[32]ii", params, bizid, input)) return SendClientMessage(playerid, COLOR_WHITE, "USAGE: /house level [houseid] [level]");
new idx = bizid;
//if(!HouseInfo[bizid][hLevel]) return SendClientMessage(playerid, COLOR_GREY, "Invalid house id.");
if(input < 1 || input > 11) return SendClientMessage(playerid, COLOR_GREY, "Levels are between 1 and 11.");
HouseInfo[bizid][hLevel] = input;
if(!strcmp("The State", HouseInfo[idx][hOwner])) format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" For Sale
"CWE"$%d", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], HouseInfo[idx][hPrice]);
else format(string, sizeof(string), ""COE"["CWE"%d, House (Lvl: %d)"COE"]
"COE"Owner"CWE" %s
"COE"Status"CWE" %s", idx, HouseInfo[idx][hLevel], HouseInfo[idx][hOwner], RHS(idx));
UpdateDynamic3DTextLabelText(HouseInfo[bizid][hText], COLOR_WHITE, string);
format(string, sizeof(string), "{FF0000}[Admin Warn]{FF6347} %s has set house ID %d\'s level to %d.", RPN(playerid), bizid, input);
SendAdminMessage(COLOR_DARKRED, 1, string);
Log("logs/house.log", string);
}
return 1;
}
stock vehicles_Load_1()
{
new query[128];
mysql_query(MySQL, "SELECT `id` FROM `Vehicules` ORDER BY DESC LIMIT 1");
new id = cache_get_row_int(0, 1);
for(new i = 1; i < id; i++)
{
format(query, sizeof(query), "SELECT model, x, y, z, angle, col1, col2, respawn FROM Vehicles WHERE id = %d LIMIT 1", i);
mysql_tquery(MySQL, query, "OnQueryVehicleDone", "i", id);
}
return 1;
}
forward OnQueryVehicleDone(id);
public OnQueryVehicleDone(id)
{
model = cache_get_row_int(0, 1);
x = cache_get_row_float(0, 2);
y = cache_get_row_float(0, 3);
z = cache_get_row_float(0, 4);
angle = cache_get_row_float(0, 5);
col1 = cache_get_row_int(0, 6);
col2 = cache_get_row_int(0, 7);
respawn = cache_get_row_int(0, 8);
if(respawn == '\0') respawn = -1;
CreateVehicle(model, x, y, z, angle, col1, col2, respawn);
return 1;
}
stock vehicles_Load_2()
{
mysql_tquery(MySQL, "SELECT * FROM Vehicules", "LoadVehicles", "");
return 1;
}
forward LoadVehicles();
public LoadVehicles()
{
new nbrVeh = cache_get_row_count();
for(new v = 1; v < nbrVeh;v++)
{
model = cache_get_row_int(0, 1);
x = cache_get_row_float(0, 2);
y = cache_get_row_float(0, 3);
z = cache_get_row_float(0, 4);
angle = cache_get_row_float(0, 5);
col1 = cache_get_row_int(0, 6);
col2 = cache_get_row_int(0, 7);
respawn = cache_get_row_int(0, 8);
if(respawn == '\0') respawn = -1;
CreateVehicle(model, x, y, z, angle, col1, col2, respawn);
}
return 1;
}
stock vehicles_Load_3()
{
mysql_tquery(MySQL, "SELECT * FROM Vehicules", "Loading_Third");
return 1;
}
forward Loading_Third();
public Loading_Third()
{
new rows, fields, data[50];
cache_get_data(rows, fields, MySQL);
for(new i = 0; i < rows; i++)
{
// Variables
cache_get_field_content(i, "model", data); model = strval(data);
cache_get_field_content(i, "x", data); x = floatstr(data);
cache_get_field_content(i, "y", data); y = floatstr(data);
cache_get_field_content(i, "z", data); z = floatstr(data);
cache_get_field_content(i, "angle", data); angle = strval(data);
cache_get_field_content(i, "col1", data); col1 = strval(data);
cache_get_field_content(i, "col2", data); col2 = strval(data);
cache_get_field_content(i, "respawn", data); respawn = strval(data);
if(respawn == '\0') respawn = -1;
// On ajoute le vйhicule
AddStaticVehicleEx(model, x, y, z, angle, col1, col2, respawn);
}
}
|
The first one is out of the question. Running multiple queries is never a good idea when you can just run one query to get the same data.
Now, from what I can see the only real difference between the last two is the "cache_get_field_" functions. I would imagine it's faster to just address each row by ID (one cell) rather than a string (a bunch of cells) but I'm not sure how MySQL works internally for that. The second looks way easier to write though and you don't need to mess around with a cache variable to store the value in then assign that to the target (also I noticed that requires using strval/floatstr). The second option looks like the best method. |
SELECT model, x, y, z, angle, col1, col2, respawn FROM Vehicules
|
Originally Posted by http://code.tutsplus.com/tutorials/top-20-mysql-best-practices--net-7855
Avoid SELECT *
The more data is read from the tables, the slower the query will become. It increases the time it takes for the disk operations. Also when the database server is separate from the web server, you will have longer network delays due to the data having to be transferred between the servers. It is a good habit to always specify which columns you need when you are doing your SELECT's. |
stock funcname(playerid, ...)
{
new args = numargs(), index, str[1024];
for(new i = 1; i < args; i++)
{
index = 0, strdel(str, 0, sizeof(str));
do str[index] = getarg(i, index);
while (str[index++] != 0);
}
}
new str[][] = {"hello", "world"};
funcname(playerid, str);
stock funcname(playerid, array[][], size = sizeof array) {}