MODULE: QuickFill 0.2 (updated) - fill an array with one line of code!

Started by RootBound, Wed 25/09/2024 13:02:38

Previous topic - Next topic

RootBound

MODULE: QuickFill 0.1
EDIT: UPDATED TO NEW VERSION WITH BUG FIX.

Hello all! Are you sick of writing a ton of lines when defining the contents of an array? Especially when your project uses a million arrays? Quickfill is a tiny new script to save time and space by allowing you to fill an array all at once in one line of code.

Huge thanks to @Crimson Wizard for advice on how to script this. My first module!  ;-D

This is the initial release, version 0.1. Please report any bugs so I can fix them!!
DOWNLOAD HERE: https://www.mediafire.com/file/92lm7w2endt2mrv/QuickFill.zip/file

IMPORTANT: This module works only with AGS 3.6.0 or later.

Within this module, five functions are offered, allowing you to fill five types of arrays: int[], float[], bool[], String[], and Point*[].
These will produce the same result as doing it the long way.

For example, instead of doing this:
Code: ags
int MyArray[6];
MyArray[0] = 640;
MyArray[1] = 480;
MyArray[2] = 800;
MyArray[3] = 600;
MyArray[4] = 1280;
MyArray[5] = 720;
You'll simply do this:
Code: ags
int MyArray[];
MyArray = quickfill_int_array(6, "640,480,800,600,1280,720");

Script API:

To use the functions, first you declare whatever arrays you want (leave the size of the array blank. The function will declare the size later, so you will get an error if you do so now):
Code: ags
int MyIntArray[]; //sizes undefined
float MyFloatArray[];
bool MyBoolArray[];
String MyStringArray[];
Point* MyPointArray[];  //Note that you use Point *pointers here, not just regular Points.
Once you've declared your arrays, the specific functions available are:
Code: ags
int[] quickfill_int_array(int size, String values)
float[] quickfill_float_array(int size, String values)
bool[] quickfill_bool_array(int size, String values)
String[] quickfill_String_array(int size, String values)
Point*[] quickfill_Point_array(int size, String values)
You call the functions with the syntax below. The first parameter is always an int determining the size of the array, and the second is a String listing the values you want for each item.
IMPORTANT: Note the slight differences in punctuation within each function's String parameter, depending on the type of array.
Code: ags
//for ints, separate items in the String parameter by using commas.
MyIntArray = quickfill_int_array(5, "0,111,-20,36,-4"); //This line is all you need!! MyIntArray is now filled!

//for floats, the same syntax, commas to separate:
MyFloatArray = quickfill_float_array(4, "1.22,8.04,3.6,99.1"); //That's it!

//for bools, use 0 and 1 to represent true and false
MyBoolArray = quickfill_bool_array(7, "1,0,0,1,1,0,1"); //This fills the array with "true, false, false, true, true, false, true"

//Strings are more complicated, since we want them to be able to contain commas. Therefore, we separate String items using an asterisk.
//IMPORTANT NOTE: for this function, you must place an asterisk after the final item, not just between each one!
MyStringArray = quickfill_String_array(5, "Hello, world!*Hi, how are you?*I'm good, thanks. And you?*Just splendid.*Glad to hear it!*"); 
//all punctuation marks except the asterisks will be retained within each String.

//Finally, we use an asterisk to separate Points as well, since each point has both an x and a y coordinate.
//NOTE: Just as with the String array, you must place an asterisk after the final item!
MyPointArray = quickfill_Point_array(6, "1,4*10,200*-20,8*1400,35*-206,32*18,20*"); //this fills the six Points in the array with: 1,4 ; 10,200 ; -20,8 ; 1400,35 ; -206,32 ; 18,20

And that's it! I hope this saves you lots of time and scrolling through endless lines of array definitions. :)
They/them. Here are some of my games:

RootBound

Updated to new version with a bug fixed in the string function.
They/them. Here are some of my games:

ThreeOhFour


SMF spam blocked by CleanTalk