Help with stock function
#1

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)
Reply
#2

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");

Reply
#3

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
Reply
#4

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"); 
Reply
#5

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

No errors in code, but what is this ?
Reply
#7

Any solution?
Reply
#8

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
Reply
#9

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...
Reply
#10

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]);
      }
   }

Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)