Page 1 of 2

Auto spawn

Posted: Sun Mar 29, 2015 1:40 pm
by nullable
Hi,

Please help me. My code doesn't set a team.

Syntax: Select all

from players.helpers import index_from_userid
from players.entity import PlayerEntity
from entities.helpers import spawn_entity
from events import Event

@Event
def player_activate(game_event):
userid = game_event.get_int('userid')
index = index_from_userid(userid)
player = PlayerEntity(index)
player.team = 3
spawn_entity(player.index)

Posted: Sun Mar 29, 2015 2:51 pm
by L'In20Cible
The real problem here is that you are only defining the m_nTeamNum property of the player. You need to tell the specific team manager by adding the player to it. I think I would try a different approach to achieve what you are trying to do:

Syntax: Select all

from commands import CommandReturn
from commands.client import ClientCommand
from engines.server import engine_server
from entities.helpers import spawn_entity
from players.constants import LifeState
from players.entity import PlayerEntity
from players.helpers import edict_from_playerinfo
from players.helpers import index_from_playerinfo

@ClientCommand('joingame')
def joingame_callback(player_info, command):
"""The 'joingame' command is executed by the clients when they click on
the [OK] button of the MOTD."""

# Simulate the player choosing a team...
engine_server.client_command(
edict_from_playerinfo(player_info), 'jointeam 3')

# We don't want the server to send the team menu so stop the process...
return CommandReturn.BLOCK


@ClientCommand('joinclass')
def joinclass_callback(player_info, command):
"""The player is selecting a class from the class menu."""

# Get the PlayerEntity instance from the PlayerInfo...
player = PlayerEntity(index_from_playerinfo(player_info))

# Make sure the player is in the connection process...
if not player.player_state:
return CommandReturn.CONTINUE

# Mark the player as respawnable...
player.player_state = 0
player.life_state = LifeState.RESPAWNABLE

# Spawn the player...
spawn_entity(player.index)

# Make sure the player won't spawn twice in case he join the server few
# seconds after the round starts...
return CommandReturn.BLOCK

Posted: Sun Mar 29, 2015 4:55 pm
by satoon101
L'In20Cible wrote:The real problem here is that you are only defining the m_nTeamNum property of the player.
That is not accurate. PlayerEntity has a defined property for team to get and set a player's team.
https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/addons/source-python/packages/source-python/players/entity.py#L120

Posted: Sun Mar 29, 2015 5:21 pm
by nullable
satoon101 wrote:
L'In20Cible wrote:The real problem here is that you are only defining the m_nTeamNum property of the player.
That is not accurate. PlayerEntity has a defined property for team to get and set a player's team.
https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/addons/source-python/packages/source-python/players/entity.py#L120


Yes, but my code stopped working properly after the upgrade source-python (8 march 2015 release).

Posted: Mon Mar 30, 2015 1:43 am
by L'In20Cible
satoon101 wrote:That is not accurate. PlayerEntity has a defined property for team to get and set a player's team.
https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/addons/source-python/packages/source-python/players/entity.py#L120
It is, since "team" is also declared as a keyvalue which point to CBaseEntity::ChangeTeam. Changing the team of a player requires more work than an entity and that keyvalue is found first in the resolution order.

Posted: Mon Mar 30, 2015 4:23 am
by nullable
I tried change my code next:

Syntax: Select all

from players.helpers import index_from_userid
from players.entity import PlayerEntity
from players.constants import LifeState
from entities.helpers import spawn_entity
from events import Event

@Event
def player_activate(game_event):
userid = game_event.get_int('userid')
index = index_from_userid(userid)
player = PlayerEntity(index)
# Mark the player as respawnable...
player.player_state = 0
player.life_state = LifeState.RESPAWNABLE
player.team = 3
spawn_entity(player.index)


But it didn't help me. Please tell me is what wrong?

P.S. The game is CSGO

Posted: Mon Mar 30, 2015 8:28 pm
by nullable
Is any ideas how can I change team without callbacks?

Posted: Tue Mar 31, 2015 2:28 am
by L'In20Cible

Syntax: Select all

player.playerinfo.change_team(3)
I still think the proper approach, for the clients, is to wait they accept the MOTD first.

Posted: Wed Apr 01, 2015 5:49 am
by nullable
Thanks. The problem has been resolved.

