SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 25/09/2018, 06:56 PM   #1
XStormiest
Gangsta
 
Join Date: Jun 2012
Location: Continent: Europe, Country: Romania, City: Buzau
Posts: 558
Reputation: 46
Default Weapons loading problem

So I have somewhere in my database weapons and ammo for each slot saved like this:

Code:
0 0 0 0 32 0 0 0 0 0 0 0 0
and for ammo
Code:
0 0 0 0 328 0 0 0 0 0 0 0 0
This is the code
Code:
Public:OnCWeaponsLoad(playerid)
{
	new string1[256], string2[256], c[1];
	new rows;
	cache_get_row_count(rows);
	if(rows)
	{
		cache_get_value_name(0, "pWeapons", string1, 256);
		cache_get_value_name(0, "pWeaponsAmmo", string2, 256);
		
		for(new i = 0; i < 13; i++)
		{
		   c[0] = string1[2*i];
		   C_Weapons[playerid][cWeaponID][i] = c[0];
		}
		
		for(new i = 0; i < 13; i++)
		{
		   c[0] = string2[2*i];
		   C_Weapons[playerid][cWeaponAmmo][i] = c[0];
		}
	}
	return 1;
}
At first I tried to use sscanf but failed cause it would just look ugly with a long line for 13 arguments, so I tried somewhat a clever aproach and it almost did the trick but not quite it seems, I gave myself a deagle just for test purposes and instead it changed it into a tec weapon with some random ammo.
__________________
XStormiest is offline   Reply With Quote
Old 25/09/2018, 07:19 PM   #2
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: Weapons loading problem

Databases were not designed to store a text with a bunch of integer values just to split them on your own. Create another table for the weaponry (userid, weaponid, ammo).

As for the reason your code fails is because it takes the ASCII character, you have to use strval. Any ID of weapon which is 2-digit will not work as it expects 1-digit. sscanf is the best choice if you decide to use this method which I advise you not to!

pawn Code:
new split_weapons[13], split_ammo[13];

sscanf(string1, "a<d>[13]", split_weapons);
sscanf(string2, "a<d>[13]", split_ammo);

for (new i; i < 13; i++)
{
    C_Weapons[playerid][cWeaponID][i] = split_weapons[i];
    C_Weapons[playerid][cWeaponAmmo][i] = split_ammo[i];
}

It may be possible to pass `C_Weapons[playerid][cWeaponID]` directly in sscanf arguments as replacement of `split_weapons` but I am unable to test it myself.
Calisthenics is offline   Reply With Quote
Old 25/09/2018, 07:33 PM   #3
XStormiest
Gangsta
 
Join Date: Jun 2012
Location: Continent: Europe, Country: Romania, City: Buzau
Posts: 558
Reputation: 46
Default Re: Weapons loading problem

I designed it this way so it won't be like 10000000 rows for each player in the database, this way is faster.
But thanks for reminding me of strval I completely forgot that.
I will change that and tell if it worked or not..
__________________
XStormiest is offline   Reply With Quote
Old 25/09/2018, 07:56 PM   #4
Calisthenics
High-roller
 
Join Date: May 2018
Posts: 1,228
Reputation: 205
Default Re: Weapons loading problem

MySQL can handle millions of rows, the number of them is meaningless to you if you design the database properly and I assure you that your method is not a good one -- it is not faster either.

You only need to store the weapons each player has in their inventory. If a player does not have any weapons, it will not load anything but in your example it will load a text, split 13 zeros and assign to a player-array that its values are already zeros!
Calisthenics is offline   Reply With Quote
Old 25/09/2018, 08:02 PM   #5
XStormiest
Gangsta
 
Join Date: Jun 2012
Location: Continent: Europe, Country: Romania, City: Buzau
Posts: 558
Reputation: 46
Default Re: Weapons loading problem

Okay, thank you ,I'll change that.
__________________
XStormiest is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Weapons not loading when relog Affan Scripting Help 8 03/05/2014 05:43 PM
This won't work, loading weapons. Dokins Scripting Help 0 06/04/2014 03:18 PM
Weapons MySQL loading ancezas Scripting Help 7 20/03/2013 04:51 PM
Loading players weapons Snowman12 Scripting Help 1 09/01/2012 12:21 AM
Crashing after loading weapons. borba Help Archive 8 27/04/2011 08:45 AM


All times are GMT. The time now is 04:53 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.