Variables & Types in VC

What is a variable?

When running any program, certain information need to be kept track of by the computer. A variable is basically a piece of memory that remembers a particular value you put in it, and can be changed by parts of your code. As an example, most RPGs will want to know a health value that changes when the player is damaged or healed. So if you wanted to track a health variable, you can declare a variable called "player_health", which can store a number that relates to their health. You then use the name of the variable to refer to it.

Types of variables

VC, like many programming languages, has a several different means of storing data, each for different purposes. These types each have a subsection, with more information:

  • Integer (int): Used for storing numbers. Note that VC doesn't have direct support for decimal numbers with fractional parts. If you want to store fractions inside of integers, please read this article on fixed-point numbers, which is one solution to this problem.
  • String (string, str): Used for storing text, whether a single character or a whole line.
  • Array (type[size]): A certain number of either strings or integers, listed under one name.
  • Struct: A collection that can have arrays, strings, and integers, arranged and named however you want.

Declaration

Declaring a variable is creating it. You cannot use a variable without first letting VC know about that variable (both what it is, and what it's called). In VC, the syntax for declaring a new variable is as follows:

Type variable_name;

where Type is a valid variable type like "int", "string" or "Monster", and variable_name is the name you want to give it.

So, in our example, we would write the following line of code:

int player_health;

This says we want to reserve a piece of memory to remember a number, which we are going to refer to as "player_health" from now on. Arrays are handled rather differently, see their specific sections for more information, but strings and structs are similar:

string player_name;
Monster baddie;

What we must do is make sure we declare the variable before using it anywhere else. This generally means you just need to make sure the line you declare the variable is above any lines in which you use it. This is good:

int playerhealth;
playerhealth = 100;

This is BAD, verge will not like it:

playerhealth = 100;
int playerhealth;

Note we could also assign a value when we first declare it, this sometimes saves space and confusion:

int playerhealth = 100;

Unlike other programming languages, verge automatically sets numbers to 0 and strings to blank (or "" as it may be referred to), even if you don't assign a value when you first declare it. However, it is considered good practice to make sure you variables always contain something before you use them.

Naming variables

You can call your variable pretty much anything you like, but it's best to name them something obvious that will remind you what information it stores. Verge allows variable names to consist of upper and lower case letters, numbers, and underscores (like _ that). ( Well, to be honest it allows all symbols that aren't reserved as something else, but don't use them. ) However, make sure the name always begins with a letter.

Bad variable names:

hit%    //No special symbols like % allowed
3DModel //Putting a number at the start will make verge try to do maths on your variable)
player$ //(Ok, so the $ is technically allowed, but will cause confusion)
estbx   //(This is allowable, BUT WHAT DOES IT MEAN? Use names that make sense)

Good alternatives:

HitPercent
Model3D
playerMoney
enemy_status_box

Another important thing to note here, is that you need to make sure all things that have names, and might exist at the same time (see scope below) must be called different things. In particular, don't name variables and functions the same, and (for a different reason) don't name ANYTHING the same as defines. A final point here is that unlike in some programming languages, upper and lower case letters are not treated is differently, so make sure to have completely independent names.

Having some kind standard system is useful to keep track of what each name is, however exactly what sort of standard system is the sort of thing programmers can argue over for hours. If you are brave, do a search and pick a method you like. However, I'll give a few guidelines at the bottom of the page that might be useful. Remember, these are not RULES, just SUGGESTIONS.

Scope (Global/Local)

In verge, a variable can be either local or global. This is related to whether a variable is declared inside or outside a function.

A local variable is declared inside a function, will only exist for the duration of the function it is in, and will be created every function call. A global variable is declared outside a function, and will exist for the duration of the program itself.

int player_hp = 0; // Global variable

void LogHitPoints()
{
    str message; // Local variable
    message = "You have " + str(player_hp) + " hit point(s)!";
    Log(message);
}

Also, remember, each variable needs a certain amount of computer memory to store it, so even though that's not so much of a problem with modern machines, do try to keep global variables to a minimum. However, sadly, at the moment ALL arrays and structs MUST be global, they cannot be declared locally.

Also, scope effects how variable can be named as well. So, you can have to entirely different local "x" variables in two different functions, but you cannot have a global "x" and then have another "x" variable anywhere else at all (global or local).

Pointers

If you do not know what pointers are, skip the next section: you're done with this one. ;)

Verge, for all intents and purposes, has no pointers. As such. However, to understand how it handles images, sounds, entities and suchlike, it's useful to know what it is doing. When you use a Load...() function, Verge assigns what ever you just loaded some space in memory, and records where it is located. It then assigns a number to that location, and gives it back to you. That number can be later used to tell verge which image you want it to use in a function. It's just like saying 1st, 2nd, 3rd, 4th image and so on - Verge removes all of the memory management worries from your shoulders.

However, if you simply must have direct memory access, you can, via a very arcane system, directly access memory. To read more on v3's DMA, go here.

Talkback

Post a new comment?

Talkback #1 written by verge-rpg.com on 2014-04-17.

This is the talkback thread for the documentation page: Variables & Types in VC

Post a new comment?

Doc Nav

Your docs
View All Docs

If you log in, you can edit the documentation, or create your own documents and tutorials!

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.