GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2010-07-15 00:37:42

flexaplex
Member
Registered: 2008-12-11
Posts: 72

Script Improvement

This is a script from my recently made multi-struct project: http://gmc.yoyogames.com/index.php?showtopic=480656

I'm sure a few more scripts in it can probably be improved but I'm most curious about this one as I'm doubting my solution is optimal. This script is made to create array indexes up to the indicated array indexes and assign the value of 0 to them. It is used to mimic the behaviour that arrays do when set in GM. For example when you set in gml:

Expandarr[2, 4] = val;

If that is the first time you have set to arr GM will automatically assign the indexes up to it with the value 0, ie:

[0, 0] [0, 1] [0, 2] [0, 3] [0, 4] [1, 0] [1, 1] [1, 2] [01 3] [1, 4] [2, 0] [2, 1] [2, 2] [2, 3] 

This is what I am trying to do with this script, except in this project the number of dimensions for the array can be from anything from 1 to 14 so it becomes a little more difficult. This is a simplified version (I've removed error checking and some other stuff) of the the script I am using:


Expand//ar_fill(id, ind1, ind2, ind3, ...) arbitrary number of indexes can be entered as arguments, the number should be equal to the array dimension

var i, dim_n, ar_id;
ar_id = argument0;

i = 1;
dim_n = ds_map_find_value(ar_dim_map, ar_id);  //returns the number of dimensions the array is

argument1 = floor(argument1);

if (dim_n == 1)  //1 dimensional arrays are handled separately as they can be executed much faster doing so
{
  for (i = 0; i < argument1; i += 1)
  {
    ds_map_add(ar_id, string(i), 0);   //to store the indexes I am just using a ds_map
  }
}
else
{
  var am, ac, str, tot;

  i = 1;
  tot = 1;
  repeat (dim_n)
  {
    am[i - 1] = argument[i] + 1;   //set all the index arguments into an array
    ac[i - 1] = 0;
    tot *= am[i - 1];  //add up the sum of all the indexes
    i += 1;
  }

  repeat (tot)
  {
    i = 0;
    str = "";
    repeat (dim_n)
    {
      str += string(ac[i]) + ",";   //add the indexes into a string for storing in the ds_map
      i += 1;
    }

    i = 1;
    repeat (dim_n)   //this loop is explained below
    {
      ac[dim_n - i] += 1;   
      if (ac[dim_n - i] == am[dim_n - i])
      {
        ac[dim_n - i] = 0;
      }  
      else
      {
        break;
      }
      i += 1;
    }
    ds_map_add(ar_id, str, 0);
  }
}

The method used in that loop is basically to increase indexes individually starting from the right if index is increased without reaching the index limit the code breaks otherwise it will move onto the next index to the left. This gives the desired effect of adding.

So yer just wondering if anyone can think of a better method or way of improving the script. Hopefully everyone wont run a mile when they see the code (like I probably would smile).

Last edited by flexaplex (2010-07-15 00:40:34)

Offline

Board footer

Powered by FluxBB