Author Topic: Looking for help with variables  (Read 468 times)

Legacy_archer4217

  • Full Member
  • ***
  • Posts: 206
  • Karma: +0/-0
Looking for help with variables
« on: August 28, 2010, 08:17:17 am »


               Hi all,

My first time pestering for help on this forum..hehe.

I'm looking for help with variables. Try as I might, I can't seem to get the grasp of using them.
Anyway, here's what I'd like help with.

I'm making a bunch of portals where the player uses a portal to go to an area called the misty borders. When the player uses the portal, I'd like to have a variable placed on them that doesn't go away. Then, when they are inside the misty borders, they'll run across triggers that will check for the variable and if they have it, a doorway will be revealed.

So, for example, in Area A is Portal A. Player uses the portal and gets variable 1. In Area B is Portal B. Player uses the portal and gets variable 2...and so on for each portal they use. Then, in the borders, they come across a trigger that checks for variable 1,2 and so on, and reveals the doorway leading back to the corresponding portal.

So, having no understanding of how to implement all this, I'm using Lilac Soul's script generator (which I love, btw), and it's giving me 3 options.

Option 1 is Set the local string with a box next to it, and under that is another box that has "to" in front of it.
Option 2 is Set the local integer with a box, and below that are boxes that say Increment or Decrement and another "to" box.
Option 3 is Set the local float with a box and another "to" below it.

So which option would I choose?
What would go in the first box and in the "to" box?

Thanks in advance for any help.
'<img'>
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Looking for help with variables
« Reply #1 on: August 28, 2010, 09:00:42 am »


               This is one of those things that confused the heck out of me for the longest time. I tried to avoid them like the plague. Until I figured out how simple they really were to use.

All variables need to be given a name. Whatever name you want to give them. So for the first Lilac's option for example you might set the string variable "Portal1" to "Active". In this case "Portal1" is the name you are giving the string variable, but the actual string is "Active". The reason you give it a name is so that you can recall the string later. So when we recall the string we are asking. "What is the string that is stored under the name "Portal1"?" . Answer: "Active".
All of the variables pretty much work the same way. In most cases you can use integers cause they are just simpler to use but it totally depends on the situation.

So for your portals you might do something like...when a player gos through portal A, you could set an integer variable on that player. For this example will name the variable "PortalA" and we'll set it to 1.

SetLocalInt(oPlayer, "PortalA", 1);

Now when we do a check later to recall that stored variable we could do:

int iPortalA = GetLocalInt(oPlayer, "PortalA");  
if (iPortalA == 1)
   {
   //Do stuff here
   }

Now in the above examples we were storing the variable on the player (oPlayer). But you said you want the variable to not go away. A permanent variable. So here are some things to remember about variable storage.

-Storing variables on the player, any NPC, any placeable object in the game, on an area or on the module itself are all temporary. Once the server reset or crashes these variables will all be gone. Some of these are more temporary than others. If you don't mind variables going missing after a server reset or crash then you will be fine storing them on NPCs, areas, placeables or the module itself. But then again if you store variables on placeables or NPCs and you don't want the variables to go missing then make sure they are plot. Cause if they are destroyed then those variable will be gone. The most temporary would be storing variables on the player. As soon as a player logs off or crashes, stored variables will be gone.
EDIT: Just been corrected by Jedi Master Lightfoot8. Variables do stay on a player if they log out. So when a player logs back in to a mod, so long as it hasn't crashed, the variables will still be there. They will however be gone once the server crashes/restarts.

-The 2 permanent methods would be storing variables to a database or to an item a player possesses. The one thing about using a player item is just to make sure that you are saving the players every so often so that the variables will also be saved.

Well that's the best I can do I think. I hope it helped out somewhat. Good luck.
               
               

               


                     Modifié par GhostOfGod, 28 août 2010 - 08:17 .
                     
                  


            

Legacy_archer4217

  • Full Member
  • ***
  • Posts: 206
  • Karma: +0/-0
Looking for help with variables
« Reply #2 on: August 28, 2010, 04:31:08 pm »


               Thank you so very much, sweetie. That was incredibly helpful, and I have to admit, very heartening to know I'm not the only one struggling to understand variables..hehe.

*hugs*
               
               

               
            

Legacy_Genisys

  • Hero Member
  • *****
  • Posts: 961
  • Karma: +0/-0
Looking for help with variables
« Reply #3 on: August 28, 2010, 04:55:56 pm »


               All of us builders who were not coders before we came to NWN struggled with variables at some point, they are really not that hard once you gain the basic concept of what they are..

If you look at variables like algebraic math expressions, like (n) or (z), then you understand that they represent something... for example:  n = ?  Like Ghost states above, you can name a variable anything, therefore:

wOOt = 7491023;

We must tell the script what type of variable it is first, so the script can understand what your variable name means.

Types of Variables:

int          (This is a whole number 0 - 198759 for example)
float      (This is a decimal number   0.2344578 or 24.219 or 9234.5)
string   (This is a word or words which must be enclosed with parenthese >>  "like this"    )

so we define the variable by first telling the script what type of variable it is, then we give the variable a name, and then we give the answer to "What does the variable equal?"

Standard format for a variable

type name  = ???

int    / Any_Name / A Whole Number
float  / Any_Name / A Decimal Number
string / Any_Name / A word or multiple words. (Even a sentence or paragraph if you like.)

so for example..

int  MyInteger = 3;  ( note ; is for closing the line, which is done after every function in nwscript)
float MyFloat = 2.8;
string MyString = "My string is this.";

You can tell the script what the variable equals outright or you can just declare the variable, which then in turn tells the computer to hold a slot in memory for this variable. (we will define it later)

Examples of declaring variables...
int nVar;  
float fTime, fMore, fLess;  
string s1, s2, s3;  

The purpose of declaring a variable has multiple points, first we declare a variable because it may change multiple times through coding..  Secondly, we may declare a variable because it will be reused repeatedly by the script, therefore allocating a slot in memory allows for better performance.  This is important, because if your going to use an integer say 40 times in one script, then you definitely want to declare it, but not define it at first.

When we define a variable, after it has been declared, you will only see the name of the variable followed by an equal sign, and then everything that follows can be what defines the variable..

Therefore:

int a, nTime, c;    <<Declared Variables

a = 1;     <<Defined Variable...

nTime = GetHour();    <<Defined Variable  (I use a function as an example of an integer here)

GetHour() is a function which will tell the script what the current module hour is, which is an integer number, so we can use it to define what nTime actually equals... (which would be the current module hour when the script fires)

Also we can add variables like so..

c = nTime + a;      <<< This is how we add the int a to the int nTime (module hour + 1)

or

nTime += a;  << myself + another int

or

nTime = nTime + a;   <<another way to add variables together.. (redefine nTime as module hour + a)

There are many uses for variables..  Also, the name for a variable must always be one whole word not multiple spaced out words, therefore  (HI_thERE)  would work, but  (Hi There) would not work...

Variables are for the most part used to tell the script something, generally they are used to HOLD the number, decimal number, or word(s), sometimes we define a variable with a function, sometimes we get the information from an object, what ever information your using or how your using it, make sure the information your putting in the = ??? part, is correct for int / float / or string, for "234" does not equal 234 nor does it equal 234.0  Make sure what your put in ??? is proper to the type of variable your using..

Constants like this one:  VFX_DUR_AURA_BLUE  (For example) is an int

Therefore if you wanted to define ( int nPart ) as the belt slot of a PC's inventory, you could do it like so

int nPart = INVENTORY_SLOT_BELT;  <<so nPart = the belt slot...

we could use nPart like so..

object oItem = GetItemInSlot(nPart, oPC);

Here we have told the script oItem = The item in the Belt Slot of THIS PC (We would need to define who the PC is)


Getting information from an object is just a matter of one function, and setting the variable on the object another.

object oObect = ???;
//For getting the information from the object
int nStored = GetLocalInt(oObject, "THIS_VARAIBLE_NAME");

int nInt = ???;
//For setting the infromation on the object
SetLocalInt(oObject, "THIS_VARIABLE_NAME", nInt);

You would natrually replace "THIS_VARIABLE_NAME" with the name of the variable your wishing to use, just make sure you write it down, if you plan on using it in other scripts...

Thats the standard for getting / setting variables on an object, the object could be a placeable object or a PC...

Ok, that's enough for now, if you need more help, just ask..
               
               

               


                     Modifié par Genisys, 28 août 2010 - 05:01 .
                     
                  


            

Legacy_archer4217

  • Full Member
  • ***
  • Posts: 206
  • Karma: +0/-0
Looking for help with variables
« Reply #4 on: August 31, 2010, 10:43:58 pm »


               Thanks sweetie. '<img'>