SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 02/01/2019, 05:41 PM   #1
CherryMond
Little Clucker
 
CherryMond's Avatar
 
Join Date: Feb 2018
Posts: 28
Reputation: 0
Default Get playerid from listitem.

Hi, I've problem with players in DIALOG_STYLE_LIST.

I show in dialog PlayerNames, code:
PHP Code:
new string[512], players[512], countPlayers;
            foreach(
Characteri){                
                if(
PlayerInQueue[i] == true){
                    
format(playerssizeof(players), "%s"GetPlayerName(i));
                    
strcat(string"\n");
                    
strcat(stringplayers);
                    
countPlayers++;
                }
             }
            if(
countPlayers == 0){
                
SendClientMessage(playerid, -1"No Players");
            }
            
ShowDialog(playerid34DIALOG_STYLE_LIST"Header"string"Select""Close"); 
Now, I need for example show player stats using function "ShowPlayerStats(playerid)" (this show dialog with stats).
Code:
if(dialogid == 34){
	if(response){
		//here I need get playerid from list
	}
}
How I get listitem == id player on list? Someone have solution?
CherryMond is offline   Reply With Quote
Old 02/01/2019, 05:50 PM   #2
NaS
High-roller
 
NaS's Avatar
 
Join Date: Mar 2008
Location: 🇩🇪
Posts: 1,785
Reputation: 635
Default Re: Get playerid from listitem.

There are multiple ways to solve this.

My personal favorite is formatting the ID of the player into the dialog, add a tab between the ID and player name and make it DIALOG_STYLE_TAB_LIST.

Something like this:

Quote:
0\tCherryMond
4\tNaS
7\tWhatever
The ID needs to be the first text in each row.

You might know that when a list dialog calls OnDialogResponse, the inputtext array will always be filled with the content of the row the player selected.

When it's a tab list dialog, only the first part of the row (until the first tab) will be passed. So when you select one of the above entries, inputtext will contain "0", "4" or "7".

So you only need to use strval or sscanf on inputtext to retrieve the ID. If you use strval, make sure to check for the length of the inputtext since there are hacks/cleo scripts to fake dialog data (which could crash the server in this case). For the same reason you should make sure the selected ID is valid etc.
It doesn't need any seperate storing and it's easy to do.

The only disadvantage is having the ID inside the dialog, however for player IDs this shouldn't be a problem.


