Aller au contenu

Photo

Help needed With Wierd Tag-Based Compiler Error <FIXED>


  • Veuillez vous connecter pour répondre
3 réponses à ce sujet

#1
Tarot Redhand

Tarot Redhand
  • Members
  • 2 688 messages

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


#2
Proleric

Proleric
  • Members
  • 2 356 messages

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.



#3
Tarot Redhand

Tarot Redhand
  • Members
  • 2 688 messages

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



#4
meaglyn

meaglyn
  • Members
  • 811 messages

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 {}.