Could someone assist me with my weapon system?
#1

Basically, my system is through MySQL and I literally had a guy help me out with it a while ago, he's out of contact and I had a break from SAMP. Can't seem to remember what does what precisely.


My weapon saves like this:

WeaponSlot is retrieved from WeaponModel.
PlayerWeapons[playerid][w] = SQLID of the weapon in the WeaponSlot i.e MP5 = 4th Slot so W4 = MP5 SQLID.

w = 13 slots (Max weapons.)


This command seems to return correctly for GetWepEX, GetWepIndexFromSQL sometimes displays as -1, although sometimes it's correct.
pawn Код:
CMD:wepcheck(playerid, params[])
{
    new string[50];
    new wep = GetPlayerWeaponEx(playerid);
    format(string, sizeof(string),"GetWepEX: %d, GetWepIndexFromSQL: %d",wep, GetWepIndexFromSQL(wep));
    SendClientMessage(playerid, COLOUR_WHITE, string);
    return 1;
}
This is the function I do not understand. The comments are conversations between me and the guy who was helping.
pawn Код:
stock GetWepIndexFromSQL(weapid)//returns the INDEX of the serverside database (PlayerWeapons[playerid][w] by giving the SQLID as parameter
{//needed when using WepVARIABLES
    new rst = -1;
    new string[40];
    format(string, sizeof(string), "SELECT `id` FROM `weapons`");
    mysql_query(string);
    mysql_store_result();
    new maxr = mysql_num_rows();
    mysql_free_result();
    for(new n=0; n<maxr; n++)
    {
        if(weapid == WepSQLID[n])   //at this point ==> weapid is the SQLID we have from PlayerWeapons
        {//and WepSQLID[n] is the SQL ID that is stored in the serverside table... n is the specific index (the number
            rst = n;//that we need)... Ahh. But if PlayerWeapons stores the SQLID? PlayerWeapons stores the SQLID, I don't
            break;// get your question:D
        }
    }
    return rst;
}
Seriously, any help would be greatly appreciated.
Reply
#2

If it can't select anything then thats probably why it gives you "-1" since it is by default "-1" due to your definition.
Reply
#3

I don't understand?
Reply
#4

Bump. Still unresolved.
Reply
#5

(I tried to mark all MySQL Db things as bold and all serverside table / serverside DB things as italic)


You're having 2 Databases / Tabels - On the one hand you've got your MySQL Database, which is storing each weapon in a new line, with the first line starting with ID 1 - this is the
first line number (so line 1 -> ID 1)
To save bandwhich and such, you transfere the MySQL Database to the server itself. You create a "table" (so an array in your case). You can access each line with a parameter in your brackets (index)
However the table on your server does NOT start with line 1 -> ID 1! Here the first line has the index 0, so line 1 -> INDEX 0

Basicly each line on your server should have the index of SQLID-1 -> one number lower
When getting data from the MySQL Table you need to "translate" / convert them, to access the right line in the server's table, as the ID != INDEX

GetWepEOIndexFromSQL will tell you the INDEX of the line you are searching for from your server's table, when you know the MySQL ID.
so
pawn Код:
stock GetWepIndexFromSQL(weapid)
means
Код:
Tell me the table INDEX, the ID I know is 'weapid' (weapid->MySQL ID!)
Now you start defining some variables. We call the variable that will store our result "rst". But as the first INDEX in your table is 0, we define it as rst = -1! We do this incase we won't find
any matching line. As we use -1 we won't get the first line in the case there actually is no line.

The following part
pawn Код:
new string[40];
format(string, sizeof(string), "SELECT `id` FROM `weapons`");
mysql_query(string);
mysql_store_result();
new maxr = mysql_num_rows();
mysql_free_result();
askes the MySQL DB for the amount of all lines in the table, as we may need this in the loop below.
We coult although also use "sizeof(WepSQLID)" instead of "maxr" in the "for" loop

Now the part that you might actually struggle with:
pawn Код:
for(new n=0; n<maxr; n++)
Our loop. We start with n = 0 and increase it each time until we reach maxr -> the amount of lines we have in our MySQL Database. We use n < maxr as we start with n = 0! If we would go to
n = maxr we would go increase it by one number to much (n = 0 - first line [ID1]; n = 1 - second line [ID2]; ...; n = maxr-1 - last line [IDMAX])

Now we compare
pawn Код:
if(weapid == WepSQLID[n])
weapid is the parameter we gave the function at the top (stock GetWepIndexFromSQL(weapid)). As told above, weapid is the ID of the MySQL Table (so in the ideal case it should be n + 1).
WepSQL is a table that connects the MySQL Table and the table of your server! It is ordered the same way as your weapon table in your server is:
The first field in the table would be the INDEX of the weapon (the one that is assigned on your serverside table). The second field is the ID of the weapon (the one that is used in the MySQL DB).
it's like:
Код:
index 0 => id 1
index 1 => id 2
index 2 => id 3
index 3 => id 4
etc...
We know the ID and want to know the index.
Therefor we use
pawn Код:
weapid == WepSQLID[n]
n is the "index". As WepSQLID[INDEX] returns the ID assigned to the specific INDEX we just check whether the ID we know (weapid) is the same as we have in the actual table line.
E.g. if we had ID 4 (GetWepIndexFromSQL(4))
We would start with n = 0.
Код:
We had "weapid == WepSQLID[n]" ==> "4 == WepSQLID[0]" - that would be false, as as we can see in the list above, index 0 as ID 1 assigned, which is obviously not ID 4
So we skip the rest and start again, but this time with n = 1
"weapid == WepSQLID[n]" ==> "4 == WepSQLID[1]" - still false, as index 1 has ID 2 assigned.
So again
"4 == WepSQLID[2]" - false, as index 2 => ID 3
"4 == WepSQLID[3]" - TRUE! index 3 has ID 4 assigned!
now
pawn Код:
rst = n;
break;
is called
As we have a matching line now, we can remove the "-1" we assigned to the "rst" before and replace it with the actual line, that the line in the MySQL DB would have in the serverside table.
We just found out that the index we were searching for is "3" - the one that is assigned to our "n". So we assign this to the rst variable.
At this point we know which line/index in the serverside table we need and we have stored in a variable. Therefor there is no need in going on with our loop anymore, as we suppose there is only
1 matching line. As we have found it, we can stop searching the through the following lines -> we stop the loop with
pawn Код:
break;
We
pawn Код:
return rst;
-> meaning the function now tells you
Код:
I have found a result, it is X" with X = whichever number is stored in rst -> -1 in case there is no found matching line!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)