S&box Wiki
Home
/
Edit Using Dynamic Expressions
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 Dynamic Expressions
<cat>Material.Intro</cat> <title>Using Dynamic Expressions</title> # The Basics So Dynamic Expressions are a way to have extra control on a material while the game is active. This is the exact same as so... https://developer.valvesoftware.com/wiki/Dota_2_Workshop_Tools/Materials/Dynamic_Material_Expressions However the main thing people will probably want to be able to do is to use these to edit their material parameters via code. and its extremely simple. # Setting up So first things first we need to set up our material. Simple [We know how to do that!](guide_to_materials), Now its all set up and we have the part of the material we want to be able to control in the code. ![Material Made](https://i.imgur.com/pO1wTRB.png) in this case i want to control the self illum tint and strength via code on the fly. so first things first, on the property we want to control, locate the little drop down arrow to the right like so and click it. ![Drop Down](https://i.imgur.com/VWoAIWf.png) And Now Click the `Add Dynamic Expression...` option, and it should show you a new window. ![Dynamic Expression Window](https://i.imgur.com/hNkYtlR.png) Now in here we can do whatever expressions we want (look to the top of the page to the valvesoftware link to understand more... HOWEVER! we want to be able to set it so we can use it in the code... That is very simple to do so. we can litteraly just put whatever we want to property name to access it as in here and its ready to go! In my case for the light color i will just set it as `lightcolor` and the self illum scale as `lightstrength` ![Expression set](https://i.imgur.com/aGzu64p.png) now we can save this window and save our material, and be able to head over to the code. # Editing via code Ok now we can head over to our games, and get to the coding. in this example i will create a custom entity for the lamp to show it off. so first i will just set up something basic that will work in the sandbox gamemode ``` using Sandbox; [Library( "ent_customlamp", Title = "Lamp", Spawnable = true )] public partial class CustomLampEntity : Prop, IUse { [Net, Change] public bool Toggled { get; set; } = false; // this just allows us to say if the lamp is toggled or not. // and every time it changes it calls OnToggledChanged() for us public override void Spawn() { base.Spawn(); SetModel( "models/items/plain_set/plain_lamp.vmdl" ); SetMaterialGroup( 1 ); SetupPhysicsFromModel( PhysicsMotionType.Dynamic, false ); } // Set my model, and the correct material group (the self illum is materialgroup 1 in my case) public bool IsUsable( Entity user ) { return true; } public bool OnUse( Entity user ) { Toggled = !Toggled; // this sets toggled to the opposite state return false; } public void OnToggledChanged() { if ( !Game.IsClient ) return; // We can ONLY change material stuff client side, // plus we dont need to do anything else on stuff other than client if ( Toggled == true ) { SceneObject.Attributes.Set( "lightstrength", 1f ); } else if ( Toggled == false ) { SceneObject.Attributes.Set( "lightstrength", 0f ); } } public void Remove() { Delete(); } } ``` This allows me to just press e on the entity to change between lightstrength 1f and 0f. In the ``OnToggledChange()`` method i am actually using the dynamic expression we set earlier!!! using ``` SceneObject.Attributes.Set( "our-material-editor-propertyname-set-here", value ); ``` <note> Whenever we have a model spawned ingame, in any scene or in the world, its always going to be a SceneObject, so dont get confused as to why we are using it, its just the client side model we can edit.</note> Lets look at our new result!!! <upload src="70317/8d9f7976088115c.mp4" size="2432735" name="2022-02-24 13-10-40_Trim.mp4" /> we can put anything we want really in here, for example lets change it to this to make use of the color property instead! ``` public void OnToggledChanged() { if ( !Game.IsClient ) return; if ( Toggled == true ) { SceneObject.Attributes.Set( "lightcolor", Color.Red ); } else if ( Toggled == false ) { SceneObject.Attributes.Set( "lightcolor", Color.Green ); } } ``` <upload src="70317/8d9f7976d3e184f.mp4" size="3073016" name="2022-02-24 13-11-32.mp4" /> <note> since we only change the colour after its been toggled, it does not get set on spawn, you will have to do that yourself if you need it to be set on spawn, but its simple to do so</note> There now you know how to use dynamic expressions to change materials in game.
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
7
Cordons
Hotspot Materials
Selection Sets
Standard Mapping Dimensions
Tool Materials
Tools Visualisation Modes
Using Entities That Require a Mesh
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
7
Custom Fonts
Embedding Websites
Enabling Pointer Events
Events and Input
Localization
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
3
Guide to Sounds
Sound Events
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
7
Creating a Tool
Custom Asset Types
Guide to Widgets
Hammer API
Hammer Gizmos
Hotload Performance
Widget Docking
VR
3
Getting Started
VR Input
VR Overlays
Misc
13
Asset Types
Attributes and Component Properties
Backend API
Cloud Assets in code
Code Accesslist
CPU Performance Profiling
DisplayInfo
FileSystem
Mounting assets at runtime
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