Author Topic: Question about a reserved word  (Read 390 times)

Legacy_Highv Priest

  • Full Member
  • ***
  • Posts: 170
  • Karma: +0/-0
Question about a reserved word
« on: December 16, 2012, 10:56:09 pm »


               I've been working in nwn for quite a while and I thought I'd learned all the tricks by now, but the "default" reserved word eludes me.

Does anyone know what that could possibly be used for?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Question about a reserved word
« Reply #1 on: December 16, 2012, 11:32:57 pm »


               switch/case Statement
               
               

               
            

Legacy_Highv Priest

  • Full Member
  • ***
  • Posts: 170
  • Karma: +0/-0
Question about a reserved word
« Reply #2 on: December 17, 2012, 06:46:53 am »


               Ah thank you. I suppose I could see that as being valuable, but I think in that situation I'd just use a series of else if's with an "else" to do the same thing as default.

Though that does bring to question, is the switch/case method less straining for the nwn engine or is the ole' standard if/else if still better?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Question about a reserved word
« Reply #3 on: December 17, 2012, 07:40:12 am »


               

Highv Priest wrote...


Though that does bring to question, is the switch/case method less straining for the nwn engine or is the ole' standard if/else if still better?


It really depends on the compiler, 

With the default nwn compiler they are about the same. if all of the breaks are placed in the switch they are compiled the same.   

If the advanced compiler is used,  well, I just don't  know if they improved it or not. 


But it is inportant to note that if you do not include the breaks the if... else if   and the switch are not the same statments.   For example,  lets say we had a placable that through PC actions get a power level var set on it.  we will say that the var could be in the range of 0 - 3.    if  we wanted the  placable to grant the PC effects based on the power level where:  

power level 1 would grant:  +4 constitution for 10 min 
power level 2 would grant : +4 constitution and 2 regenreation for 10 min.    
power level 3 would grant : +4 constitution, 2 regeneration and haste for 10 min.   

you could write it like this with a case statment: 


void main()
{
     object oPC = GetLastUsedBy();
     int nPowerLevel = GetLocalInt( OBJECT_SELF , "Power" );
     effect eEffect;
     switch ( nPowerLevel)
     {

        case 3:
            eEffect = EffectHaste();
            ApplyEffectToObject( DURATION_TYPE_TEMPORARY,eEffect, oPC,600.0);
        case 2:
            eEffect = EffectRegenerate( 2, 6.0);
            ApplyEffectToObject( DURATION_TYPE_TEMPORARY,eEffect, oPC,600.0);
        case 1:
            eEffect = EffectAbilityIncrease( ABILITY_CONSTITUTION, 4);
            ApplyEffectToObject( DURATION_TYPE_TEMPORARY,eEffect, oPC,600.0);
     }
}    

since the breaks are not there and the cases are in reverse order,  if case 3 happens  case 2 and one will also happen.   if the power level is 2: only case 2 and 1 will happen.    case 1   only case one will happen.  
               
               

               


                     Modifié par Lightfoot8, 17 décembre 2012 - 07:42 .