I suck at arrays

Started by johanvepa, Mon 03/10/2016 23:13:42

Previous topic - Next topic

johanvepa

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



Khris

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.

Crimson Wizard

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.

johanvepa

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.

Khris

You can debug the code like this:

Code: ags
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;
}

Snarky

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].

Crimson Wizard

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.

Retro Wolf

Code: ags
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.

dayowlron

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.
Pro is the opposite of Con                       Kids of today are so much different
This fact can clearly be seen,                  Don't you know?
If progress means to move forward         Just ask them where they are from
Then what does congress mean?             And they tell you where you can go.  --Nipsey Russell

Retro Wolf

Thanks for clearing that up!

SMF spam blocked by CleanTalk