Page 1 of 1

getting index from playeriter

Posted: Mon Sep 29, 2014 1:22 am
by 8guawong

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 >_<

Posted: Mon Sep 29, 2014 11:13 am
by L'In20Cible
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.

Posted: Mon Sep 29, 2014 6:20 pm
by Ayuto
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.

Posted: Mon Sep 29, 2014 7:02 pm
by L'In20Cible
He means, when iterating over a PlayerIter object, player indexes are the default returned values unless otherwise specified on instantiation.

Posted: Mon Sep 29, 2014 7:04 pm
by Ayuto
Yeah, but since he marked it as red I thought he was confused by "indexing" and "index".

Posted: Mon Sep 29, 2014 7:08 pm
by L'In20Cible
Maybe, I guess xD

Posted: Mon Sep 29, 2014 7:38 pm
by satoon101
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.