Attribute Expressions

Many attributes let you pass in string parameters that can reference members or contain C# expressions to be evaluated. This can be extremely useful, as it lets you quickly and easily inject basic logic into your inspector. Attribute expressions are denoted by a string that starts with an @ symbol.

One of the many attributes that support expressions is InfoBox, which accepts an expression that evaluate to a string value, which is used as the content of the box.

The following attribute declaration is one of the simplest possible uses of attribute expressions, and will result in an infobox that displays the unmodified contents of the myStr field:

public string myStr;

You can also write more complex logic. The following attribute declaration will result in an infobox that always displays the current time:

[InfoBox(@"@""The current time is: "" + DateTime.Now.ToString(""HH:mm:ss"")")]
public string myStr;

Attributes like ShowIf and HideIf also support expressions:

[ShowIf("@this.someNumber >= 0f && this.someNumber <= 10f")]
public string myStr;

public float someNumber;

There are also some special expression keywords that let you access various contextual values from an attribute expression. For example, the $property keyword lets you access the InspectorProperty instance of the member that the expression is being evaluated on:

public class Example
	[InfoBox(@"@""This member's parent property is called "" + $property.Parent.NiceName")]
    public string myStr;

// Now, anywhere you declare it, myStr will now dynamically know the name of its parent
public Example exampleInstance;

As you might surmise, this is a very flexible and powerful tool. Odin's attribute expressions are backed by a lightweight compiler that supports the majority of C#'s expression syntax. If you write an invalid expression, you will be provided with a helpful compiler error indicating what is wrong.