Author Topic: Save yourself a lot of time - Make The Switch!!  (Read 963 times)

Legacy_Genisys

  • Hero Member
  • *****
  • Posts: 961
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« on: August 31, 2010, 04:23:56 pm »


               (Edited)

This is designed to be a basic tutorial, showing scripters the importance of making use of constant switches (Constant Variables which control a script or multiple scripts) within a script..

This is a very trivial tutorial, and would require basic knowledge of scripting.

(NOTE: Variable would be replaced with one of the following:   int / string / float)

Script A)    (No Constant Switches)

void main()
{
  //A Bunch of code will go here..

  variable a; //May Change
  variable b; //Does Not Change

 //Still yet more code may go here...
}

Script 'B)'     The Constant Switches go above the #include "scriptname" & void main()

(Here is an example of making use of a constant as a switch (the term switch is being used here to represent constant that controls the outcome of a script)

//Set the constant below to TRUE if you want ______ to happen..
constant variable VARIABLE_X = FALSE;  //default = FALSE (don't do this)

#include "somescript"

void main()
{
  //A Bunch of code will go here..

  variable a; //May Change
  variable b = VARIABLE_X;  //The Constant Unchanging Variable Above.. (VARIABLE_X = is the name)
 
 //(another example of use)
 if(VARIABLE_X==TRUE)
 {
   // Then do something here..
 }

 //Still yet more code may go here...
}

If you can see the importance of the Constant Switches, which is nothing more than a control for a script, then you will understand that Constant Switches will not only save yourself time later if you want to come back and change the script (& most likely will), but they also give others looking at your script greater control over your script, for one builder may like it one way, while another may like it another way, we therefore can avoid dissagreements by making controls within a script (Constant Switches) that effect how the scripts works..

I love Constant Switches, because I try to build for the community, I try to include these control switches when I can..
And they are absolutely fabulous for Template Scripts (Script you will use a lot)

Hope someone enjoyed this..  ':wizard:'
               
               

               


                     Modifié par Genisys, 06 septembre 2010 - 05:59 .
                     
                  


            

Legacy_ehye_khandee

  • Hero Member
  • *****
  • Posts: 1415
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #1 on: August 31, 2010, 06:33:13 pm »


               What ARE you on about here?  SWITCHES? Are you perhaps using a wrong / non-standard term for this (because I don't recognize it and I've noted loose use of jargon in your notes before).

Your text does nothing to explain the code and the code is really bare bones, so, if there is something here, maybe you can elaborate (and double check your terminology).

I notice you have a constant in there, but 'switch' is not the term to use there.
               
               

               


                     Modifié par ehye_khandee, 31 août 2010 - 05:34 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #2 on: August 31, 2010, 06:42:18 pm »


               Looked to me like he was wanting to build something like the X2_inc_switches.  



He just did not get any of the points across to make it usefull.
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #3 on: August 31, 2010, 07:10:17 pm »


               This is what I think of when i see/hear "switches":

int iInt = d3();
switch(iInt)
{
case 1://do stuff;break;
case 2://do stuff;break;
case 3://do stuff;break;
}

That is actually what I thought I was going to see when I clicked on this topic.':lol:'

I think you are just referring to constants that you can change at the top of the scripts to alter/customize a script to fit the needs or desires of the users. Not sure if there is a specific term for them. Customizable constants? Configuration constants? Control Constants?
I use these in the systems I've made and posted because I know people like to do things their own way. People might want a different sound to play, or a different visual effect, or a different item to drop, etc.. These are just some examples of what you might put in a constant variable at the top of scripts to easily let people alter the script.
               
               

               


                     Modifié par GhostOfGod, 31 août 2010 - 06:12 .
                     
                  


            

Legacy_ehye_khandee

  • Hero Member
  • *****
  • Posts: 1415
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #4 on: August 31, 2010, 07:54:11 pm »


               just constants is all they are called. also, never use constants unless you absolutely NEED to, as there are a limit to the number you can use in a module, so why waste?



               
               

               
            

Legacy_The Amethyst Dragon

  • Hero Member
  • *****
  • Posts: 2981
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #5 on: August 31, 2010, 08:04:38 pm »


               

ehye_khandee wrote...

just constants is all they are called. also, never use constants unless you absolutely NEED to, as there are a limit to the number you can use in a module, so why waste?


I've encountered a limit in constants in individual scripts (especially when using mutiple "#include" files), but is there a whole-module limit I missed somewhere?
               
               

               
            

Legacy_Genisys

  • Hero Member
  • *****
  • Posts: 961
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #6 on: August 31, 2010, 08:20:01 pm »


               

ehye_khandee wrote...

just constants is all they are called. also, never use constants unless you absolutely NEED to, as there are a limit to the number you can use in a module, so why waste?


Uh oh, looks like I was giving bad advice then..

What is the limit, do you know?   <<<I need to know myself..
               
               

               
            

Legacy_Genisys

  • Hero Member
  • *****
  • Posts: 961
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #7 on: August 31, 2010, 08:29:36 pm »


               

The Amethyst Dragon wrote...

ehye_khandee wrote...

just constants is all they are called. also, never use constants unless you absolutely NEED to, as there are a limit to the number you can use in a module, so why waste?


I've encountered a limit in constants in individual scripts (especially when using mutiple "#include" files), but is there a whole-module limit I missed somewhere?


If you hit a limit with one script, try reducing the # in that script, then create a new script with more, and see the results..

I'm curious if his statement is true, because I have to tell you with over 2000 scripts in my module, with loads of constants in the many different include scripts, I have never hit this limit message or whatever that is suppose to happen..

I use A LOT of template scripts, many of which use 2-8 constant variables them, plus many of my systems, and other builder's systems, use constants, I have to question the validity of that statement, don't get me wrong, I just want to make sure & know the precise answer as well..
               
               

               


                     Modifié par Genisys, 31 août 2010 - 07:35 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #8 on: August 31, 2010, 08:35:35 pm »


               Yes there is limit on number of functions+constants+global variables. AFAIK nobody doesnt counted exact number, but many peoples reached it already. For example there is somewhere at vault (cant find it atm) include file with all 1.69 constants, which is very nice but if you have a medium sized custom include file and put it together you reach this limit immediately.

PRC scripts reached it too, but they use for compilling script Torlack Compiler and this compiler has this limit bugger or even none.
               
               

               


                     Modifié par ShaDoOoW, 31 août 2010 - 07:36 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #9 on: August 31, 2010, 08:38:35 pm »


               There is a limit to the number of constants that the NWN compiler can handle. So yes, if you try to include to many constants in a single script, you will run into problems. Constants in never winter scripts, are not true constants. Meaning they are not allocated constants in the constant memory section of a program. The nwn compiler does not even assign a constant to a Var memory location. It replaces the constant with the literal value when it compiles the script. So if you have a constant defined as :

const int nApples =5;

then
....
int growth += nApples;
is the same as
int growth += 5;

And compiles to exactly the same thing. The compiler replaces the nApples with 5 while the code is being compiled.

Edited: for spelling.
               
               

               


                     Modifié par Lightfoot8, 31 août 2010 - 08:38 .
                     
                  


            

Legacy_Genisys

  • Hero Member
  • *****
  • Posts: 961
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #10 on: August 31, 2010, 09:12:57 pm »


               If what your saying is true Lightfoot8, then how could there be a limit?   (Can you fix your spellings some, I couldn't make out all of what you were saying there, sorry..)

I'm lost, and would definitely like a solid answer here, because I've seen some insanely high constants & functions in some includes which are part of other includes which also have many constants & functions.

Can someone add some clarifications on this topic?   (Me confusa)
               
               

               


                     Modifié par Genisys, 31 août 2010 - 08:22 .
                     
                  


            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #11 on: August 31, 2010, 09:33:40 pm »


               When LightFoot8 wrote they are not true constants he meant that in other, higher, programming languages the constants behave a bit differently. For NWScript don't worry head about this.



As a rule of thumb, constants in normal script are okay, but avoid putting constants into "main" include file as you could very soon reach this limit.
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #12 on: August 31, 2010, 09:34:39 pm »


               Simple answer:  



If you do not get a compile error when compiling your script, you do not have to many constants in it.  



The number of constants in one script will not effect any other script.



The limit is there because the compiler can only handle so many constants/vars.  Unless you are trying to include a Global master var/constant list into every script you write I dout you will often if ever hit the limit.
               
               

               
            

Legacy_Genisys

  • Hero Member
  • *****
  • Posts: 961
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #13 on: August 31, 2010, 10:03:56 pm »


               Uh oh...

Ok, so I'm making this include which will be attached to all spell scripts that I'm editing, this Include has quite a few constant (maybe 10 or 15 I think, not sure as the list is growing) though only like a few functions (4-6) (possibly 1-3 more)...

Am I doing something wrong, based upon the given information supplied thus far?

Any suggestions?  Cause I don't want to mess this system up, it's important to me, and others..
               
               

               


                     Modifié par Genisys, 31 août 2010 - 09:11 .
                     
                  


            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Save yourself a lot of time - Make The Switch!!
« Reply #14 on: August 31, 2010, 10:07:05 pm »


               No,  You will not mess up the system.  You are only using the includes the way they should be used.