Creating Sprite does not work consistently

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Pudge90
Member
Posts: 33
Joined: Sun Jan 14, 2018 6:18 pm
Location: Germany

Creating Sprite does not work consistently

Postby Pudge90 » Wed Jan 24, 2018 9:56 am

So I am trying to create a Sprite in CS:S. This is my attempt:



Syntax: Select all

from filters.players import PlayerIter
from mathlib import Vector
from engines.precache import Model
from players.entity import Player
from players.helpers import index_from_userid


def find_closest_enemy_player(player, radius):
alive_players = PlayerIter('alive')
cur_distance = 9999
i=0
for target in alive_players:
if target.team != player.team:
distance = player.origin.get_distance(target.origin)
SayText2('The distance: %s' %distance).send(player.index)
if distance <= radius:
if distance < cur_distance:
cur_distance = distance
return_target = target
i=1
if i != 0:
return return_target
else:
return 'error'




@Event('player_say')
def chat_based_trigger(game_event)
player = Player(index_from_userid(game_event['userid']))
target = find_closest_enemy_player(player, radius =9000)
if target != 'error':
laser_model = Model('sprites/lgtning.vmt')
effect1 = tempEntity('BeamPoints', alpha = 255, red =100, blue = 25, life_time =10.0, start_width = 10, end_width = 10, frame_rate =
255)
effect1.start_point = player.origin
effect1.end_point = target.origin
effect1.model = laser_model
effect1.create()


Sometimes this work, sometimes it doesnt. That means, if I move around a idle bot und try so 'draw' a beam from my position to his, sometimes it gets drawn and sometimes not. I have no idea why. The distance does not seem to be the problem since it sometime works over 2000 Units and some times it dows not work over 100 Units. Maybe I did a very stupid mistake, I don't know.
Also the find_closest_enemy_player(player, radius) works as supposed, it finds the target and never runs into the 'error' in my scenario.
User avatar
Ayuto
Project Leader
Posts: 2193
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Re: Creating Sprite does not work consistently

Postby Ayuto » Wed Jan 24, 2018 8:01 pm

First of all, if you want help with your code, please post the code you have actually used/tested. The code you posted contains syntax errors and name errors. So, this cannot be the code you have tested.

Secondly, I took the time to fix the errors and improved your function to find the closest enemy. Your implementation looks a little bit scary, tbh. :grin:
Also, you shouldn't return 'error' to indicate an error. Better return None or even better raise an exception.

Syntax: Select all

from filters.players import PlayerIter
from engines.precache import Model
from players.entity import Player
from effects import TempEntity
from events import Event

laser_model = Model('sprites/laser.vmt')

def get_player_distances(player, radius):
for enemy in PlayerIter('alive'):
if enemy.team == player.team:
# Ignore players of the same team
continue

distance = player.origin.get_distance(enemy.origin)
if distance > radius:
# Ignore players outside of the radius
continue

yield (distance, enemy)

def find_closest_enemy_player(player, radius):
try:
distance, enemy = min(get_player_distances(player, radius))
except ValueError:
# min() will raise ValueError if it's called on an empty sequence.
# Return None instead.
return None

return enemy

@Event('player_say')
def chat_based_trigger(game_event):
player = Player.from_userid(game_event['userid'])
target = find_closest_enemy_player(player, radius=9000)
if target is None:
# No need to do anything if no enemy was found
print('No player found') # Debugging
return

effect1 = TempEntity(
'BeamPoints',
alpha=255,
red=255,
blue=0,
green=0,
life_time=3,
fade_length=1,
start_width=10,
end_width=10,
frame_rate=255,
start_point=player.origin,
end_point=target.origin,
model=laser_model,
amplitude=1,
flags=0,
speed=1,
halo=laser_model,
start_frame=0)

effect1.create()
print('Beam to', target.name) # Debugging

Back to the actual problem:
I'm able to reproduce the issue on CS:S/Win/de_dust2. It seems like it depends on the position where you and the enemy is currently standing. I remember that I already had that problem once, but unfortunately I can't remember the cause/fix.
User avatar
Pudge90
Member
Posts: 33
Joined: Sun Jan 14, 2018 6:18 pm
Location: Germany

Re: Creating Sprite does not work consistently

Postby Pudge90 » Wed Jan 24, 2018 10:16 pm

Sorry, next time I check the code before I'll post it. Thank you for putting so much effort into answering my questions.
Since you were able to reproduce the issue it might be a CS:S-issue ?! I'll try the code in CS:GO and will share my experience.
In case you remember the cause/fix, please let me know :grin:
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Creating Sprite does not work consistently

Postby satoon101 » Wed Jan 24, 2018 11:06 pm

Pudge90 wrote:*Edit: No sprites do not work at all in CS:GO, atleast not with the code provided in this post

Just a note, but sprites/laser.vmt does not exist on CS:GO, so that won't work. However, you should be able to use sprites/laserbeam.vmt.
Image
User avatar
Pudge90
Member
Posts: 33
Joined: Sun Jan 14, 2018 6:18 pm
Location: Germany

Re: Creating Sprite does not work consistently

Postby Pudge90 » Wed Jan 24, 2018 11:18 pm

Ohh, you're right satoon101. Anyways, I tried it wih /sprites/laserbeam.vmt and it creates the same issue in CSGO as in CS:S

*Update*
Neither in CS:S nor in CS:GO it is possible for me to create sprites consitently. I tried like 50 scenarios (diffrent maps, diffrent positions, beam-parameters,...). If a sprite is created seems to indeed depend, as you said Ayuto, on where the player.origin and the target.origin is. I tried a lot of positions on the 'breakflor_v1'-map with an even ground. Almost every position is working except 2 to 3 positions that seem to be at a fixed place (probably because I set the set_frozen(True) for every bot at spawn, so the target.origin does not change). In those positions I can never create a visible sprite, even if I set the end_point to the exact coordiantes of the specific 'dead' position (so hardcoding the coordinates and not getting them from the find_closest_enemy_player(player, radius) ).
Does anyone have an idea why this happens only at certain 'dead' positions/coordinates ?
This is driving me nuts! :confused: :grin:

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 21 guests