This function can be used to get the name of the constructor function that was used to create a struct. The struct itself should have been created using the new operator along with the constructor itself. You supply the variable with the struct reference to check and the function will return either a string with the constructor name or undefined.
TIP It's recommended to use is_instanceof to check the constructor of a struct, as it additionally allows you to check using parent constructors (i.e. constructors that the struct's constructor may have inherited from). is_instanceof also allows you to check using the constructor function reference directly instead of strings.
instanceof(struct_id);
Argument | Type | Description |
---|---|---|
struct | Struct | The struct reference to use. |
In this example we must first define the function that will be used as the constructor for our struct. The following function is defined in a script asset:
function init_struct(_a, _b, _c) constructor
{
a = _a;
b = _b;
c = _c;
}
This function can then be used along with the new operator to create a struct and populate it with the variables set to the values of the arguments used in the function:
mystruct = new init_struct(10, 100, "Hello World");
We can then get the name of the constructor function that was used like this:
var _name = instanceof(mystruct);
if (is_string(_name))
{
show_debug_message(_name);
}
This will print the string "init_struct" to the output log, which is the name of the constructor function as defined in its script.