Author Topic: What is a good way to clean up big if statements?  (Read 431 times)

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
What is a good way to clean up big if statements?
« on: May 19, 2011, 08:22:42 pm »


               I am working on a module that has a big if statement at the moment. And by the time I am done,
it will probably be bigger.  I was wondering what's a good way to try to streamline it.
This is the current script with it:

void main()
{
  int seconddoor; //This is the number rolled.
  int set = 0; //This is to determine if the conversation should get this far.
  string diceroll;
  object oPC = GetPCSpeaker();
  int BanksetTotal = GetLocalInt(oPC, "BanksetTotal");
  int Bank01 = GetLocalInt(oPC, "Bank01");
  int Bank02 = GetLocalInt(oPC, "Bank02");
  int Bank03 = GetLocalInt(oPC, "Bank03");
  // Get the Object for each door.
  object oNPCsDoor = GetNearestObject(OBJECT_TYPE_DOOR);
  object oDestDoor;
  if (BanksetTotal == 0)
  {
    seconddoor = d6();
    if (seconddoor < 3)
    {
        oDestDoor = GetObjectByTag("BankDoor1");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank01 = Bank01 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank01", Bank01);
    }
    else if (seconddoor > 4)
    {
        oDestDoor = GetObjectByTag("BankDoor3");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank02 = Bank02 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank02", Bank02);
    }
    else
    {
        oDestDoor = GetObjectByTag("BankDoor5");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank03 = Bank03 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank03", Bank03);
    }
  //Set oDestDoor as the destination for oNPC's door.
  SetLocalObject(oNPCsDoor,"TRANS_TARGET",oDestDoor);
  // Set oNPC's door as the Destination for oDestDoor
  SetLocalObject(oDestDoor,"TRANS_TARGET",oNPCsDoor);
  SetLocalInt(oNPCsDoor, "set", 1);
  }
  else if ((BanksetTotal == 1) && (Bank01 == 1))
  {
    seconddoor = d2();
    if (seconddoor == 1)
    {
        oDestDoor = GetObjectByTag("BankDoor3");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank02 = Bank02 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank02", Bank02);
    }
    else
    {
        oDestDoor = GetObjectByTag("BankDoor5");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank03 = Bank03 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank03", Bank03);
    }
  //Set oDestDoor as the destination for oNPC's door.
  SetLocalObject(oNPCsDoor,"TRANS_TARGET",oDestDoor);
  //Set oNPC's door as the Destination for oDestDoor
  SetLocalObject(oDestDoor,"TRANS_TARGET",oNPCsDoor);
  SetLocalInt(oNPCsDoor, "set", 1);
  }
    else if ((BanksetTotal == 1) && (Bank02 == 1))
  {
    seconddoor = d2();
    if (seconddoor == 1)
    {
        oDestDoor = GetObjectByTag("BankDoor1");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank01 = Bank01 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank01", Bank01);
    }
    else
    {
        oDestDoor = GetObjectByTag("BankDoor5");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank03 = Bank03 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank03", Bank03);
    }
  //Set oDestDoor as the destination for oNPC's door.
  SetLocalObject(oNPCsDoor,"TRANS_TARGET",oDestDoor);
  //Set oNPC's door as the Destination for oDestDoor
  SetLocalObject(oDestDoor,"TRANS_TARGET",oNPCsDoor);
  SetLocalInt(oNPCsDoor, "set", 1);
  }
  else if ((BanksetTotal == 1) && (Bank03 == 1))
  {
    seconddoor = d2();
    if (seconddoor == 1)
    {
        oDestDoor = GetObjectByTag("BankDoor1");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank01 = Bank01 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank01", Bank01);
    }
    else
    {
        oDestDoor = GetObjectByTag("BankDoor3");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank02 = Bank02 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank02", Bank02);
    }
  //Set oDestDoor as the destination for oNPC's door.
  SetLocalObject(oNPCsDoor,"TRANS_TARGET",oDestDoor);
  //Set oNPC's door as the Destination for oDestDoor
  SetLocalObject(oDestDoor,"TRANS_TARGET",oNPCsDoor);
  SetLocalInt(oNPCsDoor, "set", 1);
  }
else if ((BanksetTotal == 2) && (Bank01 == 0))
  {
//    seconddoor = d2();
//    if (seconddoor == 1)
//    {
        oDestDoor = GetObjectByTag("BankDoor1");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
//        FloatingTextStringOnCreature("The dice roll is:", oPC);
//        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank01 = Bank01 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank01", Bank01);
//    }
/*    else if (seconddoor == 2)
    {
        oDestDoor = GetObjectByTag("BankDoor3");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank02 = Bank02 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank02", Bank02);
    }*/
  //Set oDestDoor as the destination for oNPC's door.
  SetLocalObject(oNPCsDoor,"TRANS_TARGET",oDestDoor);
  //Set oNPC's door as the Destination for oDestDoor
  SetLocalObject(oDestDoor,"TRANS_TARGET",oNPCsDoor);
  SetLocalInt(oNPCsDoor, "set", 1);
  }
else if ((BanksetTotal == 2) && (Bank02 == 0))
  {
//    seconddoor = d2();
//    if (seconddoor == 1)
//    {
        oDestDoor = GetObjectByTag("BankDoor3");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
//        FloatingTextStringOnCreature("The dice roll is:", oPC);
//        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank01 = Bank01 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank02", Bank02);
//    }
/*    else if (seconddoor == 2)
    {
        oDestDoor = GetObjectByTag("BankDoor3");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank02 = Bank02 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank02", Bank02);
    }*/
  //Set oDestDoor as the destination for oNPC's door.
  SetLocalObject(oNPCsDoor,"TRANS_TARGET",oDestDoor);
  //Set oNPC's door as the Destination for oDestDoor
  SetLocalObject(oDestDoor,"TRANS_TARGET",oNPCsDoor);
  SetLocalInt(oNPCsDoor, "set", 1);
  }
else if ((BanksetTotal == 2) && (Bank03 == 0))
  {
//    seconddoor = d2();
//    if (seconddoor == 1)
//    {
        oDestDoor = GetObjectByTag("BankDoor5");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
//        FloatingTextStringOnCreature("The dice roll is:", oPC);
//        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank01 = Bank01 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank03", Bank03);
//    }
/*    else if (seconddoor == 2)
    {
        oDestDoor = GetObjectByTag("BankDoor3");
        //This is to let the PC know what the number rolled is.
        diceroll =  IntToString(seconddoor);
        FloatingTextStringOnCreature("The dice roll is:", oPC);
        SendMessageToPC(oPC, diceroll);
        BanksetTotal = BanksetTotal + 1;
        Bank02 = Bank02 + 1;
        SetLocalInt(oPC, "BanksetTotal", BanksetTotal);
        SetLocalInt(oPC, "Bank02", Bank02);
    }*/
  //Set oDestDoor as the destination for oNPC's door.
  SetLocalObject(oNPCsDoor,"TRANS_TARGET",oDestDoor);
  //Set oNPC's door as the Destination for oDestDoor
  SetLocalObject(oDestDoor,"TRANS_TARGET",oNPCsDoor);
  SetLocalInt(oNPCsDoor, "set", 1);
  }
}

