Function to return a string
#1

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(statussizeof(status), "%d", -1);
    if(
casaid >= Server[0][quantCasas]) return status;
    if(!
strcmp(Casa[casaid][pass], "bgfr") || !strcmp(Casa[casaid][pass], "")) format(statussizeof(status), ""#LIME"%s", "Aberta"); // Open
    
else format(statussizeof(status), ""#RED"%s", "Trancada"); // Closed
    
printf("Status retornado: %s"status);
    return 
status;

Command:

PHP Code:
CMD:menucasa(playeridparams[])
{
    new 
str[1000];
    
printf("tentou"); // It always prints.
    
if(Player[playerid][quantPropriedades] < 1) return Msg(playeridverde"Vocк nгo possui nenhuma casa!");
    
format(strsizeof(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(playeridCASA_DIALOGDIALOG_STYLE_LISTCASA_TITLEstr"Confirmar""Sair");

If there's a tip, I'll be thankful.
Reply
#2

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.
Reply
#3

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.
Reply
#4

PHP Code:
stock GetCasa(playerid)
{
    new 
Cache:resultname[MAX_PLAYER_NAME];
    if(
Player[playerid][quantPropriedades] < 1) return -1;
    
GetPlayerName(playeridnamesizeof(name));
    for(new 
0Server[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...
Reply
#5

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).
Reply
#6

The same thing happened, it still shows the same as above. Casa[a][dono] is not null, I set it to

PHP Code:
' ' 
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.
Reply
#7

I don't really know what it does that. Try using break instead:
PHP Code:
new value = -1;
for(new 
0Server[0][quantCasas]; a++)
{
    if(!
strcmp(Casa[a][dono], name)) 
    {
        
printf("\"%s\""Casa[a][dono]);
        
value a;
        break;
}
return 
value
Reply
#8

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!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)