(SOLVED)Global object gives null pointer error.

Started by FanOfHumor, Mon 14/03/2022 22:22:24

Previous topic - Next topic

FanOfHumor

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

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

Khris

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
   if(event == eEventEnterRoomBeforeFadein && data == 3)  // only in room 3


Next you need to do

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


Now you can use  a.

FanOfHumor

Ok,but this needs to be for every room.How do I do that?

Khris

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
  a = object[2];

is what does that. Provided that the object in question is object #2 in every room, that is.

FanOfHumor


SMF spam blocked by CleanTalk