How to make a function that returns a string - 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: How to make a function that returns a string (
/showthread.php?tid=353706)
How to make a function that returns a string -
mrcoolballs - 24.06.2012
Hello,
I am working on a property script, and have made a function, but it does not compile,
I am trying to make it so that it returns "Nobody"
pawn Код:
stock GetPropertyOwnerName(property[])
{
new file[256],name[24];
format(file,sizeof(file),"gamemode/config/properties/%s.txt",property);
if(strcmp(dini_Get(file,"Property_Owner"),"AnUnusableSA-MPName"))
{
new str[7] = "Nobody";
return str;
}
format(name,sizeof(name),"%s",dini_Get(file,"Property_Owner"));
return name;//line 235
}
This is the error I am receiving:
Код:
(235) : error 047: array sizes do not match, or destination array is too small
although if i delete "return str" the error goes away.
It is really doing my head in, if anyone could help i would appreciate it.
Re: How to make a function that returns a string -
MP2 - 24.06.2012
Not sure, but perhaps every string has to be the same size. First of all name should be 25 cells because a name can be 24 characters but there's also the null character. Try this:
pawn Код:
stock GetPropertyOwnerName(property[])
{
new file[256];
new name[25] = "Nobody";
format(file, sizeof(file), "gamemode/config/properties/%s.txt", property);
if(strcmp(dini_Get(file, "Property_Owner"), "~") != 0)
{
format(name, sizeof(name), "%s", dini_Get(file, "Property_Owner"));
}
return name;
}
I would personally set the name to ~ or something if nobody uses the house (you can't have 1-character names).
Also, put spaces after commas it looks a hell of a lot clearer to read, trust me. Just compare my code and yours.