Detect House ID from Dialog[How to improve code optimization]
#1

Hi guys

I add house keys for player on an iterator.
new Iterator: HouseKey[MAX_PLAYERRS]<MAX_HOUSES>;

When player type /house he can see list of his house and manage them.

Код:
 	new str[256],id = 1;
	foreach(new i: HouseKey[playerid])
	{
	    format(str,sizeof(str),"%s[%d]\n",str,id);
		id++;
	}
	ShowPlayerDialog(playerid,DIALOG_HOUSE_LIST,DIALOG_STYLE_LIST,"Select House",str,"OK","");
Now I need to detect which house id is he click.But I don't know how to do it.I'm do it on this way but i'm sure that it's wrong.(Yes it work,but it's bad code,i guess)

Код:
		case DIALOG_HOUSE_LIST:
		{
		    if(response == 1)
		    {
		        new skip=0;
		        foreach(new i: HouseKey[playerid])
		        {
		            if(skip == listitem)
		            {
						printf("Player is click on ID : %i",i);
        				break;
					}
					else
					{
					    skip++;
					}
				}
			}
		}
So I need advice how to improve my code optimization.
Reply
#2

This should works
pawn Код:
case DIALOG_HOUSE_LIST:
{
    if(response == 1 && inputtext[0])
    {
        printf("Player is click on ID : %i",strval(inputtext[1]));
    }
}
Reply
#3

Quote:
Originally Posted by GospodinX
Посмотреть сообщение
Hi guys

I add house keys for player on an iterator.
new Iterator: HouseKey[MAX_PLAYERRS]<MAX_HOUSES>;

When player type /house he can see list of his house and manage them.

Код:
 	new str[256],id = 1;
	foreach(new i: HouseKey[playerid])
	{
	    format(str,sizeof(str),"%s[%d]\n",str,id);
		id++;
	}
	ShowPlayerDialog(playerid,DIALOG_HOUSE_LIST,DIALOG_STYLE_LIST,"Select House",str,"OK","");
Now I need to detect which house id is he click.But I don't know how to do it.I'm do it on this way but i'm sure that it's wrong.(Yes it work,but it's bad code,i guess)

Код:
		case DIALOG_HOUSE_LIST:
		{
		    if(response == 1)
		    {
		        new skip=0;
		        foreach(new i: HouseKey[playerid])
		        {
		            if(skip == listitem)
		            {
						printf("Player is click on ID : %i",i);
        				break;
					}
					else
					{
					    skip++;
					}
				}
			}
		}
So I need advice how to improve my code optimization.
Do you want to detect which menu option selects the player or which house (houseid) is it in?
Reply
#4

Код:
new str[256], buff[10], id = 1, c = 0;
foreach(new i: HouseKey[playerid])
{
	format(buff, sizeof(buff), "Key_%d", c);
	SetPVarInt(playerid, buff, id);
	
	format(str,sizeof(str),"%s[%d]\n",str,id);
	id++;
	c++;
}
ShowPlayerDialog(playerid,DIALOG_HOUSE_LIST,DIALOG_STYLE_LIST,"Select House",str,"OK","");
Код:
case DIALOG_HOUSE_LIST:
{
	if(response == 1)
	{
		new buff[10], id;
		format(buff, sizeof(buff), "Key_%d", listitem);
		id = GetPVarInt(playerid, buff);
		printf("Player is click on ID : %i", id);
	}
}
Reply
#5

A house can only be owned by 1 player, so use latest version of y_iterate (of YSI library) and change to:
pawn Код:
new Iterator: HouseKey<MAX_PLAYERS, MAX_HOUSES>;
The per-player iterator will be storing the houses and I do not understand why every post above pass "id" in format. This will start from 1 every time and for every player. Use "i" instead to retrieve the houseid.

As for strval, the text contains brackets so will return 0. You can get rid of them and make it easier to retrieve the value without the need of another foreach loop.
Reply
#6

Quote:

Do you want to detect which menu option selects the player or which house (houseid) is it in?

Second(detect house id)
@Calisthenics : Thank you,I will use it format.
@sampkinq : Yeah.Thank you,that should work.But i'm also not sure that it's good way.Because i have many system where i want to use this: vehicle,house,flats,garage etc..So I will need for every add new pvar...
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)