House System Problem
#1

I've had lots of problems with this tiny piece of script, the problem is that when I type the command at My house. It works but the chat is spammed for me. When i get to another person's house. This code does not work. I need some help from you guys to help me fix it.

PHP код:
case DIALOG_HOUSEPASSWORDENTER:
{
    if(
response)
    {
        if(
strlen(inputtext) != 4)
        {
            
ShowPlayerDialog(playeridDIALOG_HOUSEPASSWORDENTERDIALOG_STYLE_INPUT,""COL_YELLOW"Lock Password (Numbers Only)",""COL_WHITE"Enter The House Password","Confirm","Close");
            return 
true;
        }
        for(new 
0MAX_HOUSESx++)
        {
               new 
pName4[MAX_PLAYER_NAME];
            new 
string41[128];
            new 
labelString[128];
            
GetPlayerName(playeridpName4MAX_PLAYER_NAME);
            if(
house[x][hLockCode] == strval(inputtext))
            {
                switch(
house[x][hHouseLocked])
                {
                    case 
1:
                    {
                        
format(string41sizeof(string41), "%s places their fingers on the keypad"pName4);
                        
SendLocalMessage(playeridstring4115.0COLOR_PURPLECOLOR_PURPLE);
                        
house[x][hHouseLocked] = 0;
                        
format(labelStringsizeof(labelString), "House ID: %d\nOwner: %s\n\nPress ~k~~PED_DUCK~ to enter."xhouse[x][hHouseOwner]);
                        
UpdateDynamic3DTextLabelText(house[x][hLabelID], COLOR_LIMEGREENlabelString);
                    }
                    case 
0:
                    {
                         
format(string41sizeof(string41), "%s places their fingers on the keypad"pName4);
                        
SendLocalMessage(playeridstring4115.0COLOR_PURPLECOLOR_PURPLE);
                        
house[x][hHouseLocked] = 1;
                        
format(labelStringsizeof(labelString), "House ID: %d\nOwner: %s\n\n(Locked)"xhouse[x][hHouseOwner]);
                        
UpdateDynamic3DTextLabelText(house[x][hLabelID], COLOR_LIMEGREENlabelString);
                    }
                }
                break;
            }
            else
            {
                
format(string41sizeof(string41), "%s places their fingers on the keypad"pName4);
                
SendLocalMessage(playeridstring4115.0COLOR_PURPLECOLOR_PURPLE);
                
PlayerActionMessage(playerid,15.0,"Incorrect Password");
            }
         }
    }

Thanks for your time, I've spent a lot of time trying different ways to get this to work but sadly it didn't.

The script saves and loads the code perfectly. I just have a problem with this part.
Reply
#2

Your biggest problem is that you are comparing hLockCode to the entered text for EVERY HOUSE. Before even performing any checks, you should have a function that obtains the closest house. Here is an example:

pawn Код:
stock GetClosestHouseID(playerid)
{
    new ClosestHouse;
    new Float:ClosestDistance = 10000000;
    for (new i = 1; i<MAX_HOUSES; i++)
    {
        new Float:Distance = GetPlayerDistanceFromPoint(playerid,HouseInfo[i][XPos],HouseInfo[i][YPos],HouseInfo[i][ZPos]);
        if(Distance<ClosestDistance)
        {
            ClosestDistance = Distance;
            ClosestHouse = i;
        }
    }
    return ClosestHouse;
}
The way the script is set up now, the message after "else" is sent every time that the hLockCode does not match the input text. If you have 500 houses, and only house id 400 has that Lock Code, you will get 399 messages before the loop is broken.

Also, I would perform the hHouseLocked check before the hLockCode check. It is easier for the server to compare two integers (0 or 1) than compare two strings.
Reply
#3

To be truthfull I really am confused. I've made this function "GetClosestHouseID(playerid)" to fit in with the dialog.

How am i supposed to use a loop and get the closest players house at the same time, I cant get my head around it :L

I've looked for examples but had no luck.
Reply
#4

pawn Код:
case DIALOG_HOUSEPASSWORDENTER:
{
    if(response)
    {
        if(strlen(inputtext) != 4)
        {
            ShowPlayerDialog(playerid, DIALOG_HOUSEPASSWORDENTER, DIALOG_STYLE_INPUT,""COL_YELLOW"Lock Password (Numbers Only)",""COL_WHITE"Enter The House Password","Confirm","Close");
            return true;
        }
        new pName4[MAX_PLAYER_NAME];
        new string41[128];
        new labelString[128];
        GetPlayerName(playerid, pName4, MAX_PLAYER_NAME);
        new x = GetClosestHouseID(playerid);
        if(house[x][hLockCode] == strval(inputtext))
        {
            switch(house[x][hHouseLocked])
            {
                case 1:
                {
                    format(string41, sizeof(string41), "%s places their fingers on the keypad", pName4);
                    SendLocalMessage(playerid, string41, 15.0, COLOR_PURPLE, COLOR_PURPLE);
                    house[x][hHouseLocked] = 0;
                    format(labelString, sizeof(labelString), "House ID: %d\nOwner: %s\n\nPress ~k~~PED_DUCK~ to enter.", x, house[x][hHouseOwner]);
                    UpdateDynamic3DTextLabelText(house[x][hLabelID], COLOR_LIMEGREEN, labelString);
                }
                case 0:
                {
                    format(string41, sizeof(string41), "%s places their fingers on the keypad", pName4);
                    SendLocalMessage(playerid, string41, 15.0, COLOR_PURPLE, COLOR_PURPLE);
                    house[x][hHouseLocked] = 1;
                    format(labelString, sizeof(labelString), "House ID: %d\nOwner: %s\n\n(Locked)", x, house[x][hHouseOwner]);
                    UpdateDynamic3DTextLabelText(house[x][hLabelID], COLOR_LIMEGREEN, labelString);
                }
            }
            break;
        }
        else
        {
            format(string41, sizeof(string41), "%s places their fingers on the keypad", pName4);
            SendLocalMessage(playerid, string41, 15.0, COLOR_PURPLE, COLOR_PURPLE);
            PlayerActionMessage(playerid,15.0,"Incorrect Password");
        }
    }
}
Reply
#5

Quote:
Originally Posted by MadeMan
Посмотреть сообщение
pawn Код:
case DIALOG_HOUSEPASSWORDENTER:
{
    if(response)
    {
        if(strlen(inputtext) != 4)
        {
            ShowPlayerDialog(playerid, DIALOG_HOUSEPASSWORDENTER, DIALOG_STYLE_INPUT,""COL_YELLOW"Lock Password (Numbers Only)",""COL_WHITE"Enter The House Password","Confirm","Close");
            return true;
        }
        new pName4[MAX_PLAYER_NAME];
        new string41[128];
        new labelString[128];
        GetPlayerName(playerid, pName4, MAX_PLAYER_NAME);
        new x = GetClosestHouseID(playerid);
        if(house[x][hLockCode] == strval(inputtext))
        {
            switch(house[x][hHouseLocked])
            {
                case 1:
                {
                    format(string41, sizeof(string41), "%s places their fingers on the keypad", pName4);
                    SendLocalMessage(playerid, string41, 15.0, COLOR_PURPLE, COLOR_PURPLE);
                    house[x][hHouseLocked] = 0;
                    format(labelString, sizeof(labelString), "House ID: %d\nOwner: %s\n\nPress ~k~~PED_DUCK~ to enter.", x, house[x][hHouseOwner]);
                    UpdateDynamic3DTextLabelText(house[x][hLabelID], COLOR_LIMEGREEN, labelString);
                }
                case 0:
                {
                    format(string41, sizeof(string41), "%s places their fingers on the keypad", pName4);
                    SendLocalMessage(playerid, string41, 15.0, COLOR_PURPLE, COLOR_PURPLE);
                    house[x][hHouseLocked] = 1;
                    format(labelString, sizeof(labelString), "House ID: %d\nOwner: %s\n\n(Locked)", x, house[x][hHouseOwner]);
                    UpdateDynamic3DTextLabelText(house[x][hLabelID], COLOR_LIMEGREEN, labelString);
                }
            }
            break;
        }
        else
        {
            format(string41, sizeof(string41), "%s places their fingers on the keypad", pName4);
            SendLocalMessage(playerid, string41, 15.0, COLOR_PURPLE, COLOR_PURPLE);
            PlayerActionMessage(playerid,15.0,"Incorrect Password");
        }
    }
}
Thankyou <3

Only one small problem now, when i use break; i get this error:
error 024: "break" or "continue" is out of context

Then I removed it and when I type the password to enter the house, it tells me incorrect password for both right and wrong answers.
Reply
#6

Yes, the break should not be there, I forgot to remove it.

Add a debug message and see what you get

pawn Код:
new x = GetClosestHouseID(playerid);
        format(string41, sizeof(string41), "Right code: %d   Entered code: %d", house[x][hLockCode], strval(inputtext));
        SendClientMessage(playerid, -1, string41);
        if(house[x][hLockCode] == strval(inputtext))
        {
Reply
#7

Thanks for all your help, this problem has been resolved. Rep for the both of you <3
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)