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
public func findAll() -> [String]
Get all text variables currently stored in the engine.
- Returns: Return a list of variable names.
public func set(key: String, value: String) throws
Set a text variable.
key
: The variable’s key.value
: The text to replace the variable with.
public func get(key: String) throws -> String
Get a text variable.
key:
: The variable’s key.- Returns: The text value of the variable.
public func remove(key: String) throws
Destroy a text variable.
key:
: The variable’s key.
public func referencesAnyVariables(_ id: DesignBlockID) throws -> Bool
Checks whether the given block references any variables. Doesn’t check the block’s children.
id:
: The block to inspect.- Returns:
true
if the block references variables andfalse
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 variableslet variableNames = engine.variable.findAll()
// Set, get and remove a variabletry engine.variable.set(key: "name", value: "Chris")let name = try engine.variable.get(key: "name") // Christry engine.variable.remove(key: "name")
let block = try engine.block.create(.graphic)let referencesVariables = try engine.block.referencesAnyVariables(block)