Another way would be getting the Player ID from the Player Name (since the player name will be passed to inputtext if you don't add an ID).
NaS is offline   Reply With Quote
Old 02/01/2019, 06:12 PM   #3
CherryMond
Little Clucker
 
CherryMond's Avatar
 
Join Date: Feb 2018
Posts: 28
Reputation: 0
Default Re: Get playerid from listitem.

When I use sscanf - This should be like:
PHP Code:
if(response){
    new 
id;
    if(!
sscanf(inputtext"u"id)){
        
ShowPlayerStats(id);
        return 
true;
    }

or first tab inputtext[0]?
PHP Code:
if(response){
    new 
id;
    if(!
sscanf(inputtext[0], "u"id)){
        
ShowPlayerStats(id);
        return 
true;
    }

CherryMond is offline   Reply With Quote
Old 03/01/2019, 03:34 AM   #4
RoboN1X
Huge Clucker
 
RoboN1X's Avatar
 
Join Date: Feb 2011
Location: Indonesia
Posts: 453
Reputation: 145
Default Re: Get playerid from listitem.

When you use the dialog tab list like NaS suggested, you only need to convert the inputtext using strval
Code:
if (response) {
	new id = strval(inputtext);
	if (IsPlayerConnected(id)) {
		ShowPlayerStats(id);
		return 1;
	} else {
		// selected player is no longer online
	}
}
And your dialog tab list is formatted like this:
Code:
format(players, sizeof (players), "%i\t%s", i, GetPlayerName(i));
inputtext will only return the first column of the tab list.

But if you wish to stay with your own code (dialog list of player names only), you do this:
Code:
if (response) {
	new id;
	if (!sscanf(inputtext, "u", id)) {
		if (id != INVALID_PLAYER_ID)
		{
			ShowPlayerStats(id);
			return true;
		} else {
			// selected player is no longer online
		}
	}
}
if you only do inputtext[0], it will only take the first digit... so ID "12" will be only "1"

Note it is important to handle if the regarding player is still connected or not, because the players from the list might have been left the server OR changed name before the player response the dialog.
__________________
Need Help? Use Search, WIKI, and FAQ
RoboN1X is offline   Reply With Quote
Old 03/01/2019, 08:40 AM   #5
Kane
Gangsta
 
Kane's Avatar
 
Join Date: Sep 2012
Posts: 822
Reputation: 130
Default Re: Get playerid from listitem.

If it's ever not conveniently formatted to use with sscanf, you could always do something like:

pawn Code:
new tmpstr[80];

format(tmpstr, sizeof(tmpstr), "PLIST_%i_USERID", countPlayers);
SetPVarInt(playerid, tmpstr, get_players_id);

That would be inside your iteration. How do you use it?

pawn Code:
new tmpstr[80], get_player_id;

format(tmpstr, sizeof(tmpstr), "PLIST_%i_USERID", listitem);
get_player_id = GetPVarInt(playerid, tmpstr);

Inside your OnDialogResponse. Delete it after.

We're using the listitem the player would be at in OnDialogResponse and setting their ID through the PVar.

listitems start at 0 and as does your countPlayers variable.

It's pretty simple and self explanatory but if you don't understand, let me know and I'll explain further.
__________________
Los Santos Roleplay - Developer
@ls-rp.com

Former Beta Tester

Kane is online now   Reply With Quote
Old 03/01/2019, 08:27 PM   #6
CherryMond
Little Clucker
 
CherryMond's Avatar
 
Join Date: Feb 2018
Posts: 28
Reputation: 0
Default Re: Get playerid from listitem.

Thank You, could you explain in more detail?
CherryMond is offline   Reply With Quote
Old 03/01/2019, 10:33 PM   #7
BlackBank
Gangsta
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 522
Reputation: 437
Default Re: Get playerid from listitem.

Quote:
Originally Posted by Kane_ View Post
If it's ever not conveniently formatted to use with sscanf, you could always do something like:

pawn Code:
new tmpstr[80];

format(tmpstr, sizeof(tmpstr), "PLIST_%i_USERID", countPlayers);
SetPVarInt(playerid, tmpstr, get_players_id);

That would be inside your iteration. How do you use it?

pawn Code:
new tmpstr[80], get_player_id;

format(tmpstr, sizeof(tmpstr), "PLIST_%i_USERID", listitem);
get_player_id = GetPVarInt(playerid, tmpstr);

Inside your OnDialogResponse. Delete it after.

We're using the listitem the player would be at in OnDialogResponse and setting their ID through the PVar.

listitems start at 0 and as does your countPlayers variable.

It's pretty simple and self explanatory but if you don't understand, let me know and I'll explain further.
Why using PVar for this? Because they are slow, and especially when you us them with the format function. You could just use an array and store the ID's of the players in it...

I should do it like this:
PHP Code:
// Somewhere above your script (global variable), not inside any functions:
new dialogPlayerStatsList[MAX_PLAYERS][MAX_PLAYERS];

// Inside your foreach loop where you generate the list of players:
foreach(Characteri){
    if(
PlayerInQueue[i] == true){
        
format(playerssizeof(players), "%s"GetPlayerName(i));
        
strcat(string"\n");
        
strcat(stringplayers);
        
        
dialogPlayerStatsList[playerid][countPlayers] = i;

        
countPlayers++;
    }
}

// OnDialogResponse:
if(dialogid == 34){
    if(
response){
        
// Selected playerid:
        
new selected_playerid dialogPlayerStatsList[playerid][listitem];

        
GivePlayerMoney(selected_playerid1337); // Give selected player from list money (example)
    
}

BlackBank is offline   Reply With Quote
Old 03/01/2019, 11:04 PM   #8
cessil
High-roller
 
cessil's Avatar
 
Join Date: Apr 2009
Posts: 1,346
Reputation: 296
Default Re: Get playerid from listitem.

when you say PVars are slow you're talking tens of thousands of a millisecond like 0.00023 ms according to some speed tests, meaning you'd have to use over ten thousand in the one function to notice any slow down
cessil is online now   Reply With Quote
Old 03/01/2019, 11:11 PM   #9
BlackBank
Gangsta
 
Join Date: Dec 2010
Location: The Netherlands
Posts: 522
Reputation: 437
Default Re: Get playerid from listitem.

Quote:
Originally Posted by cessil View Post
when you say PVars are slow you're talking tens of thousands of a millisecond like 0.00023 ms according to some speed tests, meaning you'd have to use over ten thousand in the one function to notice any slow down
True, but using it together with format is slow.
BlackBank is offline   Reply With Quote
Old 04/01/2019, 05:58 AM   #10
Threshold
High-roller
 
Threshold's Avatar
 
Join Date: Nov 2012
Location: Australia
Posts: 3,122
Reputation: 529
Default Re: Get playerid from listitem.

Just remember, inputtext can be altered by players. So make sure you treat it as an unsanitised input.
__________________


Kar's Cops and Robbers 2020
Threshold is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GivePlayerMoney(playerid, PlayerInfo[playerid][pNovac]); Problem Theepiccoder Scripting Help 6 03/07/2016 02:13 PM
[Ajuda] listitem Comanfis157 PortuguÍs/Portuguese 0 12/04/2014 09:17 PM
double public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]) TiXz0r Scripting Help 1 07/11/2013 08:12 PM
What's wrong with skins? GetPlayerSkin(playerid) & SetPlayerSkin(playerid, skin) McSquizzy Help Archive 7 16/04/2010 02:27 PM


All times are GMT. The time now is 05:51 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.