Aller au contenu

Photo

Global Variables in a Mod


  • Veuillez vous connecter pour répondre
33 réponses à ce sujet

#1
Groove Widdit

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

#2
Shadooow

Shadooow
  • Members
  • 4 468 messages
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.

#3
GhostOfGod

GhostOfGod
  • Members
  • 863 messages
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?

#4
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
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.  

#5
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
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.

#6
Baragg

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

#7
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
What about adding a variable in the dialogue editor?

#8
GhostOfGod

GhostOfGod
  • Members
  • 863 messages
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 .


#9
Baragg

Baragg
  • Members
  • 271 messages

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 .


#10
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
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.

#11
Baragg

Baragg
  • Members
  • 271 messages

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.

#12
_six

_six
  • Members
  • 919 messages
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 .


#13
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
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.

#14
zunath

zunath
  • Members
  • 83 messages
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));
}


#15
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages

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

#16
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
What about creating a faction just to use it as a variable? You could set it from different places, couldn't you, and make a defacto global variable?

#17
Lightfoot8

Lightfoot8
  • Members
  • 2 535 messages
I have no Idea what you are saying. Are you saying to use it as an Int that can only go from 0 -100.

#18
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
Yeah that's what I meant, but I was thinking about it and I remembered how complicated it is to change faction relationships. I think I'm going to try the nondrop item.

#19
Shadooow

Shadooow
  • Members
  • 4 468 messages
Local variables works as global variables in the sense you mean

why making workaround for something that works?

#20
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
I'm trying to have a player only speak a line of dialogue only if a Beast is alive, so I'm trying to declare the variable iBeast in the OnModuleLoad and setting it to 1--the "alive" setting--and then setting to 0 in the critter's OnDeath script. Compiles, but doesn't work.

#21
MagicalMaster

MagicalMaster
  • Members
  • 2 000 messages
Because that's a local variable within the script, not the whole module. You'd want to put this in the OnModuleLoad:

SetLocalInt(OBJECT_SELF, "beastalive", 1);

where OBJECT_SELF is the module.

Then in the OnDeath put:

SetLocalInt(GetModule(), "beastalive", 0);

You could also technically put

DeleteLocalInt(GetModule(), "beastalive");

since that effectively sets it to 0.

Then, in the conversation itself, you'd need something like

if (GetLocalInt(GetModule(), "beastalive")) return TRUE;

where "TRUE" indicates the beast is still alive since the int is still set to 1 (would be 0 if beast was dead).

Modifié par MagicalMaster, 02 novembre 2013 - 12:39 .


#22
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
The last line won't compile, it is giving me: INVALID DECLARATION TYPE.

#23
MagicalMaster

MagicalMaster
  • Members
  • 2 000 messages
Assuming it's for a conversation and this script controls what the PC is allowed to say, the whole script should look like:
int StartingConditional()
{
    return (GetLocalInt(GetModule(), "beastalive"));
}

Simplified it a bit more.

#24
Shadooow

Shadooow
  • Members
  • 4 468 messages

Groove Widdit wrote...

I'm trying to have a player only speak a line of dialogue only if a Beast is alive, so I'm trying to declare the variable iBeast in the OnModuleLoad and setting it to 1--the "alive" setting--and then setting to 0 in the critter's OnDeath script. Compiles, but doesn't work.

you dont need to do that, instead check if she has been killed and set she has been killed on module like MM suggest by SetLocalInt(GetModule(),"blabla",TRUE); / GetLocalInt(GetModule(),"blabla")

#25
Groove Widdit

Groove Widdit
  • Members
  • 378 messages
I got it to work! Thanks, people. I had to change the one script to this:
int StartingConditional()
{
if (GetLocalInt(GetModule(), "iBeast")) return TRUE;
return FALSE;
}