Author Topic: Help needed With Wierd Tag-Based Compiler Error <FIXED>  (Read 455 times)

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Help needed With Wierd Tag-Based Compiler Error <FIXED>
« on: May 10, 2015, 04:17:32 pm »


               

The following code snippet




        case X2_ITEM_EVENT_UNACQUIRE:


            // * This code runs when the item is unacquired
            // * Note that this event fires for PCs only


            oPC = GetModuleItemLostBy();            // The player who dropped the item
            oItem  = GetModuleItemLost();            // The item that was dropped


            object oHolder = GetItemPossessor(oItem);
            if(oHolder == OBJECT_INVALID)   // in other words, not in a creature or container inventory
            {
                object oPlaceable = CreateObject(OBJECT_TYPE_PLACEABLE, "ancientmapoft", GetLocation(oPC));
                DestroyObject(OBJECT_SELF, 1.0f);
            }
            break;


       case X2_ITEM_EVENT_SPELLCAST_AT:


Produces the error code


ERROR: SKIPPING DECLARATION VIA "case" STATEMENT DISALLOWED

at the 2nd case statement above. By the use of the // comment operator I have been able to isolate the offending bit of code to the section





            object oHolder = GetItemPossessor(oItem);
            if(oHolder == OBJECT_INVALID)   // in other words, not in a creature or container inventory
            {
                object oPlaceable = CreateObject(OBJECT_TYPE_PLACEABLE, "ancientmapoft", GetLocation(oPC));
                DestroyObject(OBJECT_SELF, 1.0f);
            }


but I have no idea why I am getting the error message. Any help appreciated.

 

TR



               
               

               
            

Legacy_Proleric

  • Hero Member
  • *****
  • Posts: 1750
  • Karma: +0/-0
Help needed With Wierd Tag-Based Compiler Error <FIXED>
« Reply #1 on: May 10, 2015, 05:42:37 pm »


               

Before the "switch" statement, you need declarations:


 


object oHolder;
object oPlaceable;

 


Then you can refer to oHolder and oPlaceable in cases.


 


NWScript is normally tolerant of declarations in the body of the code, but switch is an exception. Some coders prefer to declare all variables at the start of the module anyway, so that the scope of the variables is clear.



               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Help needed With Wierd Tag-Based Compiler Error <FIXED>
« Reply #2 on: May 10, 2015, 07:27:58 pm »


               

Thanks Proleric. That solution would never have occurred to me as it is counter imtuitive (to me at least). Just need to tweak the timing now and it'll be 100%.


 


TR



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Help needed With Wierd Tag-Based Compiler Error <FIXED>
« Reply #3 on: May 10, 2015, 07:44:51 pm »


               

The case is sort of a new block of code but not quite. You can also fix it by putting a new set of {} in there. e.g. 



case X2_ITEM_EVENT_ACQUIRE: {
       object oHolder = GetItemPossessor(oItem);
       //  do stuff
   
       break;
}

   The break can be outside the } if you want.  But you can't use oHolder outside the scope defined by the new {}.