A Variable is an object storing a value (number or a string) or children variables.

Constructors

  • Parameters

    • Optional varData: Readonly<{
          children?: (Readonly<{ name?: string | undefined; value?: string | number | boolean | undefined; children?: Readonly<...>[] | undefined; type?: VariableType | undefined; }>)[];
          name?: string;
          type?: VariableType;
          value?: string | number | boolean;
      }>

      The optional initial content of the variable.

    Returns Variable

Methods

  • Add the given number to the variable value

    Parameters

    • val: number

      the number to add

    Returns void

  • Add a child variable with the specified name.

    If there is an existing child variable with this name, it is erased.

    Parameters

    • childName: string

      The name of the variable to add

    • childVariable: Variable

      The variable to add as a child

    Returns this

    The variable (for chaining calls)

  • Converts the variable into another type.

    Parameters

    • newType: VariableType

      The new type of the variable

    Returns void

  • Concatenate the given string at the end of the variable value

    Parameters

    • str: string

      the string to append

    Returns void

  • Deeply copies a variable into another.

    Parameters

    • source: Variable

      The source variable.

    • target: Variable

      The target variable.

    • Optional merge: boolean

      Should the target be merged with the source, or be an exact copy?

    Returns Variable

    The target variable.

  • Divide the variable value by the given number

    Parameters

    • val: number

      the divisor

    Returns void

  • Unserialize a JSON string into this variable.

    This just logs an error if the JSON is invalid.

    Parameters

    • json: string

      A JSON string.

    Returns this

  • Converts a JavaScript object into a value compatible with GDevelop variables and store it inside this variable.

    Parameters

    • obj: any

      The value to convert.

    Returns this

  • Return the object containing all the children of the variable.

    Returns Children

    All the children of the variable

  • Get the value of the variable, considered as a boolean

    Returns boolean

    The boolean value of the variable.

  • Get the value of the variable, considered as a number

    Returns number

    The number stored in the variable

  • Get the value of the variable, considered as a string

    Returns string

    The string stored in the variable

  • Get the child with the specified name or at the specified index.

    If the variable is an array, prefer getChildAt. If the variable is a structure, prefer getChildNamed.

    If the variable has not the specified child, an empty variable with the specified name (or index) is added as child.

    Parameters

    • childName: string | number

    Returns Variable

    The child variable

  • Get the child with the specified name.

    If the variable has not the specified child, an empty variable with the specified name is added as child.

    Parameters

    • childName: string

    Returns Variable

    The child variable

  • Gets the primitive value using the getter of the current type.

    Returns string | number | boolean

  • Return the child in a variable.

    Check if the variable has the specified children

    Parameters

    • childName: string

    Returns boolean

    true if variable has the children with the specified name

  • Return true if the variable type is a primitive type.

    Parameters

    • type: VariableType

    Returns type is "string" | "number" | "boolean"

  • Check if the variable must be considered as not existing in its container (usually a gdjs.VariablesContainer).

    Returns boolean

    true if the container must consider that the variable does not exist.

  • Multiply the variable value by the given number

    Parameters

    • val: number

      the factor

    Returns void

  • Pushes a value into the array.

    Parameters

    • value: string | number | boolean

    Returns void

  • Parameters

    • Optional varData: Readonly<{
          children?: Readonly<({ name?: string | undefined; value?: string | number | boolean | undefined; children?: Readonly<...>[] | undefined; type?: VariableType | undefined; })>[];
          name?: string;
          type?: VariableType;
          value?: string | number | boolean;
      }>

    Returns void

  • Removes a variable at a given index of the array.

    Parameters

    • index: number

    Returns void

  • Remove the child with the specified name.

    If the variable has not the specified child, nothing is done.

    Parameters

    • childName: string

      The name of the child to be removed

    Returns void

  • Replaces all the children with a new map of children.

    Parameters

    • newChildren: Children

      The map of new children.

    Returns void

  • Replaces all the children with a new array of children.

    Parameters

    • newChildren: Variable[]

      The array of new children.

    Returns void

  • Change the value of the variable, considered as a boolean

    Parameters

    • newValue: boolean

      The new boolean to be set.

    Returns void

  • Change the value of the variable, considered as a number

    Parameters

    • newValue: number

      The new value to be set

    Returns void

  • Change the value of the variable, considered as a string

    Parameters

    • newValue: string

      The new string to be set

    Returns void

  • Used (usually by gdjs.VariablesContainer) to set that the variable must be considered as not existing in the container.

    Returns void

  • Sets the primitive value using the setter of the current type.

    Parameters

    • newValue: string | number | boolean

      The primitive value of the variable.

    Returns void

  • Subtract the given number to the variable value

    Parameters

    • val: number

      the number to subtract

    Returns void

  • Converts this variable into an equivalent JavaScript object.

    Returns any

    A JavaScript object equivalent to the variable.

  • Private

    Get the value of the variable, as a number if it's one, or as a string (if it's a string or something else)

    In most cases, prefer calling getAsNumber or getAsString directly. This is a fallback in case a variable type can't be known statically for sure, like getValue.

    Returns string | number