At three different cities in the module, this randomly sets the bank door for that city to one of the
premade bank interiors.  It does not set two to the same one(which is how I want it).  This way
you don't know which bank will be for which city until you go in.
But I will probably be adding cities/towns and was wondering about streamlining this.


------------------------
"Knowledge knows no bounds!" But hindsight might wish there had been some!
               
               

               
            

Legacy_MrZork

  • Hero Member
  • *****
  • Posts: 1643
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #1 on: May 19, 2011, 11:30:14 pm »


               It might be a bit more readable if you group all of the BanksetTotal checks together (i.e. have one if checking BanksetTotal==1 instead of three). But, sometimes clumps of code are unavoidable.

It sort of looks like much of the clutter is debug code that might be tightened up a smidge. For example, these four lines

//This is to let the PC know what the number rolled is.
diceroll =  IntToString(seconddoor);
FloatingTextStringOnCreature("The dice roll is:", oPC);
SendMessageToPC(oPC, diceroll);

might be one line

FloatingTextStringOnCreature("The dice roll is: "+IntToString(seconddoor), oPC); //Show PC the roll

You might also be able to plop the repetitive stuff inside each conditional into a function call that takes 3 or 4 parameters...
               
               

               


                     Modifié par MrZork, 19 mai 2011 - 10:33 .
                     
                  


            

