Tinting objects in every room - "invalid object" error (SOLVED)

Started by Akril15, Sun 31/03/2019 03:18:31

Previous topic - Next topic

Akril15

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
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?

Slasher


Matti

You may save some work if you do this:

Code: ags

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();
}


Akril15

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.

morganw

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
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 ++;
}

Akril15

That works perfectly, morganw. Thank you very much!

SMF spam blocked by CleanTalk