Creating a function
#1

Actually i have a problem with creating functions, it's a small one don't worry.
In my case i want to make a function to update a business 3D text label.
I searched everywhere in samp wiki, i found pre-made functions such as CreateVehicle etc..
And i found functions that can use only the variable (playerid)
In my case i need 2 parameters which are bizname and bizid.

Here is the Script:
PHP код:
new bizidbizname[100];
ChangeBizInfo(bizid,bizname[])
{
    new 
string[300];
    
BusinessInfo[bizid][bName] = bizname;
    
format(string,sizeof(string),"{FF0000}SERVER:{FFFFFF} You successfully changed your business bizname id [%i], to %s",bizid,bizname);
    
SendClientMessage(playeridYELLOWstring);
    
SaveBusiness(bizid);
    new 
salemsg[100],total[400],thestring[260],lockmsg[40];
    if(
BusinessInfo[bizid][bPrice]>-1format(salemsg,sizeof(salemsg),"[{009ACD}FOR SALE{FFFFFF}]\t {009ACD}Price:{FFFFFF} %i\n",BusinessInfo[bizid][bPrice]);
    else 
format(salemsg,sizeof(salemsg)," ");
    if(
BusinessInfo[bizid][bOwned]==1format(thestring,sizeof(thestring),"{009ACD}Name:{FFFFFF} %s \n {009ACD}Owner:{FFFFFF} %s \n ID: %d",bizname,BusinessInfo[bizid][bOwner],bizid);
    else 
format(thestring,sizeof(thestring),"{009ACD}Name:{FFFFFF} %s \n {009ACD}Owner:{FFFFFF} None \n ID: %d",bizname,bizid);
    if(
BusinessInfo[bizid][bLocked]==1format(lockmsg,sizeof(lockmsg),"[{009ACD}CLOSED{FFFFFF}]\n");
    else 
format(lockmsg,sizeof(lockmsg),"[{009ACD}OPEN{FFFFFF}]\n");
    
format(total,sizeof(total),"%s %s %s",salemsg,lockmsg,thestring);
    
Delete3DTextLabel(BizLabel[bizid]);
    
Create3DTextLabel(total,COLOR_WHITE,BusinessInfo[bizid][bEntranceX], BusinessInfo[bizid][bEntranceY], BusinessInfo[bizid][bEntranceZ],5.0BusinessInfo[bizid][bWorld]);
    return 
1;

And here is the error:
Quote:

C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn( 346) : warning 219: local variable "bizid" shadows a variable at a preceding level
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn( 346) : warning 219: local variable "bizname" shadows a variable at a preceding level
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn( 349) : error 047: array sizes do not match, or destination array is too small
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn( 351) : error 017: undefined symbol "playerid"
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn( 361) : error 017: undefined symbol "BizLabel"
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn( 70 : error 001: expected token: ";", but found "}"
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn( 1681) : warning 203: symbol is never used: "bizid"
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn( 1681) : warning 203: symbol is never used: "bizname"
Pawn compiler 3.2.3664 Copyright © 1997-2006, ITB CompuPhase


4 Errors.
Reply
#2

Your function is created correctly (unless you placed that code inside a callback). There are, however, some errors in your code. First of all, you are missing the "playerid" parameter, so the SendClientMessage in your function doesn't know who to send the message to. The second thing that you should know is that function parameters don't have to be declared globally, because they are automatically declared locally, strictly for that function call (so remove the "new bizid, ...;" line). The third error is the size of your bizname parameter. Because it is not any string and you want to copy it in another one, you have to specify its size. Change bizname[] to bizname[size_of_bName_field]. Of course, size_of_bName_field is actually the size that you declared in your biz Info enum. The rest of your function is fine, probably, except you didn't declare BizLabel array anywhere. Make sure you do that.

Now, a short (very very short) lesson about how to declare a function. There are 3 ways to declare a function, at least I think so.
First one, public function (has to be forwarded):
pawn Код:
forward MyPublicFunction(param1, param2[], Float:param3);
public MyPublicFunction(param1, param2[], Float:param3)
{
//code
//return some_value;
}
Notice how I wrote the parameters in the exact same order and without messing their types (int, string, float).

The second method, your method:
pawn Код:
MyRandomFunction(param1, param2[], Float:param3)
{
//code
}
Notice that this function doesn't have that "public" or "forward" keyword in front of it, you write that anywhere in your script and it will work fine. It will give a warning if it's not used in the script.

The third method, which is very similar to the second one:
pawn Код:
stock MyRandomFunction(param1, param2[], Float:param3)
{
//code
}
Notice the keyword "stock" in front of it. The third method is actually the same as the second one, except it won't give any warnings if it's not used. It doesn't have to be forwarded, just place it somewhere and you're good to go.

The advantage of public functions? They can be called from other scripts (filterscripts) via CallRemoteFunction.
Reply
#3

Quote:

error 001: expected token: ";", but found "}"

I think this is from another part of your script, but for the other errors:
__________________________________________________ _________________________________________
Quote:

error 017: undefined symbol "playerid"

You are trying to send a message to "playerid":
pawn Код:
SendClientMessage(playerid, YELLOW, string);
Yet you didn't define who "playerid" is, as you can see on your function, you only define "bizid" and "bizname" in the parameters.
pawn Код:
ChangeBizInfo(bizid,bizname[])
And you haven't defined it anywhere else in your function either.
__________________________________________________ _________________________________________
Quote:

error 017: undefined symbol "BizLabel"

Delete3DTextLabel(BizLabel[bizid]);
You didn't define "BizLabel" anywhere, example:
pawn Код:
BizLabel[bizid] = Create3DTextLabel( ... );
__________________________________________________ _________________________________________
Quote:

warning 219: local variable "bizid" shadows a variable at a preceding level
warning 219: local variable "bizname" shadows a variable at a preceding level

You define "bizid" and "bizname" globally outside your function, but then you define it again in your function parameters.
pawn Код:
ChangeBizInfo(bizid,bizname[])
Therefore when you etc. try to format "bizname", it doesn't know which one to use.
You should only use them in your function parameters, delete the global ones.
Reply
#4

Thanks alot guys, +REP.
But could you please solve this problem.
The function worked fine for changing the business' name.
But not working fine in here:
PHP код:
if(BusinessInfo[id][bLocked]==1)
            {
                 
BusinessInfo[id][bLocked]=0;
                 
GameTextForPlayer(playerid"Biz ~g~unlocked!"50003);
                
ChangeBizInfo(id,BusinessInfo[id][bName],playerid);
             }
            else if(
BusinessInfo[id][bLocked]==0)
            {
                 
BusinessInfo[id][bLocked]=1;
                 
GameTextForPlayer(playerid"Biz ~r~locked!"5003);
                 
ChangeBizInfo(id,BusinessInfo[id][bName],playerid);
             } 
Код:
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn(779) : error 047: array sizes do not match, or destination array is too small
C:\Users\moka\Desktop\PROG\03z\gamemodes\test.pwn(785) : error 047: array sizes do not match, or destination array is too small
Pawn compiler 3.2.3664	 	 	Copyright © 1997-2006, ITB CompuPhase


2 Errors.
Reply
#5

Can you show where you define "BusinessInfo[id][bLocked]"?
Reply
#6

Those two errors in lines number 779 and 785 are respectively the first and the second ChangeBizInfo, there's no problem with the "BusinessInfo[id][bLocked]"
Reply
#7

B UMP
Reply
#8

ChangeBizInfo have 2 params as per your code, so no need to modify it while implementing. Here you go:

PHP код:
if(BusinessInfo[id][bLocked]==1)
            {
                 
BusinessInfo[id][bLocked]=0;
                 
GameTextForPlayer(playerid"Biz ~g~unlocked!"50003);
                
ChangeBizInfo(id,BusinessInfo[id][bName]);
             }
            else if(
BusinessInfo[id][bLocked]==0)
            {
                 
BusinessInfo[id][bLocked]=1;
                 
GameTextForPlayer(playerid"Biz ~r~locked!"5003);
                 
ChangeBizInfo(id,BusinessInfo[id][bName]);
             } 
Reply
#9

No sir, 3 Parameters.
Reply
#10

Quote:
Originally Posted by Alvin007
Посмотреть сообщение
No sir, 3 Parameters.
pawn Код:
ChangeBizInfo(bizid,bizname[])
I don't think so^
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)