When to create and cache a TempEntity?

Please post any questions about developing your plugin here. Please use the search function before posting!
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

When to create and cache a TempEntity?

Postby Mahi » Mon Dec 13, 2021 3:34 pm

Hi, how often should I create a new TempEntity instance?

Say I'm creating a skill for an RPG mod and want to draw a laser beam between two people to indicate extra damage. I see three options:
  1. Create a new TempEntity for every player_hurt event and only cache the models
  2. Create and cache one TempEntity for identical laser beams, i.e. each RPG skill would have its own effects and only the location of the beam would change
  3. Create and cache one TempEntity for each model, i.e. have a global cache of temp entities for models
Which one would be the best? The name TempEntity really sounds like I shouldn't cache it at all, but I see some other mods caching them either permanently or per-effect.
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: When to create and cache a TempEntity?

Postby L'In20Cible » Mon Dec 13, 2021 9:13 pm

Taking your example here you could preset all the following because it doesn't change:

Syntax: Select all

model = Model('sprites/laserbeam.vmt')
te = TempEntity('BeamRingPoint')
te.start_radius = 40
te.end_radius = 60
te.frame_rate = 100
te.life_time = 1
te.start_width = 10
te.end_width = 60
te.fade_length = 6
te.amplitude = 10
te.red = 255
te.green = 100
te.blue = 100
te.alpha = 255
te.speed = 100

@Event('player_jump')
def player_jump(ev):
te.model = model
te.halo = model
te.center = Player.from_userid(ev['userid']).origin
te.create()
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Re: When to create and cache a TempEntity?

Postby Mahi » Mon Dec 13, 2021 9:40 pm

And I can reuse this throughout the plugin's entire lifetime? Just making sure :grin:
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: When to create and cache a TempEntity?

Postby L'In20Cible » Mon Dec 13, 2021 9:44 pm

Mahi wrote:And I can reuse this throughout the plugin's entire lifetime? Just making sure :grin:

Yes. The engine uses a global instance and set all values every times it sends one, but the TempEntity class was designed with isolation in mind and make a copy of the global instance so that each instances have their own values that don't affect others from other plugins, etc.
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Re: When to create and cache a TempEntity?

Postby Mahi » Mon Dec 13, 2021 9:47 pm

Thank you for all the help L'In20Cible! :)

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 35 guests