[Tutorial] MySQL and SAMP - Threading
#1

Hello there,

So I had little problem with overwriting some of data. Let me explain more, I have array PlayerInfos, with enum.
I call one query that gets players information's from database, and storing them into that array with sscanf. One time I had some serious issue, while players stats was saving (function was called in OnPlayerDisconnect) another player connected with that ID and overwrote that stats, so stats for previous player was not correct. I was thinking for about 10minutes how to solve that and found some article here, about threading. I looked at the code and get some idea. So now lets go to the tutorial part, enough chatting.

First what do we need?

We need mysql plugin (I use R6).
We need pawno too.
And of course hands and brain

pawn Код:
new bool:SavingStats[MAX_PLAYERS] = false;

stock SaveStats(playerid){
      SavingStats[playerid] = true;
      mysql_query("SOME QUERY", QUERY_ID, playerid);
      return true;
}

stock LoadStats(playerid){
     mysql_query("LOAD QUERY", ANOTHER_QUERY_ID, playerid);
     return true;
}
So in this code we made function for saving stats and loading them, most important thing here is SavingStats[playerid] = true, with that we will check is loading stats for that playerid allowed. We submit parameter playerid, because we will use it in rest of code for checking.

Now lets check and load stats

pawn Код:
public OnQueryFinish(query[], resultid, extraid, connectionHandle){
     switch(resultid){
          case ANOTHER_QUERY_ID:{
               while(SavingStats[extraid]){
                    if(SavingStats[extraid]) continue;
                    else break;
               }
               //load stats, do what you need
          }
          case QUERY_ID:{
               SavingStats[extraid] = false;
          }
     }
     return true;
}
In this code most important thing is while(SavingStats[extraid]), because it will not allow function to go further in and load the stats. In case QUERY_ID we set SavingStats[extraid] to false, and then the wile in case ANOTHER_QUERY_ID will break and loading stats will be granted.

I hope it will help someone. And sorry for my English.

All regards.
Reply
#2

Nice Tutorial , if you wanna make more tutorial i have a request for you, make a tutorial for an admin/register system with Admin levels using dini and ZCMD, because there are lots of people trying to find this kind of tutorial.
Reply
#3

I'll make it
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)