Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: johanvepa on Mon 03/10/2016 23:13:42

Title: I suck at arrays
Post by: johanvepa on Mon 03/10/2016 23:13:42
I just can't seem to get arrays right.

I am trying something that should be real simple and works if I just name all the characters one after the other, but I want to do it using arrays.

Whats wrong with this simple piece of code?? I am told there's a null pointer reference. But I assigned a value to the pointers. I don't understand.

In globalscript:
Code (ags) Select
Character *trash[12];
int trashcounter;

function removetrash()
{
  trashcounter = 1;
  while (trashcounter < 3)
  {
    if (trash[trashcounter].Frame == 4)
    {
      trash[trashcounter].x = 1;
      trash[trashcounter].y = 1;
      trash[trashcounter].Frame = 0;
    }
    trashcounter ++;
  }   
}

function repeatedly_execute()
{
  removetrash();
}

function game_start()
{
  trashcounter = 1;
  trash[1] = cAffald1;
  trash[2] = cAffald11;
  trash[3] = cAffald12;
}


Title: Re: I suck at arrays
Post by: Khris on Tue 04/10/2016 01:28:43
I tried the code as-is and it worked fine.
The only way I can see the error getting caused is you setting trash[1] or trash[2] to null elsewhere in the global script.
Title: Re: I suck at arrays
Post by: Crimson Wizard on Tue 04/10/2016 01:51:58
Not sure if related, but since you mentioned you do not know arrays very well, and seeing in your code that you start using element indexes from 1, I think I'd mention just in case that arrays have indexes starting from 0, and go up to (size-1).
For example, in your case (array of size 12) the valid indexes are 0 - 11.
Title: Re: I suck at arrays
Post by: johanvepa on Tue 04/10/2016 22:53:16
Thank you both very much.

crimson: I am aware of the array starting at 0. It's just easier on my eye to work with code beginning at 1, so I don't assign anything to 0.

Khris:
I noticed it only pops up if I assign values to trash[1] (and so on) in game_start. Crash occurs right after room_load in room 1. Any ideas what might be causing it?

Other than, of course, that elsewhere the script sets it to null.
Title: Re: I suck at arrays
Post by: Khris on Tue 04/10/2016 23:17:26
You can debug the code like this:

Code (ags) Select
Character *trash[12];
int trashcounter;

function removetrash()
{
  trashcounter = 1;
  while (trashcounter < 3)
  {
    if (trash[trashcounter] == null) Display("trash[%d] is null!", trashcounter);
    else if (trash[trashcounter].Frame == 4)
    {
      trash[trashcounter].x = 1;
      trash[trashcounter].y = 1;
      trash[trashcounter].Frame = 0;
    }
    trashcounter ++;
  }   
}

function repeatedly_execute()
{
  removetrash();
}

function game_start()
{
  trashcounter = 1;
  trash[1] = cAffald1;
  trash[2] = cAffald11;
  trash[3] = cAffald12;
}
Title: Re: I suck at arrays
Post by: Snarky on Wed 05/10/2016 05:51:36
Quote from: Nanuaraq on Tue 04/10/2016 22:53:16
crimson: I am aware of the array starting at 0. It's just easier on my eye to work with code beginning at 1, so I don't assign anything to 0.

I think you are getting yourself confused by breaking array conventions, though. For example, in your removetrash() loop, you never deal with trash[3].
Title: Re: I suck at arrays
Post by: Crimson Wizard on Wed 05/10/2016 13:10:40
Regarding array indexes, frankly I was more concerned about cAffald12. Is that character suppose to go into index 11 or 12 (which is out of bounds currently)?
Then again, maybe you know all this; simply trying to find what could be wrong there.
Title: Re: I suck at arrays
Post by: Retro Wolf on Thu 06/10/2016 21:46:48
Code (ags) Select
Character *trash[12];
Am I making sense of this right? You're creating 12 new characters in the code? So you can do this rather than right clicking in the character pane and clicking "New Character" 12 times? I didn't know you could do that with characters.
Title: Re: I suck at arrays
Post by: dayowlron on Thu 06/10/2016 22:15:27
That does not create 12 new characters. It creates an array to store Character pointers. Character pointers are nothing more than data that points to an existing character.
Title: Re: I suck at arrays
Post by: Retro Wolf on Fri 07/10/2016 19:59:47
Thanks for clearing that up!