Search ObeyBrew.Com

OBEYBREW.COM | Tutorials | A Crash Course In HuC Part 2

A Crash Course In HuC

Part 2 - To Infinity And Beyond!

...well, not quite. This lesson is slightly more advanced than the last because we're going to introduce one of the most essential building blocks of any language: variables.

What You'll Learn

In this lesson, you'll learn about variables and more text output stuff.

What's A Variable?

Simply put, a variable is a place where a quantity can be stored and this quantity can be changed. We use many variables in our everyday life without even knowing it: the time on the clock, the temperature of the oven, the number of times the boss blew his top at the lazy employee, etc. I'm sure you can think of hundreds, if not thousands, of things that are considered variables. So by understanding this concept, variables in computer terms are going to be really easy to understand.

Variable Types

While the price of gas seems to have no upper limit, computer variables do. In HuC, there are two main types of variables. They are known as the char and the int short for character and integer. We could get into all kinds of trouble with bits and bytes right here and most language texts do... and that's exactly where most people get scared off. So in order to keep your interest, we're going to approach this subject from a different angle. For now, we will only care that each variable type can hold a certain quantity.

A char is a small variable type. It can hold a number between 0 and 255.

An int is a large variable type. It can hold a number between -32768 and 32767.

Yes, that's right... an int can hold a negative quantity, sort of like the temperature in Canada. So, if this is all clear to you, let's get into some code and stop the techno-jabber.

Setting Up Our Program

Remember the first crucial part of any HuC program?
#include "huc.h"

main()
{

Creating Variables

Well then, let's put our newfound knowledge of variables to work right away.
int myint;
char mychar;
What have we done aside, from writing two lines of apparently redundant nonsense? Well, what we've done is created two variables, myint and mychar. When you create a variable, first you tell it 'I want to use this kind of variable' and next you tell it 'my baby variable will be named this'. Remember that we have two kinds, ints and chars.

Making Them Worth Something

Variables are pretty useless empty. In fact, the compiler moans like a child who dropped his ice cream if we don't actually do something with them. Fortunately, variables are painfully easy to make worth something. Observe:
myint = 15000;
mychar = 5;
Pretty easy, huh? That's all there is to assigning a value to a variable. And you thought this was going to be hard!

Now, myint is worth 15000 and mychar is worth 5. Great, now what? Well, perhaps we should show the public the results of our hard work, eh? We're going to use a function from the standard library for showing numbers, called put_number. But wait a second... we haven't set up any means of displaying text, so we have to add in our little font stuff from the last lesson before we use put_number. No problem!
set_color_rgb(1, 7, 7, 7);
set_font_color(1, 0);
set_font_pal(0);
load_default_font();
put_number(myint, 5, 0, 0);
Let's take a look at put_number. It takes four arguments. The first argument is put the quantity of this variable on the screen. The second is make sure you put up to this many places of the number. Since 15000 is 5 places, we want to use 5. This is called the width here. If we were to use a 4, the output would be 5000... we don't want to be ripped off, right? Anyway, the third and fourth arguments should look familiar from the last lesson... this is where the output goes on the screen, just like in put_string.

Okay, we have one number on the screen, but we created two variables, right? So, we should put the other on the screen too!
put_number(mychar, 1, 0, 1);
Well, there's our 5. Notice how we only used a width of 1 here... and why not? 5 is only one place so here we can get away with it. But keep in mind that a char can hold a value up to 255, which is 3 places.

Making Variables Be Variable

A variable is pretty useless if we can't change its value. So next, we're going to do a little messing around with our two variables and they'll even work together a bit.
myint = myint + mychar;
Yeah right... what did we do now? Well, simply put, we're just doing some simple addition here. This line says 'here's myint and we're going to assign it a new value, that value will be its current value plus the value in mychar'. So then, let's take a look at what we've got after this:
put_number(myint, 5, 0, 2);

Another Way To Add

myint now holds a value of 15005. Let's mess with it a little more though. I'm going to show you a little bit of C shorthand here.
myint += mychar;
put_number(myint, 5, 0, 3);
Easier to write but maybe not as easy to understand. When we use +=, what we're saying is 'increase the value of this variable with this other value'. In this case, the other value is the value of mychar. So now, we should see 15010 on the screen below the 15005.

Extremely Simple Addition

Now for everyone's favorite C addition technique! Here's the simplest thing you'll ever see when you mess with variables in HuC:
myint++;
put_number(myint, 5, 0, 4);
myint will now hold a value of 15011. 'Huh? Howsatwhysat?' Well, when we use ++ without anything else after it, what happens is the value of the variable is incremented, or increased by 1. This has a lot of uses, as you will discover later in this course.

It Works Both Ways

Finally, let's mess around a little more. Up to this point we've only done addition, so let's do a little subtraction. We're going to use both the long and the short forms for this last example.
myint = mychar - myint;
myint -= mychar;
myint--;
put_number(myint, 6, 0, 5);
Okay, now there's a mouthful, huh? Let's take a look at what we've done here. First, we take mychar and we subtract the value of myint from it. This will give us a very negative number. Next, we subtract mychar's value from myint, giving an even more negative number. Finally, we now decrement myint, giving us a final value of -15012. But, why did we use 6 for put_number's width instead of 5? Well, the sign of a negative number is considered a place here, so we have to use one extra place to display it. Otherwise, we'd simply see 15012, which is incorrect.

Okay then, that just about wraps it up, but we've not finished the function yet... remember how to do it? :)

So... What's Next?

If you have grasped this lesson, you are already halfway there. By understanding variables at the basic level, the rest will easily fall into place. You will utilize variables from here on, because they are essential to programming any program, especially games. After all, how do you think things like high scores, energy, lives, even character positions on the screen are kept? Yep, variables. You should now understand just how important these are and it cannot be stressed enough that you understand how they work.

In the next lesson, we'll look at another core concept of programming: the for loop . We'll also get our feet wet with some basic sprite function. And of course... variables will rear their ugly heads throughout the entire lesson.

Full Program Listing

#include "huc.h"

main()
{
	int myint;
	char mychar;
	myint = 15000;
	mychar = 5;
	set_color_rgb(1, 7, 7, 7);
	set_font_color(1, 0);
	set_font_pal(0);
	load_default_font();
	put_number(myint, 5, 0, 0);
	put_number(mychar, 1, 0, 1);
	myint = myint + mychar;
	put_number(myint, 5, 0, 2);
	myint += mychar;
	put_number(myint, 5, 0, 3);
	myint++;
	put_number(myint, 5, 0, 4);
	myint = mychar - myint;
	myint -= mychar;
	myint--;
	put_number(myint, 6, 0, 5);
}

Sample Output

See Also

A Crash Course In HuC - Part 3 | put_number
This site is ©2013 Eponasoft.