Legacy_eeriegeek

  • Jr. Member
  • **
  • Posts: 75
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #2 on: May 20, 2011, 04:18:48 am »


               I can't quite tell what your code is doing, a short explanation of what it means might help others give you some better ideas. Based on the general idea of what I think is going on, I think you may be able to make that segment more elegant by making it more data driven. You can construct variable names on the fly, something like:

int i = 0;
string sBankVarNameN = "BankSet" + IntToString(i);
int nBankTotalN = GetLocalInt(oPC,sBankVarNameN);
nBankTotalN += 1;
SetLocalInt(oPC,sBankVarNameN,nBankTotalN);

So you may be able to loop through all your potential banksets and bankdoors in a single set of nested loops.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #3 on: May 20, 2011, 05:07:08 am »


               Question: Is this for single player only?   If it is multiplayer would it not be better to set the Locals on something other then the PC's.   I am going to assume that this can be multi player and set the Vars on the module.  Also that once a door is set it is set for all PC's. 
The answer to cleaning up your If statment is to write in a more dynamic way.  To make a system where any number of doors can be added and still use the same script without modifing it.   I have flopped back and forth on how to store what doors are availiable for the for selection by the script.  Storing them in a string would be the simplest to understand when when reading the script.  So I thought long and hard about posting that solution.  storing the avaliable doors packed into a single int is more effecient. 

To simplify things I am just going to give you a few functions that you can make an include file out of.  It will allow you to just include it and simplify the scripts. 

Include:  _randomtags (untested let me know if you have trouble with it.)(tested bug fixed)

// Creats a local int on the module with the sPrefix lable.
// the Lower 5 bits of the var hold how many Tags are in the set.
// The Upper 26 bits holds information about which tags have already
// selected.  This function has to be called before GetRandomTagOnce is called.
// a second call to this function will reset the TagSet to where all
// Tags can be selected again.
void InitRandomTag(string sTagPrefix,int nNumTags);

// Return a Tag from the set only once.
string GetRandomTagOnceOnly(string sTagPrefix);


//Gets the number of Tags still Avaliable in the set.
// Returns -1 if the set is exhausted.
int GetNumOfAvailableTags(string sTagPrefix);
 

/////////////////Definitions ////////////////////////////////


void InitRandomTag(string sTagPrefix,int nNumTags)
{
 int nInit;

 if (nNumTags > 27) nNumTags = 27;
 if (GetStringLength(sTagPrefix) >30) sTagPrefix = GetStringRight(sTagPrefix,30);
 if (nNumTags > 0)
 {
   nInit = (1<<nNumTags)-1;
   nInit = (nInit<<5)| nNumTags;
 }

 SetLocalInt(GetModule(),sTagPrefix,nInit);
}



string GetRandomTagOnceOnly(string sTagPrefix)
{
   string sSubFix;
   int nCount, nRnd ,nPos;
   int nTagData = GetLocalInt(GetModule(),sTagPrefix);
   if (nTagData == 0 || nTagData == -1) return "";

   nRnd = Random(nTagData & 31)+1;
   nPos = 4;
   while(nCount < nRnd)
   {
     nPos++;
     if (nTagData & ( 1 << nPos)) nCount++;
   }
   nTagData = (nTagData ^ (1<<nPos)) -1;
   if ( nTagData == 0)nTagData = -1;  
   SetLocalInt(GetModule(),sTagPrefix,nTagData);

   nPos = nPos -4;
   sSubFix = IntToString(nPos);
   //if (nPos <10) sSubFix = "0" + sSubFix;

   return sTagPrefix + sSubFix;

}


int GetNumOfAvailableTags(string sTagPrefix)
{
   int nTagData = GetLocalInt(GetModule(),sTagPrefix);
   if (nTagData != -1) nTagData = nTagData & 31;
   return nTagData;
}



with the above script your starting conditions can look like this.

#include "_randomtags"

int StartingConditional()
{
    int iResult;
    iResult = GetNumOfAvailableTags("BankDoor")!= -1;
    return iResult;
}

 

and you action taken script can look like.

