[ADD-ON]PVars Saved and Loaded Automatically -- No need to think about it
#1

I thought I would go ahead and release a fast save and load for PVars before someone released some monstrousity version of it.

pawn Код:
#define MAX_PVAR_NAME 24
#define MAX_PVAR_LENGTH 64
#define PVAR_LENGTH_COMBO MAX_PVAR_LENGTH+MAX_PVAR_NAME+10
SAVING FUNCTION
pawn Код:
stock SavePVars(playerid)
{
  new tmp[PVAR_LENGTH_COMBO],File:file,pvar,index;
  GetPlayerName(playerid,tmp,24);
  format(tmp,PVAR_LENGTH_COMBO,"Accounts/%s.samp",tmp);
  printf("Saving PVars at \"%s\" -- PlayerID=%d",tmp,playerid);
  file=fopen(tmp,io_write);
  index=GetPVarsUpperIndex(playerid);
  while(pvar<index)
  {
    GetPVarNameAtIndex(playerid,pvar,tmp,MAX_PVAR_NAME);
    switch(GetPVarType(playerid,tmp))
    {
      case PLAYER_VARTYPE_STRING:
      {
        new tmp2[MAX_PVAR_LENGTH];
        GetPVarString(playerid,tmp,tmp2,MAX_PVAR_LENGTH);
        format(tmp,PVAR_LENGTH_COMBO,"STRING %s%c%s\n",tmp,1,tmp2);
        fwrite(file,tmp);
      }
      case PLAYER_VARTYPE_INT:
      {
        new tmp2;
        tmp2=GetPVarInt(playerid,tmp);
        format(tmp,PVAR_LENGTH_COMBO,"INT  %s%c%d\n",tmp,1,tmp2);
        fwrite(file,tmp);
      }
      case PLAYER_VARTYPE_FLOAT:
      {
        new Float:tmp2;
        tmp2=GetPVarFloat(playerid,tmp);
        format(tmp,PVAR_LENGTH_COMBO,"FLOAT %s%c%f\n",tmp,1,tmp2);
        fwrite(file,tmp);
      }
    }
    pvar++;
  }
  print("Successful");
  fclose(file);
}
LOADING FUNCTION
pawn Код:
stock LoadPVars(playerid)
{
  new tmp[PVAR_LENGTH_COMBO],File:file;
  new tmp2[MAX_PVAR_LENGTH];
  new spacer[2];
  format(spacer,2,"%c",1);
  GetPlayerName(playerid,tmp,24);
  format(tmp,PVAR_LENGTH_COMBO,"Accounts/%s.samp",tmp);
  if(!fexist(tmp))return printf("Loading \"%s\" failed -- PlayerID=%d",tmp,playerid);
  printf("Loading PVars from \"%s\" -- PlayerID=%d",tmp,playerid);
  file=fopen(tmp,io_read);
  fread(file,tmp);
  while(tmp[0])
  {
    if(!strcmp(tmp,"STRING",false,6))
    {
      strmid(tmp2,tmp,strfind(tmp,spacer)+1,strlen(tmp)-1);
      strmid(tmp,tmp,7,strfind(tmp,spacer));
      SetPVarString(playerid,tmp,tmp2);
    }
    if(!strcmp(tmp,"INT  ",false,6))
    {
      strmid(tmp2,tmp,strfind(tmp,spacer)+1,strlen(tmp)-1,MAX_PVAR_LENGTH);
      strmid(tmp,tmp,7,strfind(tmp,spacer),MAX_PVAR_LENGTH);
      SetPVarInt(playerid,tmp,strval(tmp2));
    }
    if(!strcmp(tmp,"FLOAT ",false,6))
    {
      strmid(tmp2,tmp,strfind(tmp,spacer)+1,strlen(tmp)-1,MAX_PVAR_LENGTH);
      strmid(tmp,tmp,7,strfind(tmp,spacer),MAX_PVAR_LENGTH);
      SetPVarFloat(playerid,tmp,floatstr(tmp2));
    }
    fread(file,tmp);
  }
  print("Successful");
  fclose(file);
  return 1;
}
You would load under OnPlayerConnect and save them under OnPlayerDisconnect.

After applying these functions, you could use them assumingly. So no need to check if it exists, etc.

This could be pretty useful on all variables that relate to a player, so when a player comes back from disconnecting, he can be in the EXACT same spot he was in when he left, on his mission, capturing the flag, etc.

I imagine that after you load the PVars you would then apply things such as money, position, guns, etc.

If you wanted, you could even place this in a filterscript on its own. PVars can be read serverwide across filterscripts and gamemodes.
Reply
#2

Quote:
Originally Posted by Seif_
What's with the massive array size?
Kind of a safety measure, I would suggest lowering them. But the difference with that and an array size of 64, is miniscule enough for noobs to not worry about. Decent scripters should lower them, however.

EDIT* There, made it adjustable, and with reasonable sizes
Reply
#3

Good job.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)