SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 17/02/2015, 09:18 PM   #1
Alvin007
Huge Clucker
 
Alvin007's Avatar
 
Join Date: Mar 2011
Location: Tunisia
Posts: 245
Reputation: 9
Default 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( 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 (c) 1997-2006, ITB CompuPhase


4 Errors.
__________________
Alvin007 is offline   Reply With Quote
Old 17/02/2015, 09:40 PM   #2
HazardouS
Huge Clucker
 
HazardouS's Avatar
 
Join Date: Aug 2014
Posts: 255
Reputation: 26
Default 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.
HazardouS is offline   Reply With Quote
Old 17/02/2015, 09:42 PM   #3
CalvinC
High-roller
 
CalvinC's Avatar
 
Join Date: Nov 2014
Location: Denmark
Posts: 1,831
Reputation: 114
Default 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.
__________________
[Tutorial] Custom Playerdamage system
Desert Eagle's dealing too much damage, you want one-hit sniper kills, or damage based on body parts? Check this out.
CalvinC is offline   Reply With Quote
Old 17/02/2015, 10:11 PM   #4
Alvin007
Huge Clucker
 
Alvin007's Avatar
 
Join Date: Mar 2011
Location: Tunisia
Posts: 245
Reputation: 9
Default 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.
__________________
Alvin007 is offline   Reply With Quote
Old 17/02/2015, 10:24 PM   #5
CalvinC
High-roller
 
CalvinC's Avatar
 
Join Date: Nov 2014
Location: Denmark
Posts: 1,831
Reputation: 114
Default Re: Creating a function

Can you show where you define "BusinessInfo[id][bLocked]"?
__________________
[Tutorial] Custom Playerdamage system
Desert Eagle's dealing too much damage, you want one-hit sniper kills, or damage based on body parts? Check this out.
CalvinC is offline   Reply With Quote
Old 17/02/2015, 10:34 PM   #6
Alvin007
Huge Clucker
 
Alvin007's Avatar
 
Join Date: Mar 2011
Location: Tunisia
Posts: 245
Reputation: 9
Default 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 is offline   Reply With Quote
Old 18/02/2015, 11:48 AM   #7
Alvin007
Huge Clucker
 
Alvin007's Avatar
 
Join Date: Mar 2011
Location: Tunisia
Posts: 245
Reputation: 9
Default Re : Creating a function

B UMP
__________________
Alvin007 is offline   Reply With Quote
Old 18/02/2015, 02:22 PM   #8
Gammix
High-roller
 
Gammix's Avatar
 
Join Date: Jan 2015
Location: Canada
Posts: 1,769
Reputation: 847
Default 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]);
             } 
__________________

Follow me on Github: Follow me on Spotify: Donate on Paypal:

Gammix is offline   Reply With Quote
Old 19/02/2015, 12:08 PM   #9
Alvin007
Huge Clucker
 
Alvin007's Avatar
 
Join Date: Mar 2011
Location: Tunisia
Posts: 245
Reputation: 9
Default Re : Creating a function

No sir, 3 Parameters.
__________________
Alvin007 is offline   Reply With Quote
Old 19/02/2015, 12:34 PM   #10
Excel™
Gangsta
 
Excel™'s Avatar
 
Join Date: Jul 2012
Posts: 549
Reputation: 95
Default Re: Re : Creating a function

Quote:
Originally Posted by Alvin007 View Post
No sir, 3 Parameters.
pawn Code:
ChangeBizInfo(bizid,bizname[])
I don't think so^
__________________
NO MORE CODING SAMP :V
JUST PLAYING...
Excel™ is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a new function Josh_Main Scripting Help 8 29/09/2014 07:15 PM
Need help with creating function Johnson_boy Help Archive 7 24/04/2011 11:35 AM
Creating a function [ Need help ] Cameltoe Help Archive 3 23/08/2010 07:10 PM


All times are GMT. The time now is 04:24 AM.


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