SA-MP Forums

SA-MP Forums (https://forum.sa-mp.com/index.php)
-   Scripting Help (https://forum.sa-mp.com/forumdisplay.php?f=12)
-   -   Creating a function (https://forum.sa-mp.com/showthread.php?t=563858)

Alvin007 17/02/2015 08:18 PM

Creating a function
 
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 Code:

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( 708) : 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 (c) 1997-2006, ITB CompuPhase


4 Errors.


HazardouS 17/02/2015 08:40 PM

Re: Creating a function
 
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 Code:
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 Code:
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 Code:
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.

CalvinC 17/02/2015 08:42 PM

Re: Creating a function
 
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 Code:
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 Code:
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 Code:
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 Code:
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.

Alvin007 17/02/2015 09:11 PM

Re : Creating a function
 
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 Code:

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

Code:

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 (c) 1997-2006, ITB CompuPhase


2 Errors.


CalvinC 17/02/2015 09:24 PM

Re: Creating a function
 
Can you show where you define "BusinessInfo[id][bLocked]"?

Alvin007 17/02/2015 09:34 PM

Re : Creating a function
 
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]"

Alvin007 18/02/2015 10:48 AM

Re : Creating a function
 
B UMP

Gammix 18/02/2015 01:22 PM

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

PHP Code:

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


Alvin007 19/02/2015 11:08 AM

Re : Creating a function
 
No sir, 3 Parameters.

Excel™ 19/02/2015 11:34 AM

Re: Re : Creating a function
 
Quote:

Originally Posted by Alvin007 (Post 3376528)
No sir, 3 Parameters.

pawn Code:
ChangeBizInfo(bizid,bizname[])
I don't think so^


All times are GMT. The time now is 08:36 PM.

Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.