Subtracting Variables: Math Functions

Started by , Tue 20/01/2004 23:40:49

Previous topic - Next topic

AGS Newbie

Hi, everyone ! After consulting the scripting tutorials, I've noticed that functions can be created to add the values of variables together, but nothing about subtraction. In my case, I'm trying to write a script that will allow the subtraction of the value of two variables, and then, the new value displayed on-screen as a " Label " in a GUI.

Example:

int var_one=5;
int var_two=2;
int value_display=0;

Variable One ( var_one ) - Variable Two ( var_two) = value_display ( Variable Three )
( 5-2 = 3 ) ( 3 now being the value of value_display)

Once the value of the two variables are subtracted, the value_display variable holds the value of the completed subtraction. This variable can be used in following script to indicate the current value of something, which is continuously displayed and updated throughout the program.

Any help changing this pseudocode into a working script would be appreciated. Thanks.

Scorpiorus

Here we go:

at the top of room/global script:
//you've already done it :)

int var_one=5;
int var_two=2;
int value_display=0;



then somewhere in script:

value_display = var_one - var_two; // just like add :)

//displaying it on a GUI

//we need a string
string str_Value;

//converting an int value to string
StrFormat(str_Value, "%d", value_display);

//showing it on a GUI named STAT, label number 0:
SetLabelText(STAT, 0, str_Value);

how does it go?

AGS Newbie

Wow, that's exactly it ! Cheers Scorpiorus, that's just as I wanted it. Only one question remains now. The script is tied to the pressing of a GUI button, so that when the button is pressed, the calculation is performed. How does one script it so that upon every press of the button, the calculation is performed again ? The calculation now is made successfully, but pressing the button again does not seem to subtract it further. Is something missing that continously changes the value to value_display to str_Value ?

Gilbert

#3
The reason was that you never change the value of var_one and var_two, so the line "var_display=..." always calculated 5-2 in game, if you want to have it further reduced (and don't need to keep the original value of var_one), you don't even need var_display and modify var_one directly, like:

at the top of room/global script:
//you've already done it

int var_one=5;
int var_two=2;
// comment out declaration of var_display, wont hurt and it's not used any more
// int value_display=0;




then somewhere in script:

value_one = var_one - var_two; // just like add

//displaying it on a GUI

//we need a string
string str_Value;

//converting an int value to string
StrFormat(str_Value, "%d", value_one);

//showing it on a GUI named STAT, label number 0:
SetLabelText(STAT, 0, str_Value);

SMF spam blocked by CleanTalk