Skip to content

Text Variables

In this example, we will show you how to use the CreativeEditor SDK’s CreativeEngine to modify variables through the variable API. The variable API lets you set or get the contents of variables that exist in your scene.

Functions

fun findAll(): List<String>

Get all text variables currently stored in the engine.

  • Returns a list of variable names.
fun set(
key: String,
value: String,
)

Set a text variable.

  • key: the variable’s key.

  • value: the text to replace the variable with.

fun get(key: String): String

Get a text variable.

  • key: the variable’s key.

  • Returns the text value of the variable.

fun remove(key: String)

Destroy a text variable.

  • key: the variable’s key.
fun referencesAnyVariables(block: DesignBlock): Boolean

Checks whether the given block references any variables. Doesn’t check the block’s children.

  • block: the block to query.

  • Returns true if the block references variables, false otherwise.

Localizing Variable Keys (CE.SDK only)

You can show localized labels for the registered variables to your users by adding a corresponding label property to the object stored at i18n.<language>.variables.<key>.label in the configuration. Otherwise, the name used in variable.setString() will be shown.

Full Code

Here’s the full code:

// Query all variables
val variableNames = engine.variable.findAll()
// Set, get and remove a variable
engine.variable.set(key = "name", value = "Chris")
val name = engine.variable.get(key = "name") // Chris
engine.variable.remove(key = "name")
val block = engine.block.create(DesignBlockType.Graphic)
engine.block.referencesAnyVariables(block)