Function to return a string -
Fel486 - 21.04.2015
Hello again, guys. I need a bit help on something I can't figure out. On public functions, it's not possible to return Strings, so I'm trying with Stock's, and doing it directly on the format string to be shown in my dialog!
If I remove the function that returns the string, it works; otherwise, if it's, the command returns 0 (Unknown Command).
So, here we are:
PHP Code:
stock EstaTrancada(casaid)
{
new status[300];
format(status, sizeof(status), "%d", -1);
if(casaid >= Server[0][quantCasas]) return status;
if(!strcmp(Casa[casaid][pass], "bgfr") || !strcmp(Casa[casaid][pass], "")) format(status, sizeof(status), ""#LIME"%s", "Aberta"); // Open
else format(status, sizeof(status), ""#RED"%s", "Trancada"); // Closed
printf("Status retornado: %s", status);
return status;
}
Command:
PHP Code:
CMD:menucasa(playerid, params[])
{
new str[1000];
printf("tentou"); // It always prints.
if(Player[playerid][quantPropriedades] < 1) return Msg(playerid, verde, "Vocк nгo possui nenhuma casa!");
format(str, sizeof(str),""#DARK_YELLOW"1) Informaзхes da casa\n"#BLUE"2) Localizar veнculo da casa\n"#ORANGE"3) Alterar password\n4) %s", EstaTrancada(GetCasa(playerid)));
return ShowPlayerDialog(playerid, CASA_DIALOG, DIALOG_STYLE_LIST, CASA_TITLE, str, "Confirmar", "Sair");
}
If there's a tip, I'll be thankful.
Re: Function to return a string -
[KHK]Khalid - 21.04.2015
I think it's something to do with the way you're using
Format in there. Start debugging your
EstaTrancada function with comments and prints aswell.
Re: Function to return a string -
Konstantinos - 21.04.2015
When a run time error occurs while executing a command, it gives the unknown command message. So load crashdetect plugin (
https://github.com/Zeex/samp-plugin-...es/tag/v4.15.1) and compile with -d3 (
https://github.com/Zeex/samp-plugin-...ith-debug-info).
I'll guess that run time error 4 is caused because of GetCasa(playerid) function which returns a value that goes off-bounds with the size of array "Casa". Though, let's see what crashdetect will print to be sure.
Re: Function to return a string -
Fel486 - 21.04.2015
PHP Code:
stock GetCasa(playerid)
{
new Cache:result, name[MAX_PLAYER_NAME];
if(Player[playerid][quantPropriedades] < 1) return -1;
GetPlayerName(playerid, name, sizeof(name));
for(new a = 0; a < Server[0][quantCasas]; a++)
{
printf("%s -> %s", Casa[a][dono], name);
if(!strcmp(Casa[a][dono], name)) return a;
}
return -1;
}
Tested myself just on printf, and it was returning always -1, even if names are equal, which I compared for 4 players on my server (Myself and 3 NPCS), and it printed:
PHP Code:
Fel_486 -> Fel_486
-> Fel_486
-> Fel_486
-> Fel_486
The Owner field on Mysql default I put as ' '. Well, the command is correct, and straight thing there is that the "return" doesn't stops the loop, lol...
Well, that's not normal...
Re: Function to return a string -
Konstantinos - 21.04.2015
So it was run time error 4.
Assuming the first output was equal, it should've returned the value of a. You could set the owner field to null and then check:
pawn Code:
if (!isnull(Casa[a][dono]) && !strcmp(Casa[a][dono], name, true, MAX_PLAYE_NAME))
For better results on the output, use
\"%s\" so we'll be able to clearly see the space(s).
Re: Function to return a string -
Fel486 - 21.04.2015
The same thing happened, it still shows the same as above. Casa[a][dono] is not null, I set it to
But that is not a character, it's a string, since it's VARCHAR, because I set as:
PHP Code:
dono VARCHAR(255) DEFAULT ' '
Yet you said it should've returned the value of a. However, it's not happening, loops continues till end.
Re: Function to return a string -
Konstantinos - 21.04.2015
I don't really know what it does that. Try using break instead:
PHP Code:
new value = -1;
for(new a = 0; a < Server[0][quantCasas]; a++)
{
if(!strcmp(Casa[a][dono], name))
{
printf("\"%s\"", Casa[a][dono]);
value = a;
break;
}
return value;
Re: Function to return a string -
Fel486 - 21.04.2015
Omg, you won't believe...
Since I put a space as default for being not null and I gave myself the house just by changing it's owner name on MYSQL, I just wrote my name, but there was the space before... I set and now works, sorry and thanks!