• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] How to save the position/skin of the player after exiting?
#7
(2021-04-04, 12:27 PM)CrypticSin Wrote: I know how to store user information, like the username, password, register date and all that but i was wondering if this has a similiar approach with saving skins and position of a player?



Im using phpmyadmin for mysql, and most people use Y_INI in their gamemodes. If it get's really complicated i might have to switch to Y_INI to store user data unless i find a way to do it on MySql.

I will show you an example of storing a player's skin.

I will use SQLite, but for MySQL, it's the same approach with different function names when you associate the result of the fields on a variable. Anyway, I have no experience with MySQL, so take these words carefully.

PHP Code:
#include <a_samp>

// Suppose USER_ID and USER_NICKNAME are already associated with the database, that is to say, you already have a login/register system, so I only show you how to store skin information when the player exits and how to set the player's skin when the player connects.

new DB:db_handle// Create a global variable to make a db connection and manipulate later on the code.

enum USER_DATA {
? ? 
USER_ID,
? ? 
USER_NICKNAME[MAX_PLAYER_NAME],
? ? 
USER_SKIN // We need to use this only for when the player connects.
};

new 
gUserData[MAX_PLAYERS][USER_DATA];

public 
OnGameModeInit() {
? ? 
db_handle db_open("main.db");

? ? if (
db_handle) {
? ? ? ? print(
"Successfully created a connection to database \"main.db\".");

? ? ? ? 
db_free_result(db_query(db_handle"CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTOINCREMENT, nickname TEXT, skin INTEGER)"));
? ? }

? ? else {
? ? ? ? print(
"Failed to open a connection to database \"main.db\".");
? ? }
? ? return 
1;
}

// To store the skin that the player has before exit, all you need to do is to get the player skin and use the UPDATE clause from SQL.
public OnPlayerDisconnect(playeridreason) {
? ? new 
query[256];

? ? 
format(querysizeof query"UPDATE users SET skin = '%i' WHERE nickname ='%q'"GetPlayerSkin(playerid), gUserData[playerid][USER_NICKNAME]);

? ? 
db_query(db_handlequery);

? ? 
// Reset player variables
? ? new 
? ? ? ? 
tmp[USER_DATA];

? ? 
gUserData[playerid] = tmp
? ? return 
1;
}

// Now you need to set the skin stored in the database to the player, and then make it spawn with that skin for example.
public OnPlayerConnect(playerid)
{
// Reset player variables
    
new 
        
tmp[USER_DATA];
gUserData[playerid] = tmp

? ? new 
query[256], DBResultresult// Create a query variable and DBResult to free the result of the query.

? ? // Suppose we have a function to check the player login succefully.
? ? if (login()) { 
? ? ? ? 
SendClientMessage(playerid, -1"Login succefully.");

? ? ? ? 
// We make the query, selecting all fields from the users table.
? ? ? ? format(querysizeof query"SELECT * FROM users WHERE nickname='%q' LIMIT 1"gUserData[playerid][USER_NICKNAME]);

? ? ? ? 
result db_query(db_handlequery);

? ? ? ? 
// If there any row, it says that the user registered has the fields that you declare before (id, nickname and skin).
? ? ? ? if (db_num_rows (result)) {
? ? ? ? ? ? 
// So we will proceed to associate the result of the field "skin" from the database to our variable gUserData[playerid][USER_SKIN].
? ? ? ? ? ? gUserData[playerid][USER_SKIN] = db_get_field_assoc_int(result"skin");
? ? ? ? }
? ? }

else {
? ? ? ? 
ShowPlayerDialog(playeridDIALOG_LOGINDIALOG_STYLE_PASSWORD"Login""La contrase?a que ingresaste es incorrecta.\nPor favor, intenta de nuevo!""Ingresar""Salir");
? ? }

? ? 
db_free_result(result); // Free the query result.
return 1;
}

public 
OnPlayerSpawn(playerid) {
? ? 
SetSpawnInfo(playerid0gUserData[playerid][USER_SKIN], 0000000000); // Here is where we use the value already passed to our variable from the database.
? ? SpawnPlayer(playerid);
? ? return 
1;


I hope this helps you a little bit!
  Reply


Messages In This Thread
RE: How to save the position/skin of the player after exiting? - by robertocaribbean - 2021-04-04, 02:13 PM

Forum Jump: