Page 1 of 1

m_clrRender calculation

Posted: Sat Jan 14, 2017 1:44 am
by Kami
Hey guys,

I'm trying to set the color of an entity with its m_clrRender property.

The problem is, m_clrRender is not a Color type but int which needs to be calculated in order to get the right RGB color.

I found this old thread on eventscripts http://forums.eventscripts.com/viewtopic.php?t=17774 but the ES calculation does not work for me when I use it in SourcePython. When I compile the C version by GODJonez it works perfectly and gives me right values.

The values of the C version are mostly negative (all of those I tried yet) and the values I get through SP are all positve and always way to big and result in "OverflowError: Python int too large to convert to C long".

This is the calculation I use:

Code: Select all

    r = random.randint(0,255)
    g = random.randint(0,255)
    b = random.randint(0,255)
    a = 255
    color = r + (g << 8) + (b << 16) + (a << 24)


Edit: I want to change the color of an env_spritetrail. Using the already built in functions for setting colors did work but left a black border around the sprite of my trail. with m_clrRender this does not happen! Just to clarify why I want to use m_clrRender :)

Re: m_clrRender calculation

Posted: Sat Jan 14, 2017 2:00 am
by satoon101
What entity are you trying to set the color of? The Entity.color property is the easiest way to get/set an entity's color.

Syntax: Select all

from colors import BLUE, RED
from events import Event
from players.entity import Player

player_colors = {
2: RED,
3: BLUE,
}

@Event('player_spawn')
def _change_player_color(game_event):
player = Player.from_userid(game_event['userid'])
if player.team in player_colors:
player.color = player_colors[player.team].with_alpha(128)


However, there are some instances where it doesn't work, though there are no working alternatives (ie weapon color in CS:GO for weapons being held by players).

Re: m_clrRender calculation

Posted: Sat Jan 14, 2017 2:03 am
by Kami
I just edited my post as I thought someone would point me to the existing color functions! :D

Re: m_clrRender calculation

Posted: Sat Jan 14, 2017 2:06 am
by satoon101
Well, you can always try to do what EventScripts' playerlib did:

Syntax: Select all

...
def getColor(self):
""" Returns a four-element tuple of the player's color in RGBA format. """
color = es.getplayerprop(self.userid, "CBaseEntity.m_clrRender")
return tuple(int(x) for x in (color & 0xff, (color & 0xff00) >> 8, (color & 0xff0000) >> 16, (color & 0xff000000) >> 24))

def setColor(self, r, g, b, a=None):
""" Sets the player's color with RGBA format. The alpha value can be omitted. """
color = int(r) + (int(g) << 8) + (int(b) << 16)
if a is None:
color += self.getColor()[3] << 24
else:
color += int(a) << 24
if color >= 2**31: color -= 2**32
es.setplayerprop(self.userid, "CBaseEntity.m_nRenderMode", es.getplayerprop(self.userid, "CBaseEntity.m_nRenderMode") | 1)
es.setplayerprop(self.userid, "CBaseEntity.m_nRenderFX", es.getplayerprop(self.userid, "CBaseEntity.m_nRenderFX") | 256)
es.setplayerprop(self.userid, "CBaseEntity.m_clrRender", color)


Primarily, I think you need this line:

Syntax: Select all

if color >= 2**31: color -= 2**32

Re: m_clrRender calculation

Posted: Sat Jan 14, 2017 2:08 am
by Kami
Yes, this line was exactly what I needed, thank you very much!