Using "Arrays(?)" > How to use multiple objects with a number?

Started by Rik Vargard, Tue 03/01/2023 12:59:38

Previous topic - Next topic

Rik Vargard

Hello, so I've seen this thing around here but I'm not sure about the exact term to search for.

I have four objects that I want to make invisible:

image1
image2
image3
image4


I don't want to write four times image(x).Visible = false;

So I tried this one but it doesn't work, I get an error : undefined token 'image'

Code: ags
int i;
    while (i < 4)
    {
      image[i].Visible = false;
      i+;
    }

Any ideas?

eri0o

image1 is the name, it could be anything like oCharmander or whatever. The number there has no meaning for the computer.

Assuming they are objects, you would write something like

Code: ags
#define OIMG_COUNT 4
Object* oImage[OIMG_COUNT];

// call this in room_Load
function initArray()
{
 oImage[0] = oImage0;
 oImage[1] = oImage1;
 oImage[2] = oImage2;
 oImage[3] = oImage3;
}

// Later in your code
  for(int i=0; i<OIMG_COUNT; i++)
  {
    oImage[i].Visible = false;
  }

I typed this on my phone, so try to run and see if it works. I renamed your objects as oImageX because I think it makes easier to pick up the AGS internal type of it.

Crimson Wizard

Like eri0o mentioned, if the objects names have numbers - that does not mean anything, and does not automatically let you iterate through them. Iterating may only be done over arrays, but array is a separate variable, like a list, that has to store references to these objects.

I think it's worth mentioning that there are 2 potential questions here:
* iterating through existing objects using AGS own arrays.
* creating your own arrays;


In AGS many types of objects are already stored inside some array. For instance, room objects are stored inside "object" array (it's literally called - "object"; you may notice it in the autocomplete list when you type).

If objects in question are stored sequentially. have sequential IDs in this room, then you may as well just use that array. In the loop you would iterate starting with the first object's ID and until last object's ID.

Code: ags
int i = FIRST_OBJECT_ID;
while (i <= LAST_OBJECT_ID)
{
    object[i].Visible = false;
    i++;
}

If objects are not sequential, and it is not convenient to remake the room, then you would have to create a custom array and store either their indices or pointers there. Think of this as writing a list of items, which references only a portion of existing ones. This is what eri0o's example is about.

I'd only exclude unnecessary extra "initArray" function to simplify things, and put the initialization directly into Room_Load.
Also, to prevent possible misunderstanding (because you already had a misunderstanding about a number in name): your array's name does not have to be similar to object names. There's no connection at all. You may call it "ImageList", or whatever.
Code: ags
// This is array of object pointers, it will contain pointers to some objects, but not objects themselves
// defining OIMG_COUNT here is not obligatory, and is used only for convenience
// (to avoid hardcoded array sizes in case you would like to change the number of elements in the future)
#define OIMG_COUNT 4
Object* ImageList[OIMG_COUNT];

function Room_Load()
{
    // array indices begin with 0; overall indices have no relation to the object names;
    // that is - object names could be MyObject, MyAnotherObject, and so on, without numbers.
    ImageList[0] = image1;
    ImageList[1] = image2;
    ImageList[2] = image3;
    ImageList[3] = image4;
}


Rik Vargard

Wow that's very interesting! Well the four lines take less time in this case, but this opens possibilities!

Thank you both very much for the explanation.

Very much appreciated!

 (nod)

SMF spam blocked by CleanTalk