How would I create a mysql weapon system
#1

Hi guys, I'm working on my script and I can't get any idea on how would I make a weapon system, the saving and loading using mysql. I'm new to MySQL and I dont know much about saving arrays or etc. So I need your suggestions and comments on how should I make a weapon system.
Reply
#2

https://sampforum.blast.hk/showthread.php?tid=505081
Reply
#3

Quote:
Originally Posted by Lokii
Посмотреть сообщение
its so old, I think by now there would be a better version than this?
Reply
#4

Dont see how old is it, see how useful it is.
Reply
#5

Quote:
Originally Posted by MafiaOink
Посмотреть сообщение
its so old, I think by now there would be a better version than this?
SQL didn't change much since this thread came out, so it's pretty much the same.
Reply
#6

Quote:
Originally Posted by iKarim
Посмотреть сообщение
SQL didn't change much since this thread came out, so it's pretty much the same.
How would I load the values in the weapon slot arrays? you can simply have like weapon id 35 and 36 in the database despite having same slots.
confused.
Reply
#7

The code from the thread:

PHP код:
public OnLoadPlayerWeapons(playerid)
{
    new
        
weaponid,
        
ammo;
    
    for(new 
icache_get_row_count(userDB); ji++) // loop through all the rows that were found
    
{
        
weaponid     cache_get_row_int(i0userDB);
        
ammo        cache_get_row_int(i1userDB);
        
        if(!(
<= weaponid <= 46)) // check if weapon is valid (should be)
        
{
            
printf("[info] Warning: OnLoadPlayerWeapons - Unknown weaponid '%d'. Skipping."weaponid);
            continue;
        }
        
        
GivePlayerWeapon(playeridweaponidammo); 
    }
    return;

You have weaponid and ammo, so determine the slot from the weaponid and insert that into the array.
Reply
#8

Is it that hard to read a tutorial? Sql requires study as it is a whole new language. Just learn the basics and you wouldnt have to ask this
Reply
#9

I've took the tutorial and modified the code, please check if it is correct or not and tell me my mistakes.
Код:
SaveWeaponData(playerid)
{
	if(PlayerInfo[playerid][LoggedIn] == false) return 0;
	new mysqlquery[512];
	mysql_format(MySQL_Database, mysqlquery, sizeof(mysqlquery), "DELETE FROM `PlayerWeapons` WHERE accountid = %d;", PlayerInfo[playerid][ID]);
	mysql_query(MySQL_Database, mysqlquery);
	for(new i = 0; i < 13; i++)
	{
		if(PlayerInfo[playerid][WeaponSlot][i] < 1 || PlayerInfo[playerid][WeaponSlot][i] > 46) continue;
		if(PlayerInfo[playerid][WeaponSlotAmmo][i] < 1) continue;
		mysql_format(MySQL_Database, mysqlquery, sizeof(mysqlquery), "INSERT INTO `PlayerWeapons` VALUES (%d, %d, %d) ON DUPLICATE KEY UPDATE ammo = %d;", PlayerInfo[playerid][ID], PlayerInfo[playerid][WeaponSlot][i], PlayerInfo[playerid][WeaponSlotAmmo][i], PlayerInfo[playerid][WeaponSlotAmmo][i]);
		mysql_pquery(MySQL_Database, mysqlquery);
	}
	return 1;
}

SaveAccount(playerid, bool:gettingBanned = false, bool:sendMsg = false)
{
	if(PlayerInfo[playerid][LoggedIn] == false) return 0;
        // saving other statistics
	SaveWeaponData(playerid);
	return 1;
}
Creating database:
Код:
mysql_query(MySQL_Database, "CREATE TABLE IF NOT EXISTS `PlayerWeapons` ( \
	`accountid` INT(11) UNSIGNED NOT NULL , \
	`weaponid` TINYINT(3) UNSIGNED NOT NULL , \
	`ammo` INT(10) UNSIGNED NOT NULL , \
	INDEX `accountid` (`accountid`), \
	CONSTRAINT `accountid` FOREIGN KEY (`accountid`) REFERENCES `accounts`(`ID`) ON DELETE CASCADE ON UPDATE CASCADE\
	) ENGINE = InnoDB;");
OnPlayerConnect
Код:
for(new slot = 0; slot < 13; slot++)
	{
	    PlayerInfo[playerid][WeaponSlot][slot] = 0;
	    PlayerInfo[playerid][WeaponSlotAmmo][slot] = 0;
	}
	PlayerInfo[playerid][WeaponsLoaded] = 0;
OnPlayerLogin
Код:
new querySnd[256];
				mysql_format(MySQL_Database, querySnd, sizeof(querySnd), "SELECT weaponid, ammo FROM `PlayerWeapons` WHERE accountid = %d;", PlayerInfo[playerid][ID]);
				mysql_tquery(MySQL_Database, querySnd, "OnLoadWeaponData", "ii", playerid, Corrupt_Check[playerid]);
Loading Weapons
Код:
public OnLoadWeaponData(playerid, corrupt_check)
{
	if(corrupt_check != Corrupt_Check[playerid]) return SFM(playerid, COLOR_RED, "Please reconnect as there has been an unknown error - Our apologies!"), Kick(playerid);
    new
        weaponid,
        ammo,
        j;
        
	cache_get_row_count(j);
    for(new i; i < j; i++)
    {
        cache_get_value_index_int(i, 0, weaponid);
        cache_get_value_index_int(i, 1, ammo);

        if(!(0 <= weaponid <= 46))
        {
            printf("[info] Warning: OnLoadWeaponData - Unknown weaponid '%d'. Skipping.", weaponid);
            continue;
        }
		if(ammo < 1)
		{
		    printf("[info] Warning: OnLoadWeaponData - Invalid ammo '%d'. Skipping.", ammo);
		    continue;
		}
		new slot = GetWeaponSlot(weaponid);
        PlayerInfo[playerid][WeaponSlot][slot] = weaponid;
        PlayerInfo[playerid][WeaponSlotAmmo][slot] = ammo;
    }
	PlayerInfo[playerid][WeaponsLoaded] = 1;
	SetTimerEx("LoadPlayerWeapons", 1000, false, "ii", playerid, Corrupt_Check[playerid]);
    return 1;
}

public LoadPlayerWeapons(playerid, corrupt_check)
{
	if(!PlayerInfo[playerid][LoggedIn]) return Kick(playerid);
    if(corrupt_check != Corrupt_Check[playerid])
    {
        return SFM(playerid, COLOR_RED, "An inrecoverable error has occured, PLease reconnect - Our apologies!"), Kick(playerid);
    }
    if(!IsPlayerSpawned(playerid)) return SetTimerEx("LoadPlayerWeapons", 1000, false, "ii", playerid, Corrupt_Check[playerid]);
    new weaponid, ammo;
    for(new slot = 0; slot < 13; slot++)
    {
        weaponid = PlayerInfo[playerid][WeaponSlot][slot];
        ammo = PlayerInfo[playerid][WeaponSlotAmmo][slot];
		if(weaponid < 1 || weaponid > 46) continue;
		if(ammo < 1) continue;
		GivePlayerWeapon(playerid, weaponid, ammo);
    }
    PlayerInfo[playerid][WeaponsLoaded] = 2;
    return 1;
}
Reply
#10

iLearner said it best...

Quote:
Originally Posted by iLearner
Посмотреть сообщение
Dont see how old is it, see how useful it is.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)