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?
Location: Belgrade, Serbia
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.
2021-04-04, 04:17 AM
(This post was last modified: 2021-04-04, 04:19 AM by robertocaribbean.)
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.
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.
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.
(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.
2021-04-04, 02:13 PM
(This post was last modified: 2021-04-04, 04:04 PM by robertocaribbean.)
(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(playerid, reason) { ? ? new query[256];
? ? format(query, sizeof query, "UPDATE users SET skin = '%i' WHERE nickname ='%q'", GetPlayerSkin(playerid), gUserData[playerid][USER_NICKNAME]);
? ? db_query(db_handle, query);
? ? // 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], DBResult: result; // 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(query, sizeof query, "SELECT * FROM users WHERE nickname='%q' LIMIT 1", gUserData[playerid][USER_NICKNAME]);
? ? ? ? result = db_query(db_handle, query);
? ? ? ? // 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(playerid, DIALOG_LOGIN, DIALOG_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(playerid, 0, gUserData[playerid][USER_SKIN], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // 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!
(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(playerid, reason) {
? ? new query[256];
? ? format(query, sizeof query, "UPDATE users SET skin = '%i' WHERE nickname ='%q'", GetPlayerSkin(playerid), gUserData[playerid][USER_NICKNAME]);
? ? db_query(db_handle, query);
? ? // 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], DBResult: result; // 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(query, sizeof query, "SELECT * FROM users WHERE nickname='%q' LIMIT 1", gUserData[playerid][USER_NICKNAME]);
? ? ? ? result = db_query(db_handle, query);
? ? ? ? // 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(playerid, DIALOG_LOGIN, DIALOG_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(playerid, 0, gUserData[playerid][USER_SKIN], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); // 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?
2021-04-04, 04:01 PM
(This post was last modified: 2021-04-04, 07:10 PM by robertocaribbean.)
(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.
2021-04-04, 04:02 PM
(This post was last modified: 2021-04-04, 04:03 PM by robertocaribbean.)
EDIT: Double post by error, sorry.
Hello, How can i achieve this with sqlite?
|