[SOLVED] Verb coin null pointer problem

Started by Revae, Tue 17/01/2012 06:23:15

Previous topic - Next topic

Revae

Hello everyone!  I'm Matt, and here's my GUI.



I'm having trouble keeping the game from returning a null pointer using the verbcoin.  You see, my inventory takes up a small portion of the screen, and I didn't care for the way it closed when I dragged my mouse away from it.  So I added an 'X' button, and you can close it by right clicking, which works well enough.  But now if you try to use the verbcoin on a hotspot, object or character while the inventory is open it returns a null pointer error.  So I added some code to the end of the script here:

//===================================================================
// PROCESS VERBCOIN BUTTON CLICKS - INVENTORY
//-------------------------------------------------------------------
//
// Note: for each action the inventory item that is below the verbcoin should be used
// since the x- and y-coördinates are no longer valid, we use the saved mousex and mousey coördinates
// We can only avoid nullpointer errors by doing this check for each button.

         else{
            if (guixy == gui[verbc_id]) {
         if (control == look_button){
           gui[verbc_id].Visible = false;
                  Wait(1);

             Mouse.SetPosition(mousex,  mousey);
             if (GetLocationType(mouse.x, mouse.y) == eLocationHotspot){
             Display("I'm busy");
           }
             else{
             if (GetLocationType(mouse.x, mouse.y) == eLocationObject){
             Display("I'm busy");
           }    
             else{
             if (GetLocationType(mouse.x, mouse.y) == eLocationCharacter){
             Display("I'm busy");
           }    
             
             else{    

             item2 = InventoryItem.GetAtScreenXY(mousex, mousey);
             item2.RunInteraction(eModeLookat); //look at item

             }
     
         }  
               if (control == talk_button){
           gui[verbc_id].Visible = false;
           
             Mouse.SetPosition(mousex,  mousey);
             if (GetLocationType(mouse.x, mouse.y) == eLocationHotspot){
             Display("I'm busy");
           }
             else{
             if (GetLocationType(mouse.x, mouse.y) == eLocationObject){
             Display("I'm busy");
           }    
             else{
             if (GetLocationType(mouse.x, mouse.y) == eLocationCharacter){
             Display("I'm busy");
           }    
             
             else{            
       
                  Wait(1);
                  item2 = InventoryItem.GetAtScreenXY(mousex, mousey);
                  item2.RunInteraction(eModeTalkto); //talk to item
   
             }

//=============================================================
This is the part I added:
             Mouse.SetPosition(mousex,  mousey);
             if (GetLocationType(mouse.x, mouse.y) == eLocationHotspot){
             Display("I'm busy");

etc...

Which works for the look action, but anything else below that doesn't work at all.  Display("I'm busy"); can be replaced with the ability to look or what-have-you, but I'm trying to keep it simple for now as I'm not the greatest script-er (clearly).

Anyway.  I've been plugging away at this for a few days now and this is as far as I've made it.  I'm hoping to have the inventory open where you right click too, but I'm gonna take this one step at a time  :P

Any insight as to why this isn't working would be greatly appreciated.  
Thanks in advance for any help!

Also I realize I probably should've started an introduction thread in the appropriate forum before asking for help, but I kinda wanted to hold off on that until I can make a little video of it.

monkey0506

This is using the verb coin template that is distributed with AGS? Personally I disliked that particular implementation, and so I wrote my own (now available as the Verbcoin module).

In any case, that's not strictly relevant. Could you tell us exactly which line is throwing the null pointer error? If you run the game directly from the editor it should highlight it in the script for you. The fact that it's throwing a null pointer error isn't very useful unless we know what pointer being referenced is null.

Off the top of my head I'm guessing it's probably something to do with this bit:

Code: ags
else{    

              item2 = InventoryItem.GetAtScreenXY(mousex, mousey);
              item2.RunInteraction(eModeLookat); //look at item

              }


Since InventoryItem.GetAtScreenXY returns null if there is not an InventoryItem at the specified coordinates. In that case the solution would be to simply add a "if (item2 != null) " before the item2.RunInteraction line.

Your game looks pretty nice by the way!

Revae

Thanks Monkey!  I've actually seen your script floating around, but kinda wanted to give the bundled one a go (mostly to brush up on my scripting).

You were spot on with your guess as to what line was giving the error.

I'm at work now, but I'll give what you mentioned a go when I get home.  Seems like a more simple/clean solution to the one I was kicking around, and therefore will probably work, what with Occums razor and all.  I appreciate the quick reply.  :=

monkey0506

Static coding is the bane of customization.

Based on this particular situation, it might be worth perusing the rest of the code just to make sure that null pointers are handled in a safe manner (that's why I even check Hotspot*s, even though I know that under normal circumstances Hotspot.GetAtScreenXY should never return null, but hotspot[0]...it's still safer to assume that some catastrophic error could cause a null result and handle that case).

Let me know if it actually works for you. ;)

Revae

Yup.  Worked like a charm.  Or better than a charm rather.  In any even it's working now, and I have you to thank.

Thanks.  :)

I even have the code for repositioning the inventory almost working...  Busy day busy day.

Now to mark this as "Solved"

SMF spam blocked by CleanTalk