How do I increase or decrease the value of a global variable?

From OHRRPGCE-Wiki
Jump to navigation Jump to search

When you use the switch command simply when you want to make a menu and display stats you need to be able to decrease and increase variable. ‏‎ They are various ways to do this. Before we start open your hss and add global variable at the top of your file.

global variable (36, test)


Dedicated plotscript command[edit]

So let's say that you want to decrease or increase the value inside the variable by two. There are plotscripts command which specially does this. Feel free to click on the link to the plotscript dictionary for more details.

If you want to decrease the value inside the variable test by 2, you just write

  decrement (test, 2)  

At the opposite, it want to increase the content of the variable by 4 you simply write

  increment (test, 4) 

Aside the plotscript commands which have been created for this are not the only way to do this. You can also choose to write directly inside the variable its new value.


Put the new value directly inside the variable[edit]

This method is very simple and requires only the sign :=. How does this work? Well let's take an example. Imagine we write

 test:= 7

In this situation the content on the variable is 7. Before the sign := we have the name of the variable and after the sign we have the new value. In our example what we want is not 7 but the value inside the variable which had been decremented or incremented. To specify incrementation or decrementation, we'll need the operators + and -- Let's take again the example from above.


If you want to decrease the value inside the variable test by 2, you just write

  test:= test--2 #write new value inside test : test--2. Value test decrease by two  

At the opposite, it want to increase the content of the variable by 4 you simply write

  test:= test+ 4 #write new value inside test : test+4. Value test increase by four 

A variation of this is with command set variable. It works also well, but you need to use parenthesis correctly when writing your script

Using the command variable[edit]

The last way of doing this is using the command set variable. Again let's start with something simple. Let's imagine we want to give the variable the value 7. We'll write

 set variable (test, 7)

So in the first part we have the name of the variable (itself linked to a number) and next we have the value. Just like in the previous section we'll have use the variable two times inside the same line and combine it with an operator. We'll need the operators + for increment and -- for decrement

Let's take the same example again.

If you want to decrease the value inside the variable test by 2, you just write

  set variable (test, (test -- 2)) #write new value inside test : test--2. Value test decrease by two  

At the opposite, it want to increase the content of the variable by 4 you simply write

  set variable (test, (test + 4)) #write new value inside test : test+4. Value test increase by four 


As mentioned in the previous section, notice that test -- 2 and test + 4 here are considered as a whole which represents the number 5 and 11. So you need to use parenthesis so that Hspeak.exe to make it understand. You can't simply write :

 set variable (test, test -- 2)

Now we have reviewed all the options to make increase or decrease the content of a variable, you may want to learn how to check its content so that you are sure that the variable has the correct value for what you intend to do.


Checking the actual value with a text box[edit]

The most simple way to check the content of a variable is to make it display is a text box. When you edit the content of text a text box, you may notice that they are a list of lines below. The first three ones are


 ${C0} Leader's name
 ${C#} Hero's name at caterpillar spot 
 ${P#} Hero Name at party slot

The one which interests us is this one

 ${V#} Global plotscript variable ID#

This line means that if we replace # by the number of a global variable, we can actually display its content inside the text box. That's exactly what need. So pick up a free one. Here we chose text bx 15. Write something in it like

The current value of this global variable is : ${V36}


All which is left to do is to make a little script


define script ( 41, check variable content, none)


global variable (36, test)

#----------------------------------------------------
plotscript, check variable content, begin

suspend player
suspend npcs

test:=8
wait (2)
test:= test+ 4
wait (3)
show text box (15)
wait for text box

resume player 
resume npcs

end #end for the script
#----------------------------------------------------

Give this script to an NPC. In the example we chose to increment by four so that make 8+4=12. If you speak to the NPC the number 12 will appear inside the text box.

See Also[edit]