Confused about command return
#1

Hey everyone!

Just adding some basic commands and debugging when all of a sudden I notice something weird.
I have a custom message in OnPlayerCommandPerformed and that's ok, but why is it being called when I don't want to?

Example: I just took a little portion of code

pawn Код:
CMD:gethere(playerid,params[])
{
    new id;
        // codes...
    if(sscanf(params, "r",id)) return SendClientMessage(playerid,COLOR,"Use: {FFFFFF}/gethere [ID / Name]");
    else
    {
        if(PlayerInfo[id][LoggedOn] == false) return SCM(playerid, COLOR,"That dude is offline!");
        if(id == INVALID_PLAYER_ID)return SCM(playerid,COLOR,"Wrong ID!");
        if(id == playerid)return SCM(playerid,COLOR,"Wrong ID!");
                // codes...
if(id == playerid) WILL BE called, but other checks before won't, resulting in !success OnPlayerCommandPerformed.

Checks are actually in one line but I did this to debug.

This seems strange to me. Maybe I forgot how sscanf works with input arguments but I would really want someone to explain me the reason behind it.

Thanks
Reply
#2

Check your function "SCM", it is returning 0 (or some other function is) and calling OnPlayerCommandPerformed as a failure.
Reply
#3

Can you please, pleave, give more explications.
I mean, your command isn't working because nothing is check before if(id == playerid) ? If OnPlayerCommandPerformed is called, it's something is returning 0.

Try this one:
PHP код:
CMD:gethere(playerid,params[])
{
    new 
id;
        
// codes...
    
if(sscanf(params"u",id)) return SendClientMessage(playerid,COLOR,"Use: {FFFFFF}/gethere [ID / Name]");
    
/* u : player id or player's part of name
    - You don't need else here*/
     
if(PlayerInfo[id][LoggedOn] == false) return SCM(playeridCOLOR,"That dude is offline!");
    if(
id == INVALID_PLAYER_ID || id == playerid)return SCM(playerid,COLOR,"Wrong ID!");
    
// codes... 
EDIT: Mistakes (thx Sew_Sumi)
Reply
#4

Quote:
Originally Posted by Dayrion
Посмотреть сообщение
Show us SCM and, pleave, give more explications.
I mean, your command isn't working because nothing is check before if(id == playerid) ? If OnPlayerCommandPerformed is called, it's something is returning 0.

Your code can be improved and "r" doesn't exist in sscanf.
SCM is just a redefined SendClientMessage.

and

Quote:

r Player name/id ******, 42

Reply
#5

He didn't post the entire command code. I doubt there might be a run time error. Use crashdetect, compile with-d3.
Reply
#6

Quote:
Originally Posted by Sew_Sumi
Посмотреть сообщение
SCM is just a redefined SendClientMessage.

and
Yes, my bad. I checked this but there wasn't "r". By the way this one exist.
Anyway, he should try with "u" instead of "r".
Reply
#7

Quote:
Originally Posted by Sjn
Посмотреть сообщение
He didn't post the entire command code. I doubt there might be a run time error. Use crashdetect, compile with-d3.
You were right. I didn't however compile with -d3 but here is the log when trying /gethere and inputing ID's or names that aren't connected.

Код:
[18:34:32] [debug] Run time error 4: "Array index out of bounds"
[18:34:32] [debug]  Accessing element at index 65535 past array upper bound 49
[18:34:32] [debug] AMX backtrace:
[18:34:32] [debug] #0 000862b4 in public cmd_gethere (0, 887476) from Paradise.amx
[18:34:32] [debug] #1 native CallLocalFunction () from samp-server.exe
[18:34:32] [debug] #2 0000e5a0 in public FIXES_OnPlayerCommandText (0, 887432) from Paradise.amx
[18:34:32] [debug] #3 00003248 in public OnPlayerCommandText (0, 887432) from Paradise.amx
Reply
#8

Can you post the full command code? So it will be easier.
Reply
#9

Sure but nothing is wrong in the command tho

pawn Код:
CMD:gethere(playerid,params[])
{
    if(PlayerInfo[playerid][AdminLevel] < HEAD_ADMIN) return SendClientMessage(playerid, COLOR_NICERED, "Denied!");
    new id, String[128], Float:X,Float:Y,Float:Z;
    if(sscanf(params, "r",id)) return SendClientMessage(playerid,COLOR,"Use: {FFFFFF}/gethere [ID / Player name]");
    if(PlayerInfo[id][LoggedOn] == false) return SCM(playerid, COLOR,"That dude is offline!");
    if(id == INVALID_PLAYER_ID)return SCM(playerid,COLOR,"Wrong ID!");
    if(id == playerid)return SCM(playerid,COLOR,"Wrong ID!");
        GetPlayerPos(playerid,X,Y,Z);
    if (GetPlayerState(id) == PLAYER_STATE_DRIVER)
    {
        SetPlayerInterior(id, GetPlayerInterior(playerid));
        SetPlayerVirtualWorld(id, GetPlayerVirtualWorld(playerid));
        SetVehiclePos(GetPlayerVehicleID(id),X,Y+4,Z);
    }
    else
    {
        SetPlayerInterior(id, GetPlayerInterior(playerid));
        SetPlayerVirtualWorld(id, GetPlayerVirtualWorld(playerid));
        SetPlayerPos(id,X,Y+2,Z);
    }
    format(String,sizeof(String),"{FFFFFF}Admin {00C0FF}%s {FFFFFF}has tp-ed you!",GetName(playerid));
    SendClientMessage(id,COLOR,String);
    format(String,sizeof(String),"{FFFFFF}You tp-ed {00C0FF}%s {FFFFFF}near you!",GetName(id));
    SendClientMessage(playerid,COLOR,String);
    return 1;
}
Reply
#10

You must check if the specified id is invalid before accessing the array.

PHP код:
if(PlayerInfo[id][LoggedOn] == false) return SCM(playeridCOLOR,"That dude is offline!");
if(
id == INVALID_PLAYER_ID)return SCM(playerid,COLOR,"Wrong ID!"); 
PHP код:
if(id == INVALID_PLAYER_ID)return SCM(playerid,COLOR,"Wrong ID!");
if(
PlayerInfo[id][LoggedOn] == false) return SCM(playeridCOLOR,"That dude is offline!"); 
Reply
#11

Question answered ^


I tested your code and it was the login checker. It indexed an invalid id (65536) inside the login checker therefore it was giving the array index out of bound error.
Reply
#12

Quote:
Originally Posted by zPain
View Post
You must check if the specified id is invalid before accessing the array.

PHP Code:
if(PlayerInfo[id][LoggedOn] == false) return SCM(playeridCOLOR,"That dude is offline!");
if(
id == INVALID_PLAYER_ID)return SCM(playerid,COLOR,"Wrong ID!"); 
PHP Code:
if(id == INVALID_PLAYER_ID)return SCM(playerid,COLOR,"Wrong ID!");
if(
PlayerInfo[id][LoggedOn] == false) return SCM(playeridCOLOR,"That dude is offline!"); 
Yep, that fixes it. I really didn\'t know swapping places would work but when I think of it, accessing something that isn\'t possible will return !success. So basically I should be using INVALID check first in every command from now on.

A man can be forgetful sometimes.


Thank you for this, I appreciate it!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)