#include "_randomtags"
const string RANDOM_SET = "BankDoor";
void main ()
{
  if(GetNumOfAvailableTags(RANDOM_SET) == 0)
  {
   InitRandomTag(RANDOM_SET,3);
  }
  object oPC = GetPCSpeaker();
  string SecondDoorTag = GetRandomTagOnceOnly(RANDOM_SET);
 
 
  // Get the Object for each door.
  object oNPCsDoor = GetNearestObject(OBJECT_TYPE_DOOR);
  object oDestDoor = GetObjectByTag(SecondDoorTag);
  //Set oDestDoor as the destination for oNPC's door.
  SetLocalObject(oNPCsDoor,"TRANS_TARGET",oDestDoor);
  //Set oNPC's door as the Destination for oDestDoor
  SetLocalObject(oDestDoor,"TRANS_TARGET",oNPCsDoor);

}


I just noticed that your tags on the doors do not have leading zeros for numbers less the 10.  so I commented out the line that was adding them.  If you want to have the zero place holder just uncomment the line.

EDIT: I had one bug in the include.   the line
if ( nTagData = 0)nTagData = -1;
should have been
if ( nTagData == 0)nTagData = -1;
               
               

               


                     Modifié par Lightfoot8, 20 mai 2011 - 10:24 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #4 on: May 20, 2011, 05:10:28 am »


               

eeriegeek wrote...

I can't quite tell what your code is doing, a short explanation of what it means might help others give you some better ideas. Based on the general idea of what I think is going on, I think you may be able to make that segment more elegant by making it more data driven. You can construct variable names on the fly, something like:


The Topic started on the toolset board.  
Scripting an Area Transition?.?.?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #5 on: May 20, 2011, 11:25:45 pm »


               Sorry I had a bug in the Include file.   Now fixed and Tested.
               
               

               
            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #6 on: May 21, 2011, 03:19:38 am »


               Eeriegeek,

This script is for a module that has multiple cities and towns.  I want the door to the bank to each city and town to randomly pick from the interior bank areas already made, and set the door in that town that is to the bank to the area randomly picked.  And for it to only use areas once.

Ex:  there are 3 cities, and 3 bank interior areas.
Through a conversation, the PC sets City 1's bank to randomly go to bank interior 1.  Then he gets to City 2.  He does the conversation that sets the bank interior area for City 2 and it randomly picks from bank interiors 2 and 3(since 1 was already used).  It picks 3.  So now City 1's bank is bank interior 1 and City 2's bank is bank interior 3.
Then the PC gets to City 3.  Going through the conversation sets the bank for City 3 to bank interior 2(since the other 2 were already used).
Now the script I originally posted in this topic IS working.  I was just wondering how to make it cleaner for when more towns/cities and bank interior areas are added.  Because that is a lengthy if statement(and it is just for 3).

Now Lightfoot, as always, your help is appreciated.  I can see how making an include statement should simplify some things, but I am not 100% sure how to set a script as an include statement to be used in other scripts.
I am going to try to see if I understand the script you provided.  I may see if I have someone here who is familiar with scripting(not for NWN, just other scripting) who can help me follow it, if I can't.

Lightfoot, I started this post in the scripting section, because one this is about scripting, and we had gotten the main theme of the other topic working.

I have some basic learning of C++ and C#, but have not done much programming other than in NWN in awhile.

edit: Lightfoot, this module might be for Single player OR multiplayer.  It is basically a premade world that people can inhabit however that want.  That's the premise at the moment anyway.

----------------------------
"Knowledge knows no bounds!  But hindsight might wish there had been some!"
               
               

               


                     Modifié par datamaster, 21 mai 2011 - 02:22 .
                     
                  


            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #7 on: May 23, 2011, 04:05:37 pm »


               OK, I am still trying to understand the include.

Do I put this script of yours:

// Creats a local int on the module with the sPrefix lable.
// the Lower 5 bits of the var hold how many Tags are in the set.
// The Upper 26 bits holds information about which tags have already
// selected.  This function has to be called before GetRandomTagOnce is called.
// a second call to this function will reset the TagSet to where all
// Tags can be selected again.
void InitRandomTag(string sTagPrefix,int nNumTags);
// Return a Tag from the set only once.
string GetRandomTagOnceOnly(string sTagPrefix);

//Gets the number of Tags still Avaliable in the set.
// Returns -1 if the set is exhausted.
int GetNumOfAvailableTags(string sTagPrefix);
 
/////////////////Definitions ////////////////////////////////

