getting index from playeriter

Please post any questions about developing your plugin here. Please use the search function before posting!
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

getting index from playeriter

Postby 8guawong » Mon Sep 29, 2014 1:22 am

Syntax: Select all

random.choice(playerlib.getUseridList('#bot'))


here is what i use in ES to get a random userid

here is what i tried in SP but don't work @@
doesn't print the index

Syntax: Select all

from events import Event
from players.entity import PlayerEntity
from filters.players import PlayerIter
@Event
def player_say(game_event):
new_t = random.choice(PlayerIter('ct'))
print(new_t)


Code: Select all

[SP] Caught an Exception:
Traceback (most recent call last):
  File '../addons/source-python/packages/source-python/events/listener.py', line 90, in fire_game_event
    callback(game_event)
  File '../addons/source-python/plugins/test/test.py', line 26, in player_say
    new_t = random.choice(PlayerIter('ct'))
  File '../addons/source-python/Python3/random.py', line 256, in choice
    return seq[i]

TypeError: 'PlayerIter' object does not support indexing


from wiki

Code: Select all

from filters.players import PlayerIter
# Usage
#   for <variable or variables> in PlayerIter([str/list is_filters], [str/list not_filters], [str/list return_types]):
 
# Since "index" is the default return type, loop over all player indexes
for index in PlayerIter():
    pass


# Since "index" is the default return type

------------------- edit -------------------------------
after searching some more maybe i need to do this

Syntax: Select all

new_t = random.choice(list(PlayerIter('ct')))


away from my comp now can't test >_<
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Mon Sep 29, 2014 11:13 am

Yes, casting the object to a list will works since it will iterate over the generator to build its sequence. Since we recently added __len__, I guess we could add indexing/slicing to the _IterObject class but this means it will iterate over the entire generator every time. A good practice will still remains to cast it as a sequence (list, tuple, set...) and store the result so you actually iterate once at this time even if you have ton of work to do on the filtered items.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Mon Sep 29, 2014 6:20 pm

8guawong wrote:# Since "index" is the default return type

I would also like to add that "indexing" has nothing to do with the return type. See this example:

Syntax: Select all

import random

print(random.choice(set(range(5))))
It's completely unrelated to Source.Python, but it will raise the same error.

Indexing just means accessing values of an iterable via indexes.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Mon Sep 29, 2014 7:02 pm

He means, when iterating over a PlayerIter object, player indexes are the default returned values unless otherwise specified on instantiation.
User avatar
Ayuto
Project Leader
Posts: 2195
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Mon Sep 29, 2014 7:04 pm

Yeah, but since he marked it as red I thought he was confused by "indexing" and "index".
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Mon Sep 29, 2014 7:08 pm

Maybe, I guess xD
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Sep 29, 2014 7:38 pm

I think Ayuto is probably right. The error that you receive is because random.choice requires an indexible object, like a list. And indexing has nothing to do with player/entity indexes, which are what the default return types return for most of the classes that inherit from _IterObject.
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 4 guests