Small query not working as its supposed to
#1

I'm attempting to learn PAWN, and I've hit a wall.

Код:
if(ABusinessData[BusID][Owner] == APlayerData[playerid][PlayerName])
{
// do code if true
}
else
{
// do code if false
}
I've debugged the results of
ABusinessData[BusID][Owner] -- This returns "Dave"
APlayerData[playerid][PlayerName] -- This returns "Dave"

----

When I use the query, it always returns true, never false.
Reply
#2

You need to use a function to compare strings in PAWN.

pawn Код:
if( !strcmp( ABusinessData[BusID][Owner], APlayerData[playerid][PlayerName]) )
{
    //strings match if strcmp returns zero
}
https://sampwiki.blast.hk/wiki/Strcmp

Other useful string functions listed at bottom of that page.
Reply
#3

Sorry, I only posted a snippet of the code to save space, I'll post the full code instead.

Код:
COMMAND:busmenu(playerid, params[])
{
	// Setup local variables
	new OptionsList[200], DialogTitle[200], BusID;

	// Check if the player has logged in
	if (APlayerData[playerid][LoggedIn] == true)
	{
	    BusID = APlayerData[playerid][CurrentBusiness];
	    if(ABusinessData[BusID][Owner] == APlayerData[playerid][PlayerName])
	    {
			// Check if the player is inside a business
			if (BusID != 0)
			{
				format(DialogTitle, sizeof(DialogTitle), "Select option for %s", ABusinessData[APlayerData[playerid][CurrentBusiness]][BusinessName]);
				format(OptionsList, sizeof(OptionsList), "%sChange business-name\n", OptionsList);
				format(OptionsList, sizeof(OptionsList), "%sUpgrade business\n", OptionsList);
				format(OptionsList, sizeof(OptionsList), "%sRetrieve business earnings\n", OptionsList);
				format(OptionsList, sizeof(OptionsList), "%sSell business\n", OptionsList);
				format(OptionsList, sizeof(OptionsList), "%sExit business\n", OptionsList);
				// Show the businessmenu
				ShowPlayerDialog(playerid, DialogBusinessMenu, DIALOG_STYLE_LIST, DialogTitle, OptionsList, "Select", "Cancel");
			}
			else
			{
				SendClientMessage(playerid, 0xFF0000FF, "You're not inside a business");
			}
		}
		else
		{
			return 0;
		}
	}
	else
	{
	    return 0;
	}

	// Let the server know that this was a valid command
	return 1;
}
Reply
#4

Swap
pawn Код:
if(ABusinessData[BusID][Owner] == APlayerData[playerid][PlayerName])
For
pawn Код:
if( !strcmp( ABusinessData[BusID][Owner], APlayerData[playerid][PlayerName]) )
Reply
#5

My bad, misread your original post, thanks for the help, much appreciated!
Reply
#6

Eek, double post..

I've still got the problem, no matter what, the dialogue always shows, even if the values are different, any idea why?

Код:
COMMAND:busmenu(playerid, params[])
{
	// Setup local variables
	new OptionsList[200], DialogTitle[200], BusID;

	// Check if the player has logged in
	if(APlayerData[playerid][LoggedIn] == true)
	{
	    BusID = APlayerData[playerid][CurrentBusiness];
	    if(!strcmp(ABusinessData[BusID][Owner], APlayerData[playerid][PlayerName]))
	    {
			// Check if the player is inside a business
			if (BusID != 0)
			{
				format(DialogTitle, sizeof(DialogTitle), "Select option for %s", ABusinessData[APlayerData[playerid][CurrentBusiness]][BusinessName]);
				format(OptionsList, sizeof(OptionsList), "%sChange business-name\n", OptionsList);
				format(OptionsList, sizeof(OptionsList), "%sUpgrade business\n", OptionsList);
				format(OptionsList, sizeof(OptionsList), "%sRetrieve business earnings\n", OptionsList);
				format(OptionsList, sizeof(OptionsList), "%sSell business\n", OptionsList);
				format(OptionsList, sizeof(OptionsList), "%sExit business\n", OptionsList);
				// Show the businessmenu
				ShowPlayerDialog(playerid, DialogBusinessMenu, DIALOG_STYLE_LIST, DialogTitle, OptionsList, "Select", "Cancel");
			}
			else
			{
				SendClientMessage(playerid, 0xFF0000FF, "You're not inside a business");
			}
		}
		else
		{
			SendClientMessage(playerid, 0xFF0000FF, "You're not the owner of this business");
		}
	}
	else
	{
	    return 0;
	}

	// Let the server know that this was a valid command
	return 1;
}
Reply
#7

Add a print to see what value BusID is. Only if it is none zero the dialog will be shown.
pawn Код:
COMMAND:busmenu(playerid, params[])
{
    // Setup local variables
    new OptionsList[200], DialogTitle[200], BusID;

    // Check if the player has logged in
    if(APlayerData[playerid][LoggedIn] == true)
    {
        BusID = APlayerData[playerid][CurrentBusiness];
        if(!strcmp(ABusinessData[BusID][Owner], APlayerData[playerid][PlayerName]))
        {
            // Check if the player is inside a business
           
            printf("BusID == %d", BusID);
            if (BusID != 0)
            {
                //if BusID is any number exept zero this will be executed.
                format(DialogTitle, sizeof(DialogTitle), "Select option for %s", ABusinessData[APlayerData[playerid][CurrentBusiness]][BusinessName]);
                format(OptionsList, sizeof(OptionsList), "%sChange business-name\n", OptionsList);
                format(OptionsList, sizeof(OptionsList), "%sUpgrade business\n", OptionsList);
                format(OptionsList, sizeof(OptionsList), "%sRetrieve business earnings\n", OptionsList);
                format(OptionsList, sizeof(OptionsList), "%sSell business\n", OptionsList);
                format(OptionsList, sizeof(OptionsList), "%sExit business\n", OptionsList);
                // Show the businessmenu
                ShowPlayerDialog(playerid, DialogBusinessMenu, DIALOG_STYLE_LIST, DialogTitle, OptionsList, "Select", "Cancel");
            }
            else
            {
                SendClientMessage(playerid, 0xFF0000FF, "You're not inside a business");
            }
        }
        else
        {
            SendClientMessage(playerid, 0xFF0000FF, "You're not the owner of this business");
        }
    }
    else
    {
        return 0;
    }

    // Let the server know that this was a valid command
    return 1;
}
TIP: use [pawn] tags instead of [code].
Reply
#8

The ID that gets printed is "129"
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)