• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Pawn] How to save the position/skin of the player after exiting?
#1
Hi, im currently scripting a server and i want to know how a players progress is saved on the server??



So for example, when the user selects their skin i want the player to keep their skin until they decide to change it manually,?instead of the skin selection appearing everytime someone logs in.



Also how do you save the position of a player when they exit the game? and when they log back?back in it should log the user back to the same posiiton they exited?



What are the areas in the SAMP-wiki should i look into to make something like this?
  Reply
#2
Databases;

Learn SQL syntax than learn MySQL!
Using Pawn.CMD?

If you're doing so, this is the very first sign that you absolutely shouldn't utilize your all powerful P-Code knowledge in any of the scripting discussion topics.
  Reply
#3
I recommend you to start by doing something simple like store money, score.. then when you come close to understand how to store basic things, you can move on into more advanced topics.

A great tutorial for starters: https://sampforumarchive.com/forum.sa-mp...l?t=449536

Also, I recommend you to see other people his code if it's possible and try to understand the process that the programmer follows in his code.

In the bottom of this page https://open.mp/docs/awesome there are many gamemodes that you can view in github.
  Reply
#4
Honestly, if you have lack of experience with SQL you can try using Dini for it, although it's not the best.



Quote:So for example, when the user selects their skin i want the player to keep their skin until they decide to change it manually, instead of the skin selection appearing everytime someone logs in.



If you select a skin, it will be the same after each respawn, if I correctly know.?

You can make a /skin command, store the skin id in a global array for MAX_PLAYERS, then call it with SetPlayerSkin at OnPlayerSpawn.
  Reply
#5
Honestly, if you have lack of experience with SQL you can try using Dini for it, although it's not the best.



Quote:So for example, when the user selects their skin i want the player to keep their skin until they decide to change it manually, instead of the skin selection appearing everytime someone logs in.



If you select a skin, it will be the same after each respawn, if I correctly know.?

You can make a /skin command, store the skin id in a global array for MAX_PLAYERS, then call it with SetPlayerSkin at OnPlayerSpawn.
  Reply
#6
(2021-04-04, 04:17 AM)robertocaribbean Wrote: I recommend you to start by doing something simple like store money, score.. then when you come close to understand how to store basic things, you can move on into more advanced topics.



A great tutorial for starters: https://sampforumarchive.com/forum.sa-mp...l?t=449536



Also, I recommend you to see other people his code if it's possible and try to understand the process that the programmer follows in his code.



In the bottom of this page https://open.mp/docs/awesome there are many gamemodes that you can view in github.



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.
  Reply
#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
#8
(2021-04-04, 02:13 PM)robertocaribbean Wrote:
(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)

{

? ? 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!



Hi thanks for your help!?

What if i wanted to store more user data like player kills, gamescore, deaths etc.. Will i have to follow the same pattern as you showed for the skins or is there a shorter way of saving these information?
  Reply
#9
(2021-04-04, 03:39 PM)CrypticSin Wrote: Hi thanks for your help!?

What if i wanted to store more user data like player kills, gamescore, deaths etc.. Will i have to follow the same pattern as you showed for the skins or is there a shorter way of saving these information?

For the game score, it's simple because there are built-in functions that keep track of the score of a player, like "GetPlayerScore", so you need to get the player's score before he exits, use UPDATE, and then when he connects use "SetPlayerScore" in a similar way as we have to do it with their skin.

For other things like player kills and deaths, I guess you have to make a custom function that keep track of that values and then do the same process that you need to do for the score or skin.
  Reply
#10
EDIT: Double post by error, sorry.
  Reply
#11
Hello, How can i achieve this with sqlite?
  Reply


Forum Jump: