Author Topic: Global Variables in a Mod  (Read 1226 times)

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« on: July 20, 2011, 07:18:53 pm »


               Is it possible to create and use variables in a mod, like "int respect"? How is this done exactly? I suspect in the scripts.
               
               

               
            

Legacy_Shadooow

  • Hero Member
  • *****
  • Posts: 7698
  • Karma: +0/-0
Global Variables in a Mod
« Reply #1 on: July 20, 2011, 07:48:19 pm »


               not sure what you mean but global variable is defined outose of void main(){} like above or in include file

but this variable is not global totally thorought all scripts, only in one script. If you call it in second Im pretty sure its lost its value already so in the end its not much use except of something like custom GetFirst/NextSomething function where you need to count it somehow, using local variable on some object is one variant using global variable second one. But be carefull with name, it may happen that you reuse the global variable name inside void main(){}, then bad things can happen.
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Global Variables in a Mod
« Reply #2 on: July 20, 2011, 08:18:06 pm »


               Are you trying to have a respect system where you can store and track the amount of respect points a player has? Is it multi or single player mod, or a PW?
               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Global Variables in a Mod
« Reply #3 on: July 20, 2011, 09:36:36 pm »


               In truth every single varaiable that you create in NWscript is a local variable.   A Local varaiable being defined as a varaiable that is pushed/stored on the stack.  Vars that are defined before the void main () {}, are actualy still Local vars with a larger scope.   Still when the script is finished running and the stack is returned to the state iit was in when the script was called the Vars are gone.  

Ironically, In order to get something that resembles a Global variable, you need to use what NWN calls a Local Varaiable.  What NWN calls a Local Varaiable is a Key/Value list that is part of many of the Objects structures in the game,  So to get your Global Var you will need to attach it to an Object as a Local var to that Object.  Of cource the scope(Life Time)  of the var will not exceed the life time of the Object you attach it to. 

Look at:  
 Local Variables Functions

Looking mostly at the  Get/SetLocal*  functions.  
               
               

               
            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #4 on: July 20, 2011, 10:29:49 pm »


               Yeah I want to have the pc earn or lose respect in the mod. I wanted it to be playable either as single or multi.

L8 - thanx - aSetPLocalInt looks promising.
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Global Variables in a Mod
« Reply #5 on: July 21, 2011, 01:18:57 am »


               You could either use a database or I would recommend placing the variable on a no drop item the PC has, for persistence.
               
               

               
            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #6 on: July 21, 2011, 01:30:48 am »


               What about adding a variable in the dialogue editor?
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Global Variables in a Mod
« Reply #7 on: July 21, 2011, 02:17:18 am »


               On the line that you want to add the integer you would go to the ActionTaken tab and put in a script similar to this:


void main()
{
    object oPC = GetPCSpeaker();
    object oDBItem = GetItemPossessedBy(oPC, "Tag of DB Item here");
    int iReputation = GetLocalInt(oDBItem, "MY_REPUTATION");
    SetLocalInt(oDBItem, "MY_REPUTATION", iReputation + 10);
}


It checks the player for an item, then checks that item for an integer called "MY_REPUTATION", and then sets whatever that number is + 10. You can of course call the integer whatever you want and change the +10 to whatever you want. You might want to set a reputation int on the NPC the player is talking to. Then this script could just check for that integer and add that instead of the +10. Then you would only need one script if NPC will be giving different amounts of reputation. This can also be altered to add the reputation points to all of the players faction members as well.

Hope it helps. Good luck.
               
               

               


                     Modifié par GhostOfGod, 21 juillet 2011 - 01:21 .
                     
                  


            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Global Variables in a Mod
« Reply #8 on: July 21, 2011, 04:42:43 am »


               

Groove Widdit wrote...

What about adding a variable in the dialogue editor?


That would be more like how you add it, compared to where you put it. If your gonna go with the db or nodrop item idea you may want to consider the different groups in your mod may have different rep ratings toward the pc. The pc may have a steriling rep with the knights of goodly goodness, but be hated intensly by the dastardly devils of bewilderment. So you could in effect have several rep ratings with various groups, instead of one overall rep for a pc.
               
               

               


                     Modifié par Baragg, 21 juillet 2011 - 03:44 .
                     
                  


            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #9 on: July 21, 2011, 06:35:58 am »


               Yeah I could have structures of arrays of specific rep elements - this could be getting into fractal calculi - or even Quantum Superposition (if, of course, the whole universe could be used as a data manipulation field).

Thanks God. I'll try it.
               
               

               
            

Legacy_Baragg

  • Sr. Member
  • ****
  • Posts: 496
  • Karma: +0/-0
Global Variables in a Mod
« Reply #10 on: July 21, 2011, 02:43:28 pm »


               

Groove Widdit wrote...

Yeah I could have structures of arrays of specific rep elements - this could be getting into fractal calculi - or even Quantum Superposition (if, of course, the whole universe could be used as a data manipulation field).

Thanks God. I'll try it.


Lol.

If I am not mistaken NWScript does not have arrays.
               
               

               
            

Legacy__six

  • Hero Member
  • *****
  • Posts: 1436
  • Karma: +0/-0
Global Variables in a Mod
« Reply #11 on: July 21, 2011, 09:04:22 pm »


               No, but there are plenty of ways to simulate arrays if you're feeling particularly ambitious. Strings of data separated by special characters, for instance. For a puzzle where I had a 4x4 grid of switches I actually used a 16 bit boolean converted to a decimal int. Admittedly I did that more because it was fun to code than because it was even remotely practical.

Although a better solution in this case might just be to have a set number of sensibly named factions to lose or gain "_respect" with.
               
               

               


                     Modifié par _six, 21 juillet 2011 - 08:06 .
                     
                  


            

Legacy_Groove Widdit

  • Sr. Member
  • ****
  • Posts: 293
  • Karma: +0/-0
Global Variables in a Mod
« Reply #12 on: July 21, 2011, 09:11:02 pm »


               That sounds cool.

No arrays? Lame. Then you can never represent the entire universe in NWN, then. The basic unit of the universe is the string, with different frequency settings. On a certain level of VR you can take out the "virtual" - or go the other way: THe whole universe is virtual reality.
               
               

               
            

Legacy_zunath

  • Full Member
  • ***
  • Posts: 152
  • Karma: +0/-0
Global Variables in a Mod
« Reply #13 on: July 22, 2011, 04:06:37 pm »


               The lazy man's array:



// Although not a real array, it can be used as one.
// Returns an integer value stored at index iIndex
int GetLocalArrayInt(object oObject, string sVarName, int iIndex);
// Although not a real array, it can be used as one.
// Returns a string value stored at index iIndex
string GetLocalArrayString(object oObject, string sVarName, int iIndex);
// Although not a real array, it can be used as one.
// Returns an object value stored at index iIndex
object GetLocalArrayObject(object oObject, string sVarName, int iIndex);
// Sets an integer value at index iIndex for a variable named sVarName
void SetLocalArrayInt(object oObject, string sVarName, int iIndex, int iValue);
// Sets a string value at index iIndex for a variable named sVarName
void SetLocalArrayString(object oObject, string sVarName, int iIndex, string sValue);
// Sets an object value at index iIndex for a variable named sVarName
void SetLocalArrayObject(object oObject, string sVarName, int iIndex, object oData);
// Deletes an integer value at index iIndex for a variable named sVarName
void DeleteLocalArrayInt(object oObject, string sVarName, int iIndex);
// Deletes a string value at index iIndex for a variable named sVarName
void DeleteLocalArrayString(object oObject, string sVarName, int iIndex);
// Deletes an object value at index iIndex for a variable named sVarName
void DeleteLocalArrayObject(object oObject, string sVarName, int iIndex);

int GetLocalArrayInt(object oObject, string sVarName, int iIndex)
{
    return GetLocalInt(oObject, sVarName + IntToString(iIndex));
}

string GetLocalArrayString(object oObject, string sVarName, int iIndex)
{
    return GetLocalString(oObject, sVarName + IntToString(iIndex));
}

object GetLocalArrayObject(object oObject, string sVarName, int iIndex)
{
    return GetLocalObject(oObject, sVarName + IntToString(iIndex));
}

void SetLocalArrayInt(object oObject, string sVarName, int iIndex, int iValue)
{
    SetLocalInt(oObject, sVarName + IntToString(iIndex), iValue);
}

void SetLocalArrayString(object oObject, string sVarName, int iIndex, string sValue)
{
    SetLocalString(oObject, sVarName + IntToString(iIndex), sValue);
}

void SetLocalArrayObject(object oObject, string sVarName, int iIndex, object oData)
{
    SetLocalObject(oObject, sVarName + IntToString(iIndex), oData);
}

void DeleteLocalArrayInt(object oObject, string sVarName, int iIndex)
{
    DeleteLocalInt(oObject, sVarName + IntToString(iIndex));
}
void DeleteLocalArrayString(object oObject, string sVarName, int iIndex)
{
    DeleteLocalString(oObject, sVarName + IntToString(iIndex));
}
void DeleteLocalArrayObject(object oObject, string sVarName, int iIndex)
{
    DeleteLocalObject(oObject, sVarName + IntToString(iIndex));
}

               
               

               
            

Legacy_Lightfoot8

  • Hero Member
  • *****
  • Posts: 4797
  • Karma: +0/-0
Global Variables in a Mod
« Reply #14 on: July 22, 2011, 05:16:13 pm »


               

zunath wrote...

The lazy man's array:
....


Or just use the one already in the base scripts.   Even though it is not as expanded as the one Zunath posted.

#include "nw_o0_itemmaker"

GetLocalArrayInt

GetLocalArrayString

SetLocalArrayInt

SetLocalArrayString