Posted: Wed Apr 01, 2015 1:52 pm
by satoon101

Posted: Mon Jun 29, 2015 6:53 pm
by nullable
The problem is that all the players die if I run this code in round_end event:
[PYTHON]def switch_teams():
for player in PlayerIter(return_types='player'):
if player.name in TV_BOTS:
continue
team_id = Team.CT.value if player.team == Team.TF.value else Team.TF.value
player.playerinfo.change_team(team_id)[/PYTHON]

Is there a way to solve the problem is not killing all the players? I would like to make a similar change in the period when mp_halftme 1.

Posted: Mon Jun 29, 2015 7:03 pm
by satoon101
Use switch_team instead.

[python] player.switch_team(team_id)[/python]
This method is added dynamically through the data system:

CS:GO's CCSPlayer::SwitchTeam
CS:S' CCSPlayer::SwitchTeam


Also, there is no need to use player.playerinfo directly. We handle that automatically in __getattr__:

https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/addons/source-python/packages/source-python/entities/entity.py#L74

The above line loops through the base instances for the entity/player and gets whether the instance has the given attribute. For PlayerEntity, the playerinfo is the first instance that is checked against:

https://github.com/Source-Python-Dev-Team/Source.Python/blob/master/addons/source-python/packages/source-python/players/entity.py#L66

Posted: Mon Jun 29, 2015 7:16 pm
by nullable
I have an error when I use player.switch_team

Syntax: Select all

[SP] Caught an Exception:    
Traceback (most recent call last):
File '../addons/source-python/packages/source-python/events/listener.py', line 93, in fire_game_event
callback(game_event)
File '../addons/source-python/plugins/test/test.py', line 262, in round_end
switch_teams()
File '../addons/source-python/plugins/test/test.py', line 145, in switch_teams
player.switch_team(team_id)
File '../addons/source-python/packages/source-python/entities/entity.py', line 87, in __getattr__
if hasattr(server_class, attr):
File '../addons/source-python/packages/source-python/memory/manager.py', line 610, in __get__
binary = find_binary(cls._binary, cls._srv_check)

OSError: Unable to find ../bin/server_srv.so

Posted: Tue Jun 30, 2015 4:07 am
by satoon101
I just had time to work on that error, and it is now fixed:
https://github.com/Source-Python-Dev-Team/Source.Python/commit/4e716a963aac732db3628d1bd05097dc61f0c425

We will get a new release posted hopefully sometime this week.

Posted: Tue Jun 30, 2015 4:50 am
by nullable
OFFTOP: Will it be possible to change the nickname like player.name to the new release?

Posted: Tue Jun 30, 2015 4:55 am
by satoon101
I personally don't think that would be a good idea to add directly into the API for multiple reasons. It's fine to use in your own plugin(s), though.

Posted: Tue Jun 30, 2015 6:39 am
by necavi
I'd be curious as to what those reasons are, especially when there's completely different ways of changing the name in CS:S and CS:GO, seems better to support that all at one point rather than making everyone have their own method of doing it. (Unless there's already a method on the player to do it that I'm forgetting and you're just referring to setting the variable)

Posted: Tue Jun 30, 2015 11:01 am
by Ayuto
Yeah, I also think that we should include that. That would prevent people from copying and pasting the script I have made into every plugin.

Posted: Tue Jun 30, 2015 1:16 pm
by satoon101
I don't really like the idea of changing a players name without their permission, and I am sure I am not alone in that. This functionality will require multiple symbols/signatures that need to be maintained. Not to mention, if a player did not like the fact that a server changed their name, they would either blacklist the server or have a key bound to reset their name. I know that the resetting can be prevented, and I wonder if either of you would advocate implementing that into the API. That would seem to be a huge overstepping to me.

Having said all of that, I think this would be a perfect candidate for a custom package. That way, any plugin that wishes to be able to change a players name can just require the custom package and utilize its functionality. I know that people would want to use this functionality. I just don't think it belongs in the API itself.

Posted: Tue Jun 30, 2015 5:22 pm
by Ayuto
One could argue that people also don't want that servers change their clan tags, health or speed. But that's one of the basic features of SP.

Actually, we just need the signature/symbol of the SetName method and the sv pointer. The offset for ClientCommand will be available through the new memory branch and GetClient can be exposed.