Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Akril15 on Sun 31/03/2019 03:18:31

Title: Tinting objects in every room - "invalid object" error (SOLVED)
Post by: Akril15 on Sun 31/03/2019 03:18:31
I'm working on a game that's going to have a day/night cycle, and since most of the rooms have at least one object in them, I want a a code that tints every object in every room when it's night. I thought this would work:

Code (ags) Select
if (day==false) {
        object[0].Tint(10, 10, 200, 70, 40);
        object[1].Tint(10, 10, 200, 70, 40);
        object[2].Tint(10, 10, 200, 70, 40);
        object[3].Tint(10, 10, 200, 70, 40);
                }
    else {
        object[0].RemoveTint();
        object[1].RemoveTint();
        object[2].RemoveTint();
        object[3].RemoveTint();
          }


But every time I open the game, I get an error at line 9 with the comment "SetObjectTint: invalid object". How can I fix this?
Title: Re: Tinting objects in every room - "invalid object" error
Post by: Slasher on Sun 31/03/2019 07:57:26
I assume you have checked all object ID's?
Title: Re: Tinting objects in every room - "invalid object" error
Post by: Matti on Sun 31/03/2019 11:03:55
You may save some work if you do this:

Code (ags) Select

int i;

if (day==false)
{
  for (i = 0; i < Room.ObjectCount; i++)
    object[i].Tint(10, 10, 200, 70, 40);
}
else
{
  for (i = 0; i < Room.ObjectCount; i++)
    object[i].RemoveTint();
}

Title: Re: Tinting objects in every room - "invalid object" error
Post by: Akril15 on Tue 02/04/2019 20:37:33
Quote from: Slasher on Sun 31/03/2019 07:57:26
I assume you have checked all object ID's?
Could you please elaborate on this? If I'm missing something obvious, I apologize.

Matti, I'm using AGS 3.3.3 -- I can't use "for" in that version.
Title: Re: Tinting objects in every room - "invalid object" error
Post by: morganw on Tue 02/04/2019 21:02:38
Quote from: Akril15 on Tue 02/04/2019 20:37:33
Quote from: Slasher on Sun 31/03/2019 07:57:26
I assume you have checked all object ID's?
Could you please elaborate on this? If I'm missing something obvious, I apologize.
I think just that, if you are accessing objects by an index you have to be sure that the index is valid. Your error suggests that you are trying to reference an object that doesn't exist, but using Room.ObjectCount should fix that.

Quote from: Akril15 on Tue 02/04/2019 20:37:33
Matti, I'm using AGS 3.3.3 -- I can't use "for" in that version.
You can do it with a while loop, you just have to manage the counter yourself.

Code (ags) Select
int i = 0;
int max = Room.ObjectCount;
 
while(i < max)
{
  if (day)
  {
    object[i].RemoveTint();
  }
  else
  {
    object[i].Tint(10, 10, 200, 70, 40);
  }
   
  i ++;
}
Title: Re: Tinting objects in every room - "invalid object" error
Post by: Akril15 on Wed 03/04/2019 02:48:47
That works perfectly, morganw. Thank you very much!