Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: FanOfHumor on Mon 14/03/2022 22:22:24

Title: (SOLVED)Global object gives null pointer error.
Post by: FanOfHumor on Mon 14/03/2022 22:22:24
I have a problem with a global object giving the null pointer error.Ive used global objects before so i'm thinking it might be the way i'm using it.Maybe I did something wrong with On_Event."a" is the global object.This happens for anything having to do with the object "a".
Code (ags) Select

function on_event(EventType event, int data)
{
  int geargraphic = Random(5);
  if(event == eEventEnterRoomBeforeFadein)
  {
   
    if(geargraphic==0)
    {
      a.Graphic=13;
    }
    if(geargraphic==1)
    {
      a.Graphic=8;
    }
    if(geargraphic==2)
    {
      a.Graphic=7;
    } 
    if(geargraphic==3)
    {
      a.Graphic=9;
    }
    if(geargraphic==4)
    {
      a.Graphic=10;
    }
    if(geargraphic==5)
    {
      a.Graphic=12;
    }
    a.X=200;
    a.Y=200;
  }
}
Title: Re: Global object gives null pointer error.
Post by: Khris on Tue 15/03/2022 00:28:02
Your  a  is a global Object pointer, currently pointing at nothing. You need to first make sure you're in the proper room:

Code (ags) Select
   if(event == eEventEnterRoomBeforeFadein && data == 3)  // only in room 3

Next you need to do

Code (ags) Select
  a = object[2]; // use actual object id from the editor

Now you can use  a.
Title: Re: Global object gives null pointer error.
Post by: FanOfHumor on Wed 16/03/2022 02:50:29
Ok,but this needs to be for every room.How do I do that?
Title: Re: Global object gives null pointer error.
Post by: Khris on Wed 16/03/2022 10:35:03
Just use your original if condition.
In the case of event being eEventEnterRoomBeforeFadein, the  data  param contains the room number, so just don't add that test to the condition.

What's causing the error is a being null because you never pointed it at an actual object. That's what you need to fix, and
Code (ags) Select
  a = object[2];
is what does that. Provided that the object in question is object #2 in every room, that is.
Title: Re: Global object gives null pointer error.
Post by: FanOfHumor on Wed 16/03/2022 20:47:47
Alright then. Thanks.