Jump to content

Scripting in Fallout New Vegas for Dummies part2 - variables.


Guest tomm434

Recommended Posts

Guest tomm434

It’s time to continue our journey into Scriptland. Today we’ll talk about variables. Variable is a name which contains some value.  We can use them to track progress of our quests, block scripts from running all over again etc.

First thing you should do is to load your old ESP file that you saved during First part of the tutorial. For it go to File-Data tab and find esp in the mod list, click on it and push "Set Active file" button and then "OK" button. Right now everything we change ingame is recorder to this file, "Fallout.esm" stays the same(it is never edited and you should not edit it under any circumstances.)
 

Then find our existing quest and its script.

Scriptname aaMyQuest
Begin gamemode
  If player.getav health <50
    Player.additem dogmeat 1
  Endif
End

As you already know this script will run indefinitely. Every moment script proceeds and player’s health is less than 50, he will be added a dogmeat. What if we want this code run only one time? For that we need to declare a variable.

It’s very simple – outside gamemode block we will type

short DoOnce”

 

And as simple as that we just created a variable.

 

DoOnce will be the name of variable(you can give it almost any name. If name is invalid, GECK will tell you about it).

Default value of  any variable is 0 (zero).

 

So we have variable which value is 0. Let’s add it to our condition.

Scriptname aaMyQuest
Short DoOnce

Begin gamemode
  If player.getav health <50 && DoOnce ==0
    Player.additem dogmeat 1
  Endif
End

You noticed that between GetAV and DoOnce conditions we placed “&&” symbols. They mean that we want 2 conditions be true at the same time. So it means that we want player health be less than 50 and DoOnce is eval to 0 and only then script can run.

 

If player’s health is more than 50, Script won’t run’.

If DoOnce is not eval to 0, script won’t run.

 

Now game engine will check for these 2 conditions and only then script wil be executed. But how do we block script from executing again? We sure can’t do anything with player’s health and that would be pointless and illogical to do. NOW because we introduced variable, we can block script from running again. For that simply set variable to 1 when script is executed.

 

For that we use command “set YourVariable to Value

In our case it will look like this – “Set DoOnce to 1

Scriptname aaMyQuest
Short DoOnce
Begin gamemode
  If player.getav health <50 && DoOnce ==0
    player.additem dogmeat 1
    set DoOnce to 1
  Endif
End

And now this happens – first time script is executed, DoOnce is eval to 0.

Then after script is executed once DoOnce becomes eval to 1.

 

And now next time game check for these 2 conditions it won’t execute scripts again because DoOnce is not 0 anymore. Thus player will be given DogMeat only once. Then before you manually set DoOnce condition to 0, script won’t be executed ever again.

Ok. What if you need to give player dogmeat 3 times? Simple! Just change conditions and the code!

Scriptname aaMyQuest
Short DoOnce
Begin gamemode
  If player.getav health <50 && DoOnce  <3
    player.additem dogmeat 1
  Endif
End

In this case script will only execute when DoOnce is eval to 0,1,2. If DoOnce is eval to 3,4,5,6,7, 1200, 5000. Etc, condition won’t be true.

 

In that case we need to add 1 every time script runs

We can do it by following command

   Set DoOnce to DoOnce +1

   Set YourVariable to Value

 

We use “Set” command again.

“Set YourVariable to Value”

But this time we don’t place a simple Figure instead of Value(1, or 2, or 1340) but variable itself +1. If DoOnce was eval to 0, it will become 1, if it was 1 it will become 2, etc.

With NVSE we can evade this ugly command(it’s ugly, we need to type variable name 2 times).

If you have  run GECK via NVSE launcher, you will be able to comply

 

 

Let DoOnce +=1

 

Let VariabeName $=Value

Instead of  $ you can put +, - and : symbols.

Some example so you unserstand how it Set and Let commands correspond

 

"Set DoOnce to 1" is equal to "Let DoOnce :=1"

"Set DoOnce to DoOnce -1" is equal to Let DoOnce -=1"

"Set DoOnce to DoOnce +1" is equal to Let DoOnce +=1 "

You can take a look at DoctaSax tutorial for further information(http://www.loverslab.com/topic/26749-tutorial-nvse4-part-1-syntax-and-expressions/)

 

I recommend using NVSE commands because they are shorter but in this guide I will continue using set commands so you understand text better. When you start scripting set is a better choice in my opinion(it gives you confidence, you can easily make a mistake with"let" command).

 

 

 

Let’s get back to our script

Scriptname aaMyQuest
short DoOnce
Begin gamemode
  If player.getav health <50 && DoOnce  <3
    player.additem dogmeat 1
    Set DoOnce to DoOnce +1
  Endif
End

First time script runs, DoOnce is 0 and it is set to be “0+1 ==1”

Second time script runs, DoOnce is 1 and it is set to be “1+1==2”

Third time script runs, DoOnce is 2 and it is set to be “2+1 ==3”

 

Fourth time script runs.. oh wait. It doesn’t. Now DoOnce is equal to 3 and condition “DoOnce <3” is not true. What a shame. Player won’t get dogmeat anymore. See what I’m talking about?

 

 

Now let’s talk about variables more.

There are 4 types of them but now you need to know only 2.

You can read about it here

http://geck.bethsoft.com/index.php?title=Declaring_Variables

 

     1)"short" or "int" variables. Don’t worry, they are equal.

int DoOnce  is the same as short DoOnce

No difference. EVER!

This type of variables can have value from -32,768 to 32,767

I’ve been scripting since February and I haven’t used LONG variable a SINGLE time. Just keep in mind that it exists.

 

 

