[MySQL] Problem trying to load weapons
#1

Hey guys, I was following this tutorial: https://sampforum.blast.hk/showthread.php?tid=505081
And now I managed to get player weapons saved into a db but I'm having some troubles with the code (Mostly because I can't find any updated MySQL R41-2 functions/commands -I'm not sure how it's called-).

Errors:

Код:
warning 213: tag mismatch
warning 213: tag mismatch
warning 213: tag mismatch
Code:

Код:
forward OnLoadPlayerWeapons(playerid);
public OnLoadPlayerWeapons(playerid)
{
	new
	    weaponid,
	    ammo;

	for(new i, j = cache_get_row_count(g_SQL); i < j; i++) // loop through all the rows that were found
	{
	    weaponid 	= cache_get_value_int(i, 0, g_SQL);
	    ammo    	= cache_get_value_int(i, 1, g_SQL);

		if(!(0 <= weaponid <= 46)) // check if weapon is valid (should be)
		{
			printf("[info] Warning: OnLoadPlayerWeapons - Unknown weaponid '%d'. Skipping.", weaponid);
			continue;
		}

		GivePlayerWeapon(playerid, weaponid, ammo);
	}
	return;
}
The lines with errors are the bold ones.


And by the way, I know I should add a SELECT query there but I'm not sure where specifically.
Reply
#2

Second parameter for cache_get_value_int function is field name, and third parameter is for reference variable.
Example:
Код:
cache_get_value_int(i, "field_name", weaponid);
cache_get_value_int(i, "field_name", ammo);
If you want to get value by its index, use cache_get_value_index_int instead.
Example:
Код:
cache_get_value_index_int(i, 0, weaponid);
cache_get_value_index_int(i, 1, ammo);
Reply
#3

Quote:
Originally Posted by X337
Посмотреть сообщение
Second parameter for cache_get_value_int function is field name. If you want to get value by its index, use cache_get_value_index_int instead.
Replaced cache_get_value_int function with cache_get_value_index_int and I'm still getting tag mismatch error
Reply
#4

Quote:
Originally Posted by X337
Посмотреть сообщение
Second parameter for cache_get_value_int function is field name, and third parameter is for reference variable.
Example:
Код:
cache_get_value_int(i, "field_name", weaponid);
cache_get_value_int(i, "field_name", ammo);
If you want to get value by its index, use cache_get_value_index_int instead.
Example:
Код:
cache_get_value_index_int(i, 0, weaponid);
cache_get_value_index_int(i, 1, ammo);
Tried with that last code, now I'm not getting any error except the tag mismatch from this line

Код:
for(new i, j = cache_get_row_count(g_SQL); i < j; i++)
Reply
#5

https://sampwiki.blast.hk/wiki/MySQL/R40..._get_row_count

The function does not return the number of rows but it stores it to a variable (passed-by-reference) in version R40+.
You can also use:
Код:
for(new i, j = cache_num_rows(); i < j; i++)
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
https://sampwiki.blast.hk/wiki/MySQL/R40..._get_row_count

The function does not return the number of rows but it stores it to a variable (passed-by-reference) in version R40+.
You can also use:
Код:
for(new i, j = cache_num_rows(); i < j; i++)
Now I'm not getting errors while trying to compile but the game is not updating any data, just saving the weapons and not loading them, my code:

Код:
UpdatePlayerData(playerid, reason)
{
	if (pInfo[playerid][IsLoggedIn] == false) return 0;

	// if the client crashed, it's not possible to get the player's position in OnPlayerDisconnect callback
	// so we will use the last saved position (in case of a player who registered and crashed/kicked, the position will be the default spawn point)
	if (reason == 1)
	{
		GetPlayerPos(playerid, pInfo[playerid][X_Pos], pInfo[playerid][Y_Pos], pInfo[playerid][Z_Pos]);
		GetPlayerFacingAngle(playerid, pInfo[playerid][A_Pos]);
  		GetPlayerHealth(playerid, pInfo[playerid][pSalud]);
		GetPlayerArmour(playerid, pInfo[playerid][pChaleco]);
		GetPlayerMoney(playerid);
		

	}
	
	new query[145];
	mysql_format(g_SQL, query, sizeof query, "UPDATE `jugadores` SET `PosX` = %f, `PosY` = %f, `PosZ` = %f, `PosA` = %f, `interior` = %d WHERE `id` = %d LIMIT 1", pInfo[playerid][X_Pos], pInfo[playerid][Y_Pos], pInfo[playerid][Z_Pos], pInfo[playerid][A_Pos], GetPlayerInterior(playerid), pInfo[playerid][ID]);
	mysql_tquery(g_SQL, query);

	new query2[145];
	mysql_format(g_SQL, query2, sizeof query2, "UPDATE `jugadores` SET `pSalud` = %f, `pChaleco` = %f, `pDinero` = %d WHERE `id` = %d LIMIT 1", pInfo[playerid][pSalud], pInfo[playerid][pChaleco], GetPlayerMoney(playerid), pInfo[playerid][ID]);
	mysql_tquery(g_SQL, query2);
	
    
    SavePlayerWeapons(playerid);
    return 1;

}

SavePlayerWeapons(playerid)
{
    	new
		armaID,
		municion;

		for(new i; i < 13; i++) // looping through all weapon slots (0 - 12)
		{
			GetPlayerWeaponData(playerid, i, armaID, municion); // get weaponid and ammo

			if(!armaID) continue; // don't insert if there's no weapon in this slot
	        new query3[145];
			mysql_format(g_SQL, query3, sizeof(query3), "INSERT INTO armas_usuarios (`userID`, `armaID`, `municion`) VALUES (%d, %d, %d) ON DUPLICATE KEY UPDATE municion = %d;", pInfo[playerid][ID], armaID, municion, municion);
			mysql_tquery(g_SQL, query3); // parallel queries
		}
}

 forward OnLoadPlayerWeapons(playerid);
public OnLoadPlayerWeapons(playerid)
{
	new
	    weaponid,
	    ammo;

    new query2[145];
    mysql_format(g_SQL, query2, sizeof(query2), "SELECT armaID, municion FROM armas_usuarios WHERE id = %d;  ", pInfo[playerid][ID]);
	mysql_tquery(g_SQL, query2); // parallel queries
	
	for(new i, j = cache_num_rows(); i < j; i++) // loop through all the rows that were found
	{
	    weaponid 	= cache_get_value_index_int(i, 0, weaponid);
	    ammo    	= cache_get_value_index_int(i, 1, ammo);

		if(!(0 <= weaponid <= 46)) // check if weapon is valid (should be)
		{
			printf("[info] Warning: OnLoadPlayerWeapons - Unknown weaponid '%d'. Skipping.", weaponid);
			continue;
		}

		GivePlayerWeapon(playerid, weaponid, ammo);
	}
	return;
}
Just for reference:
jugadores = players
chaleco = armour
salud = health
arma = weapon
municion = ammo


If I delete the SavePlayerWeapons/LoadPlayerWeapons everything starts working again. I hate not being able to debug.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)