GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2018-08-14 21:17:31

JangersBrah
Member
Registered: 2018-08-14
Posts: 2

Initialize 2D Array - GMS 1.4 & 2.1.5 versions

I created a quality of life 2D array creation script with debugging built in. It allows you to initialize a 2D array of any size with a value (optional).
Edit: Updated the functions with optimized array creation, and built in debugging. Along with a few other optimization tweaks. Thanks xot! biggrin

GMS 1.4

/// array_init_2d(size_x, size_y, debug, [value])
// Create a two dimensiona array, initialized with [value] (optional).
// size_x : The width of the array.
// size_y : The height of the array.
// debug : Output debug information in the compiler output? 'true' or 'false'
// [value] : Optional value for initialized array indices.

// Script Created By: JangersBrah
// Special Thanks To: xot(GMLscripts.com - Forum Admin)

// GMLscripts.com/license

var v, k, debug, value; // Declare local variables...
debug = argument[2]; // Is debug 'true' or 'false'?
value = 0; // Default value to 0...
for (v = argument[0] - 1; v >= 0; v -= 1;) // Reverse loop through x axis...
{
    for (k = argument[1] - 1; k >= 0; k -= 1;) // Reverse loop through y axis...
    {
        if (argument_count > 3) // Was a value given?
        {
            value = argument[3]; // It was! Set value to [value]...
        }
        array[v, k] = value; // Set array index value...
        if (debug == true) // Is debugging set to 'true'?
        {
            show_debug_message("[" + string(v) + ", " + string(k) + "] = " + string(value)); // It was! Output debug information in compiler output...
        }
    }
}

return array; // Return array...

GMS 2.1.5

/// @function array_init_2d(size_x, size_y, debug, [value])
/// @description Create a two dimensiona array, initialized with [value] (optional).
/// @param {real} size_x The Width of the array.
/// @param {real} size_y The Height of the array.
/// @param {bool} debug Output debug information in the compiler output? 'true' or 'false'
/// @param {undefined} [value] Optional value for initialized array indices.

// Script Created By: JangersBrah
// Special Thanks To: xot(GMLscripts.com - Forum Admin)

// GMLscripts.com/license

var v, k, debug, value; // Declare local variables...
debug = argument[2]; // Is debug 'true' or 'false'?
value = 0; // Default value to 0...
for (v = argument[0] - 1; v >= 0; v -= 1;) // Reverse loop through x axis...
{
    for (k = argument[1] - 1; k >= 0; k -= 1;) // Reverse loop through y axis...
    {
        if (argument_count > 3) // Was a value given?
        {
            value = argument[3]; // It was! Set value to [value]...
        }
        array[v, k] = value; // Set array index value...
        if (debug == true) // Is debugging set to 'true'?
        {
            show_debug_message("[" + string(v) + ", " + string(k) + "] = " + string(value)); // It was! Output debug information in compiler output...
        }
    }
}

return array; // Return array...

Last edited by JangersBrah (2018-08-15 10:06:01)

Offline

#2 2018-08-15 03:57:36

xot
Administrator
Registered: 2007-08-18
Posts: 1,195

Re: Initialize 2D Array - GMS 1.4 & 2.1.5 versions

Oh, this is a good idea. It seems pretty odd this isn't built-in.

I'd just make one little tweak. I'd populate the array in reverse, starting from the largest indices. That way memory is only allocated once instead of every time the bounds are increased.

GMS2 Manual wrote:
var i = 9;
repeat (10) {
    array[i] = 0;
    i -= 1;
}

This simple code will initialize a ten position array (0-9) to 0, in that each position in the array contains the value 0. You will notice that the array has been initialised backwards, with the last value being defined first. This is not strictly necessary but is the optimal way to do it as it will reserve a space in memory that is the exact size of the array, whereas if you initialize an array from 0 upwards, the memory has to be re-allocated for every additional value added. The speed difference is negligible for smaller arrays, but larger ones should be optimised as much as possible in this way. [Link]

Thanks for the submission and welcome to the forums.


Abusing forum power since 1986.

Offline

#3 2018-08-15 06:16:47

JangersBrah
Member
Registered: 2018-08-14
Posts: 2

Re: Initialize 2D Array - GMS 1.4 & 2.1.5 versions

This is great, I hadn't even thought of this before. It does make sense though. I'll work on both scripts and test them. I'll update each version accordingly along with the original post. Thanks smile

Offline

#4 2018-08-15 22:41:15

xot
Administrator
Registered: 2007-08-18
Posts: 1,195

Re: Initialize 2D Array - GMS 1.4 & 2.1.5 versions

Looks good! Thanks again.


Abusing forum power since 1986.

Offline

Board footer

Powered by FluxBB