Jump to content

Scripting Question - Incrementing Variables


Recommended Posts

Hello all and a Happy New Year!

 

I think the question that I have is rather simple to answer, but I can't find a solid answer online. What is the proper format for incrementing a variable? For instance, I've posted an example snippet of code below. Basically, I'd like to take the currently assigned value of a variable (Let's assume its a global variable I've created) and increment it by 5. In the mod I'm working on I tried:

Set MyVar to (MyVar + 5)

Let's say for demonstration purposes that "MyVar" is set to 50 before that line is run. When it runs in game however, instead of being set to 55 (which is what I would like for it to do), "MyVar" gets set to 5 instead. Am I using the wrong syntax? Obviously you can solve this issue by creating two "identical" variables and adding extra lines of code to make them mirror each other, such as:

Set MyVar to 50
Set MyOtherVar to 50

Set MyVar to (MyOtherVar + 5)
Set MyOtherVar to MyVar

Is there a simpler / more efficient way to complete this task within the game engine?

 

Thanks for your time!

 

Link to comment
Share on other sites

We'd need to see more and probably all of this script to know where your problem is. The first example is fine and should work. What value is MyVar supposed to hold? Is the variable ever reset to 0 in your script (or maybe another script, since it's a global)? If so, make sure this reset code is in the correct section of the script(s), e.g. having it in a GameMode block (which runs every frame) will cause MyVar to be continuously reset to 0 etc. This same problem would also be why your second example works as intended, since MyVar is also being continuously initialized at 50 with every frame, before the 5 is added. The second variable (MyOtherVar) is unnecessary and makes no difference to how the engine or your first variable works. Edited by TheMastersSon
Link to comment
Share on other sites

So that's the tricky part, that's basically all there is to the script. To put it in context however, it is running through the quest dialogue result script window. There's an NPC that speaks to the player, and after the NPC is done, that line is being run. The global variable that is being changed is initalized to 0 in the CS, but is changed via another separate script well before this line runs. The idea is that a base value is set initially and every following in-game week is either added to or subtracted from using the format listed above.

 

So, for example purposes, let's say the game changes the variable to 200. That value remains the same and is not changed at all outside of the line of code listed above. Essentially I'm trying to tell the engine to take 200 and add 5, but instead it is just changing the value to 5. It's not running inside a typical gamemode block, and if it were, I would add an if statement that forced it to only happen at a specific time

 

The first example is fine and should work.

 

So this is exactly my problem. That's exactly what I thought because it seems very straightforward and simple. But I feel like I'm missing some small nuance in the game's engine that is causing it to be interpreted incorrectly. :wallbash:

Link to comment
Share on other sites

What value is MyVar supposed to hold? Is the variable ever reset to 0 in your script (or maybe another script, since it's a global)?

 

Solved! This got me thinking, so I booted up the game and ran the appropriate console commands to get the value of the variable, and it came back as 0 (which of course explains the behavior above). I was operating under the assumption that the variable was only being modified in two places, and had forgotten about a third script that changes it back to 0. I feel really silly now, but thanks for your help TheMastersSon! +1 kudos for you :D

Link to comment
Share on other sites

Just in case someone else stumbles here later, there is more about assigning values on the set command page over at the Construction Set Wiki. There are also some expressions added by Oblivion Script Extender to reduce the amount of typing, listed in the OBSE Expressions section of the OBSE Command Documentation.

 

Hopefully that helps a bit. :)

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...