Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Let’s Get Cooking on Wed 31/12/2014 12:17:36

Title: Null character pointer
Post by: Let’s Get Cooking on Wed 31/12/2014 12:17:36
Hi, I am trying to create a situation where an enemy (cTigerbad) touches any other character, that character will move to certain co-ordinates.

I thought I could add the below in the rep ex to check if the tiger is touching a character, assign the pointer "Charhit" to that character then make them move, but when the tiger does touch the character the game crashes due to null pointer on the last line.

Code (ags) Select

if (Character.GetAtScreenXY (cTigerbad.x, cTigerbad.y) != null){
Character * Charhit =  Character.GetAtScreenXY (cTiger.x, cTiger.y);
Charhit.Move (100, 180, eNoBlock, eAnywhere);}


I'm worried I may have completely misunderstood the use of the character pointer - any help would be appreciated!
Title: Re: Null character pointer
Post by: Monsieur OUXX on Wed 31/12/2014 18:08:00
I think that your issue is very simply that you check what character is at (tigerBad.x, tigerBad.y) but then you compute something using (tiger.x, tiger.y).


The code should be :

Code (ags) Select

Character * Charhit=  Character.GetAtScreenXY (cTigerBad.x, cTigerBad.y);
if (Charhit!=null) {
    Charhit.Move (100, 180, eNoBlock, eAnywhere);
}

Title: Re: Null character pointer
Post by: Let’s Get Cooking on Wed 31/12/2014 19:33:52
Drats! I cannot believe I missed that.

Thanks very much for pointing it out (and also for the more efficient way to write the code!)

Happy new year.