The Dink Network

DinkC Tutorial : Lesson 2

It's finally finished. You can read it in the comments. Happy learning!

DinkC Tutorial : Lesson 2

Lesson 2 has been completed later than I had expected but to make for it this lesson is a little bigger than the previous one. After you complete this you should be able to do any calculations you like and have a firm grasp of variables and what they can be used for.

1) Getting Started

Create a new map square and place 2 sprites on there, doesn’t matter what. Then, create 2 script files : “person1.c” and “person2.c” and attach them to the sprites. There, that wasn’t difficult. Let’s get started.

2) Variables

Variables can be seen as containers of data. In DinkC, variables can only hold numeric values. There are two kinds of variables : Local Variables and Global Variables.

Local variables can only be used in the script they’ve been declared in. Global variables can be used in every script, on every map square. You must be thinking, why not use global variables for everything then? The answer’s quite simple. You can only have around 250 global variables in one game. Now let’s take a look at a variable’s declaration :

Int &var = 2;

This creates a local variable called &var and gives it a value of “2”. The ampersand in front of a variable name is required or it will not be seen as a variable.

*Note : Int stands for integer, which is a variable type. But since there aren’t any other variable types in DinkC, I won’t go into it any deeper.

There are a few limitations when considering a name for your variable though :

- First of all, you can’t use any symbols in your variable name aside from “_” and “-“ and no spaces.

- Your variable name can not be an extension of another variable name. Having a variable called &demon and a variable called &demonic would not work for example. If you do do this, you will always refer to &demon and &demonic can’t be used. This includes the variable names Dink requires by default, such as &gold, &strength, &defense, &magic, etc… You can find the entire list in main.c.

Main.c is also the place where you usually declare global variables. Just add your own at the end of the list.

A global variable is declared in the following way :

Make_global_int(“&global”, 2);

The second parameter “2” is the starting value of the global. You can place any number here, of course.

3) Calculations

Calculations are more work in DinkC than they are in most other programming languages. You can only perform one calculation at a time per line. This would not work :

&total = 3 + &sum + 5;

Instead, you’d have to do this :

&total += 3;

&total += ∑

&total += 5;

There are of course more calculations you can make than just adding things up. These are the different calculations :

+= : Add

-= : Subtract

* : Multiply

/ : divide

= : Equals



4) Sprites and Globals

You can easily store a sprite's ID number in a global, which means that if you refer to the global you refer to the sprite. This allows you to alter any of the sprites properties or make him act from within any script on that screen. Just place this in the target sprite’s script in its main procedure :

&global = &current_sprite;

You can of course do this for multiple sprites using the same global if they are on different screens, since the main procedure is run every time the sprite appears on screen.

5) Example Script

Let’s take a look at an example script and see how all of this can be used. put the following script in “person1.c” :

Void talk (void)

{

freeze(1);

freeze(&current_sprite);

say_stop(“`2Hello there. Are you interested in an experience boost?”, &current_sprite);

say_stop(“Sure. How much will it cost me?”, 1);

say_stop(“`2Give me 200 gold for it and we’ll see how much EXP you’ll gain.”, &current_sprite);

say_stop(“`4Is that even a good deal? You may be better off buying a new weapon.”, &person);

say_stop(“No, I’ll go for the experience!”, 1);

int &temp = &defense;

&temp += &strength;

&temp += &magic;

&temp * 100;

&exp += &temp;

&gold -= 200;

say_stop(“`2You’ve gained &temp experience, according to your stats.”, &current_sprite);

say_stop(“`4You’ve been robbed…”, &person);

unfreeze(1);

unfreeze(&current_sprite);

}


The only relatively new thing in this is the use of the variable &temp in the dialogue itself. You can always display any variable value by doing it this way. Just don’t forget the ampersand, as always.

*Note : The experience that is being added does NOT show up above anyone’s head. If you would want the gained experience to appear above Dink’s head, you’d have to use add_exp(), but we’ll discuss that in a later lesson.

And put this script in “person2.c” so the former script recognizes &person :

Void main (void)

{

&person = &current_sprite;

}


And in main.c (it’s in your story directory) add this line at the bottom of variable declarations :

Make_global_int(“&person”, 0);

There’s one important issue that may prevent your script from working. If the amount of experience leads to a level up, the level up script will show up and prematurely interfere in your script which can have unpredictable results… Therefore, it would be a better idea to add the experience at the end of the script, right before the unfreeze commands.

That brings us to the end of this tutorial. Next time we’ll handle choices and if() statements.