2) "float" variables. It’s still variable but it can have precise value. In some cases(timer for example) you will need some precise value like “2.3”. If you declare short variable “DoOnce” and set it to 2.3, it will be pointless because this variable can’t have this value.

But if you declare float variable

 

float DoOnce

 

You can set it to any value you need.

You will need float variables when you make a timer.

 

In the end of the tutorial We will make a simple timer. What is a timer?

First of all, it’ s a simple variable. Yes. It has its own value and no magic beyond that.

If you want to use timer, you need to declare it first. Always use float variables for it.

float timer

Then you use out old set command

set timer to timer + GetSecondsPassed

Here we have new GetSecondsPassed  function. It is added to timer value every time script proceeds.

By the way, instead of set timer to timer + GetSecondsPassed you can use "let timer +=GetSecondsPassed". Looks slim compared to Set one

 

Let’s examine this code

float timer
Begin gamemode
set timer to timer + GetSecondsPassed
end

So for example if quest script delay is set to 1 second, this line is executed every second. So after 15 seconds, timer variable will be equal to 15.

But what if quest script delay is set to 3 seconds? No troubles for you!

The things is, value of GetSecondsPassed is defined by script delay. Meaning that in case script executes every 3 seconds(instead of 1), every time GetSecondsPassed will be equal to 3 seconds. In 15 seconds script will execute itself 5 times, adding each time 3 seconds to the timer value. Thus after 15 second pass, timer’s value will still be 15 seconds.

 

And what if  script quest delay is set to 10 seconds? Same thing! Every 10 seconds timer value will be set to timer value + 10.

 

And in case if script delay is set to 0.01 seconds(less than second), timer will be added small amount of value every time but still in 15 seconds you will get value of 15.

 

The important thing there is that script must be executed over and over again. Remember –timer is a variable with its own value, nothing more, it needs to be refreshed like any other variable.

 

Now you might ask –what should I do next after I set timer? Do condition of course. In case you already forgot it – timer is a variable.

float timer
Begin gamemode
set timer to timer + GetSecondsPassed
   if timer >=20
     MesageEx “Timer has expired”
    Set timer to 0
   endif
end

So when timer has value more than 20, message will appear and then timer is set to 0 again. Then again after 20 seconds have passed, message appears once again.

 

What if you want to stop timer forever after it’s value reaches 20? You already know. Do script blocker.

float timer
Begin gamemode

If DoOnce ==0
Set timer to timer + GetSecondsPassed

   if timer >=20
     MesageEx “Timer has expired”
    Set DoOnce to 1
   endif

endif

end

You see? After timer has more value than 20, DoOnce is set to 1 and timer won’t “tick” anymore. Try making your own timer. Try setting more difficult conditions and result script.

 

The only thing you should remember though is that quest variables are stored in your savegames. So if DoOnce is set to 1 once, it will stay 1 before you do something with it. So, right now you have many options here.

  1. Make clean save before activating your esp and then load it every time. Every time game will start a new quest in which DoOnce wil be equal to 0
  2. Introduce new variable every time “DoOnce2, MyVariable, Another variable. Keep in mind that you can change quest script anytime you want even if the quest is running in savegame, nothing bad will happen. You can even change conditions and game won’t CTD or anything.
  3. Reset your quest from game console. Use command “ResetQuest QuestID” – in console you cant use QuestID just like you do in GECK. http://geck.bethsoft.com/index.php?title=ResetQuest

In GECK you can type “stopquest aaMyQuest” or “resetquest aaMyQuest” but in console you will have to type quest ID. You can see quest ID in GECK – just wide “FormID field”. Then remove first 2 numbers and place load order of esp in their place.

 

In GECK formID is “12805Fd0d”

In game you will type “xx805fd0f” where XX is the load order of the esp. It is very useful to learn how to do that because eventually you might have to use that command to save the time when you mod.

 

 

 

 

Variables can be tricky. That’s why now we are going to play a game. I will give you wrong scripts and you will fix them.

 

When you make a script and it doesn’t do what you told it to, always examine it like you are the game engine. To control game engine, you need to think like game engine, feel like game engine, become one of them!!

 

I you don’t know the answer, read guide again(and the part in first guide where I say things about how game engine checks script and the part which is under the spoiler). If you still can’t answer the question, look under the spoiler below (and make me sad because it this my fault in some way that you got here and still can’t read this script )

 

 

1)

Begin gamemode
   if timer >=20
     set timer to timer + GetSecondsPassed
     MesageEx “Timer has expired”
   endif
end

In this script command “MesageEx “Timer has expired” will never be executed. Why? Think like game engine and figure it out yourself.

 

 

 

Engine checks If timer >=20 FIRST and it is not true. Thus “set timer to timer + GetSecondsPassed” command will skipped and never be executed. Thus timer will always stay 0.

 

 

 

 

2)

Begin gamemode
 set timer to timer + GetSecondsPassed
     if timer >=20
        MesageEx “Timer has expired”
    endif

  Set timer to 0
end

 

No matter what happened with timer before "Set timer to 0" line, it will always reset timer and next time script is executed again, timer will be equal to 0

 

 

3)

Begin gamemode
Short DoOnce
   set timer to timer + GetSecondsPassed

     if timer >=20
       MesageEx “Timer has expired”
       set timer to 0
    endif


end

 

Variable is declared inside gamemode block.

 

 

 

Now you know a simple thing about variables and timers. Hope this helps.

 

Link to comment

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

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

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. For more information, see our Privacy Policy & Terms of Use