Particles In Code: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
(expanded)
Line 7: Line 7:
<source lang=cpp>PrecacheParticleSystem( "your_particle_effect_name" );</source>
<source lang=cpp>PrecacheParticleSystem( "your_particle_effect_name" );</source>


== Dispatching ==
== Creating ==


Particles systems are "dispatched" on the client. They are always associated with an entity.
=== Serverside ===


{{tip|You can stop all systems tied to a given entity with <code>StopParticleEffects( CBaseEntity* pEntity )</code>. But you cannot otherwise directly control the lifespan of a system: it will choose when it terminates.}}
Particles are dispatched as [[events]]. The functions are shared, so there's no need to <code>#ifdef</code> calls out on the client.
 
=== Simple ===


<source lang=cpp>
<source lang=cpp>
#include "particle_parse.h"
#include "particle_parse.h"


void DispatchParticleEffect( const char *pszParticleName, Vector vecOrigin, QAngle vecAngles, CBaseEntity *pEntity )
// Create at world co-ords
void DispatchParticleEffect( const char *pszParticleName, ParticleAttachment_t iAttachType, CBaseEntity *pEntity,  
void DispatchParticleEffect( "my_particle", vecOrigin, angAngles, pOwnerEntity )
int iAttachmentPoint, bool bResetAllParticlesOnEntity  )</source>
// Create at model attachment
 
void DispatchParticleEffect( "my_particle", ParticleAttachment_t, pOwnerEntity, iAttachmentPoint, bResetAllParticlesOnEntity  )</source>
The first function creates the system at some arbitrary world co-ordinates. The second enables you to spawn it at an [[attachment]] point and/or make it follow the entity.


{{bug|Valve's code for dispatching a particle attached to a model is broken. [[SDK Known Issues List Fixed#Server Dispatching an Attached Particle Effect|Get the fix here.]]}}
{{bug|Valve's code for dispatching a particle on a model [[attachment]] is broken. [[SDK Known Issues List Fixed#Server Dispatching an Attached Particle Effect|Get the fix here.]]}}


Values for <code>ParticleAttachment_t</code> are:
Values of <code>ParticleAttachment_t</code> are:


; <code>PATTACH_ABSORIGIN</code>
; <code>PATTACH_ABSORIGIN</code>
Line 37: Line 34:
: Spawn at a given origin.
: Spawn at a given origin.
; <code>PATTACH_WORLDORIGIN</code>
; <code>PATTACH_WORLDORIGIN</code>
: Don't attach to an entity. Use with caution.
: Don't associate with an entity at all. Use with caution - could easily cause memory leaks.
 
=== Client ===


=== Complex ===
Each particle system is a [[temporary entity]]: only the client can interact with it once it has been dispatched. In addition, it isn't possible to specify [[Control Point (particles)|control points]] on the server at all.


If you need to control the system after it spawns, the helper functions above won't do. Instead:
Each active system is a <code>[[CNewParticleEffect]]</code> object that can be created and accessed through an entity's <code>[[ParticleProp()]]</code> accessor.


<source lang=cpp>
<source lang=cpp>
#include "particles_new.h"
#include "particles_new.h"


CNewParticleEffect* pEffect = ParticleProp()->Create( "rail_shot", PATTACH_ABSORIGIN );
// To create a new systen:
CNewParticleEffect* pEffect = ParticleProp()->Create( "my_particles", PATTACH_ABSORIGIN );
 
// Or if the system already exists:
CNewParticleEffect* pEffect = ParticleProp()->FindEffect( "my_particles" );


pEffect->SetControlPoint( 1, vecSomeVector );
pEffect->SetControlPoint( 1, vecSomeVector );
</source>
</source>


See also <code>[[CNewParticleEffect]]</code>.
Alternatively, you can hook into particle creation:
 
<source lang=cpp>void C_MyEntity::OnNewParticleEffect( const char* pszParticleName, CNewParticleEffect* pNewParticleEffect )
{
// ...
}</source>
 
== Destroying ==
 
You can stop particle systems with:
 
; <code>StopParticleEffects( CBaseEntity* )</code>
: Stops all effects on the given entity
; <code>ParticleProp()->StopParticlesInvolving( CBaseEntity* )</code>
: Stops all effects with a control point attached to the given entity.
; <code>ParticleProp()->StopParticlesNamed( const char* )</code>
: Stops all effects with the given system name. {{todo|Global or only on current entity?}}
; <code>[[CNewParticleEffect]]<nowiki>::</nowiki>StopEmission( bool bInfiniteOnly, bool bRemoveAllParticles, bool bWakeOnStop)</code>
: Stops the current particle system, without removing it. {{todo|Meaning of first and third arguments.}}


[[Category: Particle System]]
[[Category: Particle System]]

Revision as of 15:55, 25 October 2009

Precaching

Particle systems must be precached before they can be used. This is done by listing the PCF containing them in particles_manifest.txt, then making this call:

PrecacheParticleSystem( "your_particle_effect_name" );

Creating

Serverside

Particles are dispatched as events. The functions are shared, so there's no need to #ifdef calls out on the client.

#include "particle_parse.h"

// Create at world co-ords
void DispatchParticleEffect( "my_particle", vecOrigin, angAngles, pOwnerEntity )
// Create at model attachment
void DispatchParticleEffect( "my_particle", ParticleAttachment_t, pOwnerEntity, iAttachmentPoint, bResetAllParticlesOnEntity  )
Icon-Bug.pngBug:Valve's code for dispatching a particle on a model attachment is broken. Get the fix here.  [todo tested in ?]

Values of ParticleAttachment_t are:

PATTACH_ABSORIGIN
PATTACH_ABSORIGIN_FOLLOW
Spawn at (and optionally follow) the entity's origin.
PATTACH_POINT
PATTACH_POINT_FOLLOW
Spawn at (and optionally follow) an attachment point on the entity's model.
PATTACH_CUSTOMORIGIN
Spawn at a given origin.
PATTACH_WORLDORIGIN
Don't associate with an entity at all. Use with caution - could easily cause memory leaks.

Client

Each particle system is a temporary entity: only the client can interact with it once it has been dispatched. In addition, it isn't possible to specify control points on the server at all.

Each active system is a CNewParticleEffect object that can be created and accessed through an entity's ParticleProp() accessor.

#include "particles_new.h"

// To create a new systen:
CNewParticleEffect* pEffect = ParticleProp()->Create( "my_particles", PATTACH_ABSORIGIN );

// Or if the system already exists:
CNewParticleEffect* pEffect = ParticleProp()->FindEffect( "my_particles" );

pEffect->SetControlPoint( 1, vecSomeVector );

Alternatively, you can hook into particle creation:

void C_MyEntity::OnNewParticleEffect( const char* pszParticleName, CNewParticleEffect* pNewParticleEffect )
{
	// ...
}

Destroying

You can stop particle systems with:

StopParticleEffects( CBaseEntity* )
Stops all effects on the given entity
ParticleProp()->StopParticlesInvolving( CBaseEntity* )
Stops all effects with a control point attached to the given entity.
ParticleProp()->StopParticlesNamed( const char* )
Stops all effects with the given system name.
Todo: Global or only on current entity?
CNewParticleEffect::StopEmission( bool bInfiniteOnly, bool bRemoveAllParticles, bool bWakeOnStop)
Stops the current particle system, without removing it.
Todo: Meaning of first and third arguments.