S&box Wiki

Using Dynamic Expressions

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!, 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

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

And Now Click the Add Dynamic Expression... option, and it should show you a new window.

Dynamic Expression Window

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

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_custonlamp", 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 ( !Host.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 );
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.

Lets look at our new result!!!

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 ( !Host.IsClient ) return; if ( Toggled == true ) { SceneObject.Attributes.Set( "lightcolor", Color.Red ); } else if ( Toggled == false ) { SceneObject.Attributes.Set( "lightcolor", Color.Green ); } }
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

There now you know how to use dynamic expressions to change materials in game.