Author Topic: Help with an if statment  (Read 338 times)

Legacy_Surek

  • Full Member
  • ***
  • Posts: 169
  • Karma: +0/-0
Help with an if statment
« on: May 15, 2014, 02:42:02 am »


               

Help writing an if statement.


I have been working on this for a couple hours and I give up. Basically what I would like to happen is,


On player respawn check to see if the pc is either a particular class or a particular sub race and if so do something if not either one of those two options just stop running the script. Here is the bit of code I have been working with.


 


if ((GetLevelByClass(CLASS_TYPE_WIZARD, oRespawner)==0))


if (GetSubRace(oRespawner)!="Demonologist")


   return;


effect eFail = SupernaturalEffect(EffectSpellFailure());


ApplyEffectToObject(DURATION_TYPE_PERMANENT,eFail, oRespawner);


 


Please  let me know if this clear enough on what I'm trying to do or do you need more information.


               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Help with an if statment
« Reply #1 on: May 15, 2014, 02:53:16 am »


               

If I understand you right something like this should do it:



if (!GetLevelByClass(CLASS_TYPE_WIZARD, oRespawner) && GetSubRace(oRespawner)!="Demonologist")
   return;

               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Help with an if statment
« Reply #2 on: May 15, 2014, 03:14:19 am »


               

I am going to add some indentation and brackets to you code to make it a little more clear. They are not needed. it is just to show what you have scripted.


 



if ((GetLevelByClass(CLASS_TYPE_WIZARD, oRespawner)==0))
{
    // This is what is going to happen if respawner has 0 wizard levels.  
    if (GetSubRace(oRespawner)!="Demonologist")
    {
        // This is the true case for if the  respawner is not a Demonologist subrace.
        // It is also only happens if there where no wizard level. since it is in the true case of the       //statment above.
        return;
    }
}

// This is happening only if you did not make it through both If statments above.
effect eFail = SupernaturalEffect(EffectSpellFailure());
ApplyEffectToObject(DURATION_TYPE_PERMANENT,eFail, oRespawner);

With what you have written you would have to have 0 wizard level  and not have the subrace to advoid the effects.   



               
               

               
            

Legacy_Guest_JujuSamedi_*

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
Help with an if statment
« Reply #3 on: May 15, 2014, 03:38:38 am »


               

if (!GetLevelByClass(CLASS_TYPE_WIZARD, oRespawner)==0 && GetSubRace(oRespawner)!="Demonologist")
{
   return;
}

effect eFail = SupernaturalEffect(EffectSpellFailure());
ApplyEffectToObject(DURATION_TYPE_PERMANENT,eFail, oRespawner);

boolean expressions get evaluated from left to right. The first thing that will get evaluated will be the GetLevelByClass and then the GetSubRace. This is important because it can also be used for short circuiting. If you ask me you do not need the !GetLevelByClass(Class_Type_Wizard,oRespawner)==0, just replace it with GetLevelByClass(Class_Type_Wizard) because if it evaluted to either 0 or any number other than 0 than it can be used to evaluate boolean expressions.  A boolean expression in C++/C will either return 1 or 0 so you could skip that part and evaluate it with



if (!GetLevelByClass(CLASS_TYPE_WIZARD, oRespawner) && GetSubRace(oRespawner)!="Demonologist")
{
   return;
}

effect eFail = SupernaturalEffect(EffectSpellFailure());
ApplyEffectToObject(DURATION_TYPE_PERMANENT,eFail, oRespawner);

the ==0 is a bit redundant.


 


Concerning the braces, I add them because they make the code easier to read. A double brace even for one statement could help a lot in a huge codebase.


 


Your naming conventions are fine actually.



               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Help with an if statment
« Reply #4 on: May 15, 2014, 03:56:04 am »


               

sometimes it is best to just state what you want, then code it just like you said it.


Here is you statement:

 



if the pc is either a particular class or a particular sub race and if so do something if not either one of those two options just stop running the script.



if (the pc is either a particular class or a particular sub race)
{
   and if so do something
}
else
}
   if not either one of those two options just stop running the script.
}

 


Then just replace with the proper code.  



if (GetLevelByClass(CLASS_TYPE_WIZARD, oRespawner)> 0 ||  GetSubRace(oRespawner)=="Demonologist")
{
  effect eFail = SupernaturalEffect(EffectSpellFailure());
  ApplyEffectToObject(DURATION_TYPE_PERMANENT,eFail, oRespawner);
}
else
}
   return;
}


               
               

               
            

Legacy_Guest_JujuSamedi_*

  • Newbie
  • *
  • Posts: 12
  • Karma: +0/-0
Help with an if statment
« Reply #5 on: May 15, 2014, 04:38:16 am »


               


 


sometimes it is best to just state what you want, then code it just like you said it.


Here is you statement:

 



if the pc is either a particular class or a particular sub race and if so do something if not either one of those two options just stop running the script.



if (the pc is either a particular class or a particular sub race)
{
   and if so do something
}
else
}
   if not either one of those two options just stop running the script.
}

 


Then just replace with the proper code.  



if (GetLevelByClass(CLASS_TYPE_WIZARD, oRespawner)> 0 ||  GetSubRace(oRespawner)=="Demonologist")
{
  effect eFail = SupernaturalEffect(EffectSpellFailure());
  ApplyEffectToObject(DURATION_TYPE_PERMANENT,eFail, oRespawner);
}
else
}
   return;
}


 




This is good advice.


 


Programming is just logic actually so this is perfect advice.