Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: Snarky on Fri 27/02/2015 01:05:50

Title: character[] array and Game.CharacterCount
Post by: Snarky on Fri 27/02/2015 01:05:50
Working on the AGS ceremony, I'm having a weird crash on this bit of code:

Code (ags) Select
      bool on_stage = false;
      int o=0;
      while (o < Game.CharacterCount)
      {
         if ((character[o].y < 270) && (character[o].y > 224))    // CRASH!
         {
            on_stage = true;
         }
         o++;
      }


It crashes on the indicated line, with an "array out of bounds" error (index 77, range 0...76). The game currently has 90 characters (0-89), so I'm confused about why character[] would only go up to 76. Particularly since this bit of code earlier on does NOT crash:

Code (ags) Select
   int i=70;
   while(i<90)
   {
      character[i].Transparency = 100;
      i++;
   }


The crash started happening right after I added a bunch more characters, and I do think the character count before this was 77. I'm mystified... ???
Title: Re: character[] array and Game.CharacterCount
Post by: Kitty Trouble on Fri 27/02/2015 06:55:33
I'm not experienced with AGS scripting, but I do know programming in general. Possible explanations might be that Game.CharacterCount is more than 90 while you have initialized the array to be 90 (or whatever you initialized it to). Try creating a debug feature in your game to test this out (can't you just output the Game.CharacterCount as text somehow?)

It seems like this would have to be the case, unless AGS has problems with members of the array being uninitialized, but even if so, you wouldn't get an array out of bounds error from that.

Keep in mind that arrays are usually implemented as fixed-size, that is, the size you declared when it was initialized is set in stone and usually can't be extended directly.
Title: Re: character[] array and Game.CharacterCount
Post by: Crimson Wizard on Fri 27/02/2015 08:01:03
Quote from: chaosgodkarl on Fri 27/02/2015 06:55:33Possible explanations might be that Game.CharacterCount is more than 90 while you have initialized the array to be 90 (or whatever you initialized it to).
Both Game.CharacterCount and array size are declared by AGS itself, user has no control over them.

However, "character" array size is defined at the time of script compilation, while Game.CharacterCount is a property and its value is generated at runtime.
There might be a bug in either compiler or engine. Or maybe the game contains several scripts compiled at different times.

Snarky, what version of AGS it it? Have you tried to do "Rebuild all"?
Title: Re: character[] array and Game.CharacterCount
Post by: Snarky on Fri 27/02/2015 13:42:57
I'm on 3.3.3. Yes, it must be some kind of AGS bug, a value not getting updated somewhere internally. Restarting the editor didn't help, but after a "rebuild all" it's working. Thanks!

.. and thanks to chaosgodkarl too. Like CW says, the character[] array is created internally by AGS and should always have the same size as Game.CharacterCount, but you weren't to know that, and it was a reasonable suggestion.
Title: Re: character[] array and Game.CharacterCount
Post by: Crimson Wizard on Fri 27/02/2015 14:33:46
Quote from: Snarky on Fri 27/02/2015 13:42:57
I'm on 3.3.3. Yes, it must be some kind of AGS bug, a value not getting updated somewhere internally. Restarting the editor didn't help, but after a "rebuild all" it's working. Thanks!
AGS compiler does not rebuild all the scripts all times, it has some kind of detection algorithm to know when they need to be recompiled. Apparently, this algotithm does not work very well, and some of the modules keep old array definitions.
Title: Re: character[] array and Game.CharacterCount
Post by: monkey0506 on Fri 27/02/2015 15:54:19
Quote from: Crimson Wizard on Fri 27/02/2015 14:33:46Apparently, this algotithm does not work very well, and some of the modules keep old array definitions.

I rarely trust the incremental build for precisely this reason. Especially when publishing for Steam, if I change anything I've learned to do "Rebuild All", even if The Cat Lady does take several years to build. 8-)