S&box Wiki
Home
/
Edit Using particle systems from C#
View
Edit
History
No Category
Developer Overview
The Project System
Publishing To Asset Party
Getting Started With Hammer
Mapping Basics
Mapping Entities
Advanced Mapping Techniques
Getting Started with Modeldoc
Animgraph & Animation
Physics
Modeldoc Nodes
Advanced Modelling
UI Basics
Styles & Stylesheets
Razor Templates
Game Menus
Materials
Built In Shaders
Shaders
Shader Reference
Sounds & Audio
Particles
Getting Started
Making Games
Input
Networking
Physics
Rendering
Editor & Tools
VR
Misc
Playing Guides
Console Commands & Variables
Dedicated Server
Log in to edit
Using particle systems from C#
<cat>
Particle.Intro
</cat>
<title>
Using particle systems from C#
</title>
Once you have
[created a particle system](Creating_your_first_particle_effect)
in the particle editor there are many ways to create and interact with it in game from the C# API.
```csharp // Most basic way to create a particle system Particles MyParticle = Particles.Create( "particles/my_particle.vpcf" ); // Set control point 0 to a random world position, your particle system should use this control point to set it's position. MyParticle.SetPosition( 0, new Vector3( 100, 200, 100 ) ); ```
It is important to understand
[how control points work](Understanding_Emmiters_Initializers_&_Operators)
before creating particle systems in code, when you call Particles.SetPosition you are not directly setting the position of the system itself but rather the position of a control point that can then be used within the particle system itself.
#### Networking
Particle systems can be created both on the server and client - when created on the server they are automatically networked to all clients along with any of their control point values. When they are created on the client they are visible for that client only.
## Setting Named Values
```csharp // Use .Set for Named Values with the name you set inside the particle and assign a Float. MyParticles.Set( "Radius", 10.0f ); // This works the same as Float, but requires a Vector instead. MyParticles.Set( "MoveDirection", new Vector3( 0, 0, 100 ) ); ```
Inside the particle you can use
"Named Float or Named Vector"
instead of using Control Points, these act the same as Control Points but use a name instead of a number.
## Attaching a particle to an entity
When creating particles they can be attached to an entity.
```csharp Particles.Create("particles/my_particle.vpcf", MyEntity, "" ); ```
When you do this two things happen:
* The lifetime of this particle system is determined by the entity, when the entity is destroyed so is the particle system.
* Control point 0 is set to the entity (or its attachment) position, and will follow it.
<note>
Particle systems can be attached to an entity without an attachment by providing an empty string, this does return a warning though. https://github.com/Facepunch/sbox-issues/issues/894
</note>
## Destroying particle systems
Particle systems can be destroyed at any point in your code via a reference to the particle system.
```csharp var MyParticle = Particles.Create( "particles/my_particle.vpcf" ); // Destroys a particle system MyParticle.Destroy() // Destroys a particle system immediately MyParticle.Destroy( true ); ```
When you destroy a particle system non immediately, the following happens:
* The emitters within the system stop emitting.
* Particles within the system that have emitted began their lifespan decay.
* The end cap is played.
When destroyed immediately none of that happens, all particles within the system are immediately deleted.
<validate>
Does it wait for lifespan decay, need to validate
</validate>
# Prediction and particles
Particles can be predicted, this means when you're creating particles in a
[predicted method](Prediction)
such as Simulate it is expected to be called from both the server and client -
**if you fail to predict it properly it may not show up for your simulated client**
.
```csharp public override Simulate( IClient cl ) { // When the client presses their secondary attack binding, create our particle if ( Input.Pressed( InputButton.SecondaryAttack ) ) { // This will be predicted properly Particles.Create( "particles/test.vpcf", this, "" ); // This particle will not appear on the simulated Client, but will show for others. if ( Game.IsServer ) { Particles.Create( "particles/test.vpcf", this, "" ); } // This particle will appear on the client, but is not predicted. if ( Game.IsServer ) { using ( Prediction.Off() ) { Particles.Create( "particles/test.vpcf", this, "" ); } } } } ```
<cat>Particle.Intro</cat> <title>Using particle systems from C#</title> Once you have [created a particle system](Creating_your_first_particle_effect) in the particle editor there are many ways to create and interact with it in game from the C# API. ```csharp // Most basic way to create a particle system Particles MyParticle = Particles.Create( "particles/my_particle.vpcf" ); // Set control point 0 to a random world position, your particle system should use this control point to set it's position. MyParticle.SetPosition( 0, new Vector3( 100, 200, 100 ) ); ``` It is important to understand [how control points work](Understanding_Emmiters_Initializers_&_Operators) before creating particle systems in code, when you call Particles.SetPosition you are not directly setting the position of the system itself but rather the position of a control point that can then be used within the particle system itself. #### Networking Particle systems can be created both on the server and client - when created on the server they are automatically networked to all clients along with any of their control point values. When they are created on the client they are visible for that client only. ## Setting Named Values ```csharp // Use .Set for Named Values with the name you set inside the particle and assign a Float. MyParticles.Set( "Radius", 10.0f ); // This works the same as Float, but requires a Vector instead. MyParticles.Set( "MoveDirection", new Vector3( 0, 0, 100 ) ); ``` Inside the particle you can use "Named Float or Named Vector" instead of using Control Points, these act the same as Control Points but use a name instead of a number. ## Attaching a particle to an entity When creating particles they can be attached to an entity. ```csharp Particles.Create("particles/my_particle.vpcf", MyEntity, "" ); ``` When you do this two things happen: * The lifetime of this particle system is determined by the entity, when the entity is destroyed so is the particle system. * Control point 0 is set to the entity (or its attachment) position, and will follow it. <note>Particle systems can be attached to an entity without an attachment by providing an empty string, this does return a warning though. https://github.com/Facepunch/sbox-issues/issues/894</note> ## Destroying particle systems Particle systems can be destroyed at any point in your code via a reference to the particle system. ```csharp var MyParticle = Particles.Create( "particles/my_particle.vpcf" ); // Destroys a particle system MyParticle.Destroy() // Destroys a particle system immediately MyParticle.Destroy( true ); ``` When you destroy a particle system non immediately, the following happens: * The emitters within the system stop emitting. * Particles within the system that have emitted began their lifespan decay. * The end cap is played. When destroyed immediately none of that happens, all particles within the system are immediately deleted. <validate>Does it wait for lifespan decay, need to validate</validate> # Prediction and particles Particles can be predicted, this means when you're creating particles in a [predicted method](Prediction) such as Simulate it is expected to be called from both the server and client - **if you fail to predict it properly it may not show up for your simulated client**. ```csharp public override Simulate( IClient cl ) { // When the client presses their secondary attack binding, create our particle if ( Input.Pressed( InputButton.SecondaryAttack ) ) { // This will be predicted properly Particles.Create( "particles/test.vpcf", this, "" ); // This particle will not appear on the simulated Client, but will show for others. if ( Game.IsServer ) { Particles.Create( "particles/test.vpcf", this, "" ); } // This particle will appear on the client, but is not predicted. if ( Game.IsServer ) { using ( Prediction.Off() ) { Particles.Create( "particles/test.vpcf", this, "" ); } } } } ```
S&box Wiki
Development
Developer Overview
6
Editor Overview
General FAQ
System Requirements
The s&box wiki
Troubleshooting
Useful Links
The Project System
4
Adding Assets
Creating a Game Project
Project Settings Window - Games
Project Types
Publishing To Asset Party
2
Uploading assets
Uploading projects
Hammer
Getting Started With Hammer
3
Getting Started With Hammer
Making Your First Map
Mapping Resources
Mapping Basics
6
Cordons
Hotspot Materials
Selection Sets
Standard Mapping Dimensions
Tool Materials
Tools Visualisation Modes
Mapping Entities
2
Creating a Door
Light Entities
Advanced Mapping Techniques
8
Collaborating With Prefabs and Git
Instances
Prefabs
Quixel Bridge Plugin
Tilesets
Tilesets-Advanced
Tilesets-Proxies
VIS Optimizations
Models & Animation
Getting Started with Modeldoc
7
Automatic Model Setup
Breakpieces
Creating a Model
Guide to Models
Importing Rust Weapons
LODs
ModelDoc FAQ & best practices
Animgraph & Animation
4
Animations without Animgraph
AnimEvents, AnimGraph Tags, Attachments
Animgraph
Delta Animations
Physics
3
Cloth Physics
Collisions, Physics & Surface Types
Jiggle Bones
Modeldoc Nodes
1
Custom ModelDoc nodes
Advanced Modelling
6
Bodygroups
Citizen
First Person
IKChains and Stride Retargeting
Morphs
Vertex Normals
User Interface
UI Basics
6
Custom Fonts
Embedding Websites
Enabling Pointer Events
Events and Input
UI Basics
UI with Components
Styles & Stylesheets
1
Video Backgrounds
Razor Templates
4
A Razor Overview
Aliases and SetProperty Attributes
Generic Components
Templates
Game Menus
1
Making a Custom Pause Screen
Materials & Shaders
Materials
5
Guide to Materials
Material Attributes
Material Resources
Texture Settings
Using Dynamic Expressions
Built In Shaders
2
Foliage Shader
Glass Shader
Shaders
4
Compute Shaders
Constant Buffers
Material API
Shading Model
Shader Reference
5
Anatomy of Shader Files
Getting rid of Tex2D macros
Shader Reference
Shader States
Texture Format Cheat-Sheet
Other Assets
Sounds & Audio
4
Guide to Sounds
Sound Events
Sound Occlusion
Soundscapes
Particles
5
Creating animated sprites
Creating your first particle effect
Understanding Particle Editor
Using custom sprites
Using particle systems from C#
Coding
Getting Started
5
Cheat Sheet
Learning Resources
Setting up Rider
Setting up Visual Studio
Setting up Visual Studio Code
Making Games
2
Components
GameObjects
Input
4
Commands
ConVars
Input System
Speech Recognition
Networking
7
Auth Tokens
Http Requests
Lobby System
Networked Types
Networking Basics
RPCs
WebSockets
Physics
5
Collisions
Hitboxes
Joints
Traces
Triggers
Rendering
3
Render Tags
RenderHooks
Scenes
Editor & Tools
5
Guide to Widgets
Hammer API
Hammer Gizmos
Hotload Performance
Widget Docking
VR
3
Getting Started
VR Input
VR Overlays
Misc
10
Asset Types
Attributes and Component Properties
Backend API
Code Accesslist
CPU Performance Profiling
DisplayInfo
package/find
Setting Up A Navigation Mesh
Threaded Tasks
TypeLibrary
Playing
Playing Guides
3
Default Keybinds
Proton
s&box on macOS (Experimental)
Console Commands & Variables
1
Launch Arguments
Dedicated Server
1
Dedicated Servers