[CS:S] how to set team score properly?

Please post any questions about developing your plugin here. Please use the search function before posting!
derkalle
Junior Member
Posts: 9
Joined: Sat Nov 26, 2022 4:49 pm

[CS:S] how to set team score properly?

Postby derkalle » Mon Dec 05, 2022 5:10 pm

Hi all,

found a problem while setting the team score. It seems to work but it will reset after the end of the next round. I wanted to have this to be able to switch teams after 50% of the rounds are over.

This is what I came up with

Code: Select all

    def set_team_score(self, team, score):
        for entity in EntityIter('cs_team_manager'):
            if entity.team != team:
                continue
            # set score
            entity.score = score
            # set rounds won
            entity.rounds_won = score


Thought that setting "rounds_won" additionally would fix the behaviour, but it doesn't. Sourcemod seems to have a function to set the team score without resetting it afterwards. Haven't found any clue on this forums nor in the wiki. Which doesn't mean I have found everything I could have :)

According to the definition I have found the above source code sets

Code: Select all

[score]
prop = "CTeam.m_iScore"
type = "int"

[rounds_won]
prop = "CTeam.m_RoundsWon"
type = "int"


I don't know whether this is the correct place at all to actually make the change or if it is only for reading the data.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: [CS:S] how to set team score properly?

Postby satoon101 » Mon Dec 05, 2022 5:56 pm

Yes, those properties are for retrieving and setting data. What I did with GG-Scoreboard was stored the values in a dictionary and set the team scores on both round_start and round_end.

https://github.com/satoon101/GunGame-Sc ... py#L68-L79
Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: [CS:S] how to set team score properly?

Postby L'In20Cible » Mon Dec 05, 2022 7:10 pm

The scores themselves are stored into the game rules, not the team managers. The later are only used as reflection for client-side scoreboard. The easiest way, without using hard-coded offsets, would be to use a game_score entity:

Syntax: Select all

from entities.dictionary import SyncedEntityDictionary
from entities.entity import Entity
from filters.entities import EntityIter

players = SyncedEntityDictionary(iterator=EntityIter('player'))
managers = SyncedEntityDictionary(iterator=EntityIter('cs_team_manager'))

def set_team_score(team, score):
# Get a random player to use as activator
player = next(iter(players.values()), None)
if player is None:
return

# Find the manager for the given team
for team_manager in managers.values():
if team_manager.team == team:
break
else:
raise ValueError(f'No manager found for team {team}.')

# No need to go further if the score is already set to the desired value
current_score = team_manager.score
if current_score == score:
return

# Create a game_score entity to set the score
game_score = Entity.create('game_score')
game_score.spawn_flags |= 0x0001 | 0x0002
game_score.set_key_value_int('points', score - current_score)

# Save the current team of the player so we can restore it
original_team = player.team_index
player.team_index = team

# Apply the desired score
game_score.call_input('ApplyScore', activator=player)

# Restore the original team of the player
player.team_index = original_team

# Remove the game_score entity we created
game_score.remove()
derkalle
Junior Member
Posts: 9
Joined: Sat Nov 26, 2022 4:49 pm

Re: [CS:S] how to set team score properly?

Postby derkalle » Mon Dec 05, 2022 9:35 pm

Many thanks! :) learning everyday about Sourcepython and I really like the possibilites!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 37 guests