struct_get_names

This function returns an array with the variable names from a struct.

You pass in the struct reference to check, and each entry in the array will be a String of the variable names that the struct contains.

NOTE This function doesn't return the static variables of the struct. These belong to its Static Struct, that you can get using static_get.

 

Syntax:

struct_get_names(struct);

Argument Type Description
struct Struct The struct reference to check.

 

Returns:

Array (each entry is a String)

 

Example 1: Basic Use

var _my_struct = {a: 7, str: "a string"};

var _arr_names = struct_get_names(_my_struct);
show_debug_message("Variables for struct: " + string(_arr_names));

var _str = "", _len = array_length(_arr_names);
for (var i = 0; i < _len; i++;)
{
    _str = _arr_names[i] + ":" + string(struct_get(_my_struct, _arr_names[i]));
    show_debug_message(_str);
}

The above code first creates a temporary struct variable _my_struct with two variables in it: a and str. Next, struct_get_names is called to get an array with the variable names for the given struct. These are displayed. Finally, a for loop is used to loop through all the variable names in the array and to look up the corresponding value in the struct by name using struct_get. For each of these key-value pairs, a debug message is shown using show_debug_message.

 

Example 2: Including Static Variables

function vec3(_x, _y, _z) constructor
{
    x = _x;
    y = _y;
    z = _z;
    
    static add = function(_vec2)
    {
        x += _vec2.x;
        y += _vec2.y;
        z += _vec2.z;
    };
    static dot = function(_vec2)
    {
        return dot_product_3d(x, y, z, _vec2.x, _vec2.y, _vec2.z);
    };
};
var _v1 = new vec3(100, 20, 0);

var _arr_names = struct_get_names(_v1), _arr_names_static = struct_get_names(static_get(_v1));
var _arr_names_all = array_concat(_arr_names, _arr_names_static);
show_debug_message($"Variable names for struct (including static): {_arr_names_all}");

The extended example above shows how to include static variable names by retrieving a struct's static struct and appending those variable names to the array of variable names.

First a simple vec3 struct is defined, which stores an x, y and z variable, as well as two static methods add and dot. A new struct _v1 is created from this. Next, both _v1's and its static struct's variable names are retrieved using struct_get_names. The two arrays are then concatenated using a call to array_concat. Finally, a debug message shows the contents of this new array, that contains all of the struct's variable names.

NOTE In case the struct gets static variables from a chain or hierarchy of static structs you'll need to traverse the chain of static structs to get the names of all static variables.