Question:
How are games variables programmed?
?
2013-03-24 00:09:07 UTC
There's so many variables in games. What I mean is, what happens in a game is varied like "if" this "then" this is the outcome. For example, you get a certain amount of xp points depending upon how much dmg. you inflicted, how quickly, etc. Or, another example, your weapon does this much damage depending on your stats but also depending on opponents stats. What I'm asking is, how are these variables defined in programming, by mathematics?
Three answers:
Peppy88
2013-03-24 02:24:36 UTC
~ TL; DR ALERT ~

(Sorry for the length of this, but this is not easy to summarise without confusing you.)





Variables are defined differently in different programs, or different languages.



*** Declaration ***

Declaration means defining the variable in your program or script.



For example, in a typical Java/C++ program, you would use either:

= ;

or, if you don't want to assign a value yet,

;



If you had an integer called 'bullets', with a value of 50, it would look like:

int bullets = 50;

Or, if it doesn't have a value:

int bullets;



In something, such as UnityScript (Variant of Javascript used in Unity3D), the declaration would look like:

var : = ;

With the example looking like:

var bullets : int = 50;



In python:

=

bullets = 50



NOTE: From now on, I will be using Java to show you everything else. There may be slight changes in the syntax with different programs, so bear that in mind.





*** Assignments ***



To give a variable a value, you have to assign one to it. When you declare a variable, you usually assign a value with it as well, though you don't have to in some languages.



Usually, assignments are done with the '=' symbol. So, in a typical Java program, if you want to give bullets a different value after declaring it, you would change it by adding a line such as:

bullets = 60;



If you have two variables, say 'bullets' and 'maxClip', and you want to give 'bullets' the value of 'maxClip', it would look like:

bullets = maxClip;





*** Arithmetic Operators ***



Now, you want to add and subtract stuff? Say, if you used a bullet and want to decrease it by one, it would look like:

bullets = bullets - 1;



You can use addition (+), subtraction (-), multiplication (*) or division (/) to perform arithmetic operations, and brackets/parenthesis () to group operations together. For another example, if you wanted to add the bullet by 5, then subtract it by 2:

bullets = bullets + 5 - 2;



...or, if you wanted to add 3 numbers to a variable called 'score' and find the average:

score = (2 + 6 + 4) / 3;





*** Conditional Statements ***



When you want to compare certain values, so certain things will only happen if the criteria is met, you would use conditional statements in the form of 'if' and 'else' statements.



For example, if you only want to lower the variable 'health' if the boolean (Boolean is a data type that has the value 'true' or 'false') 'gotHit' is true:



if (gotHit == true)

{

health = health - 1;

}



(By the way, you use '==' for comparative operations, and '=' for assignments, typically. Certain languages use '=' for everything, it varies.)



That's just the basics, although it extends more into programming than game creation. It is important to understand a bit of programming if you want to know how game variables work, and how games themselves are structured. Hope this helps, and sorry if it's a bit lengthy and/or confusing.



There's a number of links below if you want to read more about it.

.

.
?
2013-03-24 07:15:38 UTC
Ik not sure if I understand your question, but a game variable is defined lile any other. If you want to keep track of experience points you can declare an integer, which can count from zero to approximately 4 billion if its unsigned. Then you write a method or function that is called every time aj action is performed and it adds (or subtracts) xp. When you save and quit the vaariiables are saved by loading from memory and storing to storage like in a file.
?
2013-03-24 07:12:38 UTC
some use a character variable such as if this criteria matches this use this function, some use a luck system aka dice which is an algorithm to randomly decide critical strikes and such they use multipliers to define skill and such


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...