SA-MP Forums Archive
Help with stock function - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: Help with stock function (/showthread.php?tid=610917)



Help with stock function - nemanjasepa - 30.06.2016

I am trying to make drug system, but I have problem with stock... GetDrugName function won't show drug name...

Код HTML:
stock GetDrugName ( playerid ) {
   new drugname [ 30 ];
   for( new slot = 0; slot != 12; slot++ )
   {
      if ( PlayerInfo [ playerid ] [ pDrug ] [ slot ] == 1 ) { drugname = "Marijuana"; }
      if ( PlayerInfo [ playerid ] [ pDrug ] [ slot ] == 2 ) { drugname = "Cocaine"; }
      if ( PlayerInfo [ playerid ] [ pDrug ] [ slot ] == 3 ) { drugname = "Heroine"; }
   }
   return drugname;
}
Код HTML:
format(string, sizeof(string), "[ 1. %s (%d) ]", GetDrugName(PlayerInfo [ playerid ] [ pDrug ] [ 0]), PlayerInfo[playerid][pDrugAmount][0]);
So when you use /mydrugs it will show you [1 . (100) ] (for example)


Re: Help with stock function - Vince - 30.06.2016

I'm a little bit confused. You're passing some kind of slot into the function but it expects a playerid. I also have no idea what the loop is for. A simple array ought to work.

PHP код:
GetDrugName(drugid)
{
    static const 
DRUG_NAMES[][] = {
        
"Marijuana",
        
"Cocaine",
        
"Heroine"
        
// more ...
    
};
    
    return (
<= drugidsizeof DRUG_NAMES) ? (DRUG_NAMES[drugid]) : ("Unknown");




Re: Help with stock function - nemanjasepa - 30.06.2016

Hmm when you purchase drugs, you will get drugs on your first free slot, so for example it's 0

PlayerInfo [ playerid ] [ pDrug ] [ 0] = 1

and when player use /mydrugs
I want 1 to be converted into marijuana for example

Loop is for getting slot... Max is 12 slots


Re: Help with stock function - nemanjasepa - 30.06.2016

I am trying with your code, but I have an error
Код HTML:
error 001: expected token: ";", but found "return"
This is line
PHP код:
    return (<= slot sizeof DRUG_NAMES) ? (DRUG_NAMES[slot]) : ("Unknown"); 



Re: Help with stock function - Vince - 30.06.2016

Sorry, missed that. I don't usually test these small pieces of code myself. Edited my previous reply.


Re: Help with stock function - nemanjasepa - 30.06.2016

No errors in code, but what is this ?



Re: Help with stock function - nemanjasepa - 01.07.2016

Any solution?


Re: Help with stock function - SyS - 01.07.2016

PHP код:
GetDrugName playerid ) {
   new 
drugname 30 ];
   for( new 
slot 0slot != 12slot++ )
   {
      if ( 
PlayerInfo playerid ] [ pDrug ] [ slot ] == ) { drugname "Marijuana"; }
      if ( 
PlayerInfo playerid ] [ pDrug ] [ slot ] == ) { drugname "Cocaine"; }
      if ( 
PlayerInfo playerid ] [ pDrug ] [ slot ] == ) { drugname "Heroine"; }
   }
   return 
drugname;

PHP код:
format(stringsizeof(string), "[ 1. %s (%d) ]"GetDrugName(playerid), PlayerInfo[playerid][pDrugAmount][0]); 
the function is using playerid as parameter and it should be passed on calling


Re: Help with stock function - nemanjasepa - 01.07.2016

Quote:
Originally Posted by Sreyas
Посмотреть сообщение
PHP код:
format(stringsizeof(string), "[ 1. %s (%d) ]"GetDrugName(playerid), PlayerInfo[playerid][pDrugAmount][0]); 
the function is using playerid as parameter and it should be passed on calling
This works only if player have one kind of drug... But if player have 2 or more it will show only one...


Re: Help with stock function - jlalt - 01.07.2016

this supposed to work
PHP код:
GetDrugName(playeridslotID) {
   new 
drugname [30];
   switch(
PlayerInfo playerid ] [ pDrug ] [ slotID ]) {
      case 
1: {
          
drugname "Marijuana";
      }
      case 
2: {
          
drugname "Cocaine";
      }
      case 
3: {
          
drugname "Heroine";
      }
      default: {
          
drugname "-255";
      }
   }
   return 
drugname;

Usage:
GetDrugName(playerid, slotID);

example:
PHP код:
public OnPlayerConnect(playerid) {
   new 
string[128];
   for( new 
slot 0slot 13slot++ ) {
      new 
drugname[30];
      
drugname GetDrugName(playeridslot);
      if(
strval(drugname) != -255) {
          
format(stringsizeof(string), "%s[ %d. %s (%d) ], ",stringslot+1GetDrugName(playeridslot), PlayerInfo[playerid][pDrugAmount][slot]);
      }
   }