void InitRandomTag(string sTagPrefix,int nNumTags)
{
 int nInit;
 if (nNumTags > 27) nNumTags = 27;
 if (GetStringLength(sTagPrefix) >30) sTagPrefix = GetStringRight(sTagPrefix,30);
 if (nNumTags > 0)
 {
   nInit = (1<<nNumTags)-1;
   nInit = (nInit<<5)| nNumTags;
 }
 SetLocalInt(GetModule(),sTagPrefix,nInit);
}
 
string GetRandomTagOnceOnly(string sTagPrefix)
{
   string sSubFix;
   int nCount, nRnd ,nPos;
   int nTagData = GetLocalInt(GetModule(),sTagPrefix);
   if (nTagData == 0 || nTagData == -1) return "";
   nRnd = Random(nTagData & 31)+1;
   nPos = 4;
   while(nCount < nRnd)
   {
     nPos++;
     if (nTagData & ( 1 << nPos)) nCount++;
   }
   nTagData = (nTagData ^ (1<<nPos)) -1;
   if ( nTagData == 0)nTagData = -1; 
   SetLocalInt(GetModule(),sTagPrefix,nTagData);
   nPos = nPos -4;
   sSubFix = IntToString(nPos);
   //if (nPos <10) sSubFix = "0" + sSubFix;
   return sTagPrefix + sSubFix;
}

int GetNumOfAvailableTags(string sTagPrefix)
{
   int nTagData = GetLocalInt(GetModule(),sTagPrefix);
   if (nTagData != -1) nTagData = nTagData & 31;
   return nTagData;
}

into a new script compiler, save and compile it KNOWING that it will have errors on compile(I tried already) and make sure to save it as _randomtags  ?

I was just thinking, as I am typing this.  It might help me to understand if you can make an example mod using this on a number of areas(your pick) and send the mod to me so I can load it and take a look at it.

edit-My email is dataalt@hotmail.com.
               
               

               


                     Modifié par datamaster, 23 mai 2011 - 03:09 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #8 on: May 23, 2011, 04:28:47 pm »


               Im at work right now so can not send you anything.  

So let me just first give you information on what an include is.  

An Include file is a file that contains code such as functions, vars, and constants  That you want to use in more then one script.   The file itself does not have a Main Entry Point ( void main())  there four you can not compile the file.   So if you have a function or other code that you know you are going to use in more then one script, instead of retyping it into every script you are going to use it in, you move it into a seperate file and save it under a unique name.   Now this file is not compiled under the name it is saved under.   Rather is is then included into other scripts that are compiled.   When ever the main body ( void main () )  of the script is compiled. The files #included at that top are treated as if they where typed line for line into the file itself.  

To answer your question you do not need to compile the file.  You just need to save it under a Unique name(all lower case). then  have #include "unique file name"  at the tope of the script you want to compile that uses the functions in the Include.

This makes it a lot simpler to add the same code over and over again.

If I did not want to use it as an include you could just take all of that code and add it to the top of both of the other scripts to have the same result.
               
               

               
            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #9 on: May 23, 2011, 05:00:07 pm »


               When I said compile, it was mainly because it "saves and compiles" when you save the script.
What I did to try based on what you had here, I put the first script into the script editor and saved it _randomtags, then I put the starting condition script you had here into the script editor and set it for the starting condition for a conversation.  Then the other one in the actions taken script for that conversation.
Ok none of the three scripts are compiling(which from what you are saying I can expect from _randomtags).
And for your last post, I DO understand WHAT an include is, I just was not sure HOW to set one up(ie make a script an include) but if you just save a script(all lowercase) that helps.

Still, it might be better(for me to understand) if you just send me a mod with this script in it(working, obviously).  When you get the chance(it doesn't have to be now).
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #10 on: May 23, 2011, 05:08:24 pm »


               Ok Ill send you a copy tonight.    The one miss conception I see in your last post is that you can save a file without compiling it.   Just hit the little Disk icon on the tool bar.   IT will save the script without compiling it.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #11 on: May 24, 2011, 04:44:15 am »


               Mail Sent.
               
               

               
            

Legacy_datamaster

  • Jr. Member
  • **
  • Posts: 94
  • Karma: +0/-0
What is a good way to clean up big if statements?
« Reply #12 on: May 24, 2011, 04:51:56 am »


               Ok, I got it.  Thanks again.  Now to go check it out.