DS Game Maker Logo
Home Icon

Introduction to Logic and Programmatical concepts

Introduction

This guide will help you familiarise yourself with the logic behind computer games, how the computer sees your actions and how to make some clever games in DS Game Maker.

Of course, logic is what makes games work. Games have to think for themselves and respond based on user input. Some logic methods are very simple if the game play is limited, and some are dynamic, when the player has many options and routes to go down in the hierarchy of the game.

This guide will teach you little about DS Game Maker itself (the interface, sprites, backgrounds): You can see the Beginners Tutorial for that. Instead, this deals mainly with actions, their arguments, structuring and ordering, as well as variables, handling data, program efficiency and keeping your logic usable. These concepts are easily applied to programming itself, thus it is important to remember that creating your list of actions in DS Game Maker is rather like programming, just more visual, and for most, easier to work with.

Commenting

This is the most important element to master when creating games, and more so with advanced logic. Commenting is like explaining what your game does, as you add actions. This way, if you need to make changes, you can go back and understand how you constructed something without difficulty. To demonstrate this, take the following set of actions:

Set Variable x;y
Set Variable x1;x
Draw Variable z
If y == x
   z = (x * y) + (y / 2) * z
End If

It is impossible for anyone else to understand what the above does.

The action which is used for commenting is ---. There is a reason why the action is named so: It is not designed to look like any other action so that it is no confused with an action itself. The action has just 1 argument, which is the comment that it displays in the action list. Consider the following example of how the Comment action is very useful:

--- 'x' will hold the Players current weapon
Declare Global x;u8;0
If x == 0
   --- Super blaster weapon
End If
If x == 1
   --- Ray Gun
End If
If x == 2
   --- Silly Swobbler Gun
End If

Once you begin creating larger games and demonstrating technical ability, it is essential that you comment what your logic does. It will help you understand, modify and possibly fix it in the future. If you need to ask for help on the forums, the commenting will be most useful to those who will be of assistance to you.

Variables

There are 2 types of data: constant and variable. Constant data never changes in value, for example, the numbers 20, 100, 9. You can input these numbers into actions, but it is limiting. Sometimes you do not know exactly what number to put in because of how the game can alter as a result of the player's choices.

Variable data is of course the solution, and it closely resembles that found in Algebra. So, instead of entering the numbers 32, 49 into an action, you may use X and Y. In this way, letters or names are used to express a piece of data that can change in value as the game runs. A piece of data that can be changed and is represented by a letter or name is called a Variable.

As the game runs, at any point you can change the value of a variable. You can change the value to the value of another variable, a Sprite location, or just a plain constant number. At other points in the game you will most likely use the variable and perform action based upon it. For example, you may check at the end of the level whether the score is worthy of unlocking the next level. The Score, of course, is a variable, because it is not constant, it changes.

There are 3 important things about a variable which you should remember:

  • Name - What the variable is called and how you represent it.
  • Type - What kind of information the variable holds.
  • Scope - What other elements in the game can use and modify the variable.

Variable Names

A Variable can have any name you choose. It can have a long or short name, completely at your discretion. Long names such as HowManyChairs are easy to understand but short names may be more useful in complicated logic, for example, X, Y Z.

When naming a variable, you must consider the following:

  • No spaces. "My name" is not valid but "MyName" and "My_Name" are.
  • Do not use symbols in the variable name, except the Underscore. "Dog&Bone" is not valid.
  • Do not begin a variable name with a number. "1Way" is not valid. "W1ay" and "Way1" however work fine.
  • Avoid programming terms like 'main', 'int' or 'for' as variable names.
  • Simple variable names are fine, e.g. "x" and "i", but if you are going to make a big game it may become confusing what the variables do and hold.
  • Do not make variable names longer than 32 characters. "ThisIsNotALongVariableName" is still less than 32 characters. Of course the shortest length a name can be is 1 character.
  • Variable names must be unique. You cannot have 2 variables with the same name: How would the computer know the difference between them when they are only identified by name?

A variables name has to be unique. You cannot have more than 1 variable with the same name, even if they have different types. If you do something like the following, you will get an error:

Declare Global x;int;0
Output Text 0;0;0;Hello
Declare Global x;boolean;true

Variable Types

The 'Type' of a variable decides what sort of data it can hold. There are a few types of variables which are useful to know:

  • int. Mathematically known as an integer - a whole number that can be positive and negative. This is the type of variable you will use most often.
  • bool. Yes or No, True or False is what a boolean holds. It does not hold a numeric value. It uses very little memory. So for example you should use a bool instead of "0" or "1" in an int.
  • s8. A number that ranges from -128 to 127. You should use this instead of an integer if you have a small number in order to save memory and make the game faster.
  • u8. A number that ranges from 0 to 255. Similar to the above but with a wider range and therefore no negative possibility.

Variable Scopes

In very basic terms, the scope of a variable is visibility and accessibility to other rooms within the game.

For example, in the overall game, it is important to note how many points the player has scored all together (i.e. over all rooms and all levels). A variable with a much smaller scope is Boss' health: This does not affect other levels and therefore the scope will be to the specific level only. Other rooms will not be able to access the Boss' health.

In DS Game Maker, a variable can have one of the following scopes, determining how other rooms can access it:

  • Global. A global variable can be accessed and modified by all rooms. Example: Player score. This variable needs to be accessible by all levels.
  • Room Global. This variables is accessible only within the room in which it is declared. This means you can have one 'Boss Health' variable per room and they will not interfere with each other.

In most cases, the most useful scope of variable is Room Global. It is the most common scope of variable because you can have multiple variables of the same name in your game in different rooms (e.g. an individual score variable per level).

'Declaring' a Variable

To begin using a variable, you need to Declare it. When you declare a variable you are telling the computer that it needs to allocate memory to store something under the name of that variable. Once your variable has been declared, you may modify and display it. To declare a Room Global variable, use the Declare Room Global action, as follows:

Declare Room Global TotalCollectedDiamonds;int;0

The Declare (Room) Global actions have 3 arguments and are identical apart from their scope. The arguments are as follows:

  • Variable Name, described above.
  • Variable Type, described above.
  • Default Value. When you declare a variable, you must give it a default value so that you can use it straight away. Examples of default values are:
    • -100, 0, 49, 45, 67 for an s8.
    • 32, 64, 48, 19, 34 for a u8.
    • true or false for a bool.

Examples of declaring variables follow:

Declare Room Global FrogsInBasket;u8;0
Declare Room Global IsANaughtyPlayer;bool;false
Declare Room Global ScoreOffset;s8;0

Now that you have declared a variable, you may use the other actions which deal with variables. Note that you may insert the Declare Variable actions anywhere in your Rooms action list but it is recommended that you place them at the top to keep the list organized.

Changing a Variable

To change a variables value, use the Set Variable action. The action has 2 arguments:

  • Variable Name. The name of the variable of which to set the value. It must be Declared. If the variable is a Global variable you can set its value as such from any room. If it is a Room Global variable you may only use the Set Variable action on the variable in the room in which it is declared.
  • Variable Value. Nothing illusive here: This is the same as the Default Value in the Declare (Room) Global action. It must fit the Type of the variable. For example a bool will require true or false in this argument but an int would require a number.

Consider the following example of setting a variable:

Declare Room Global PlayerSweets;int;10
--- Player has 10 sweets.
Set Variable PlayerSweets;20
--- Player has 20 sweets.

Manipulating Variables

You can use Mathematical Operators to change the values of Variables. The 4 basic mathematical operators are *, +, - and / . The following examples demonstrate how you should use them:

Declare Global Myage;int;15
Declare Global MyAgeIn1YearsTime;int;0
Set Variable MyAgeIn1YearsTime;MyAge+1
--- The above is dynamic. You can change MyAge to whatever
value and the second variable will become MyAge + 1
Declare Global MyAge;int;15
Declare Room Global MyAgeNotReally
Set Variable MyAgeNotReally;MyAge * 2
--- MyAgeNotReally will be 30, or double the default
value of MyAge

You can also simply copy variables without using an operator:

Declare Room Global Age;int;15
Declare Room Gloal BackupAge;int;0
Set Variable BackupAge;Age
Set Variable Age;100
--- Whoops - James' age isn't 100, but we can still use his
real age because we copied it to BackupAge.

If

Variables become useful when they become part of your game logic. We saw an example of some variable logic in the Commenting section of this guide. The most basic logic uses the If action and performs blocks of actions based on the results. The following example shows some basic logic:

Declare Room Global TotalLabels;u8;0
If TotalLabels == 0
   Output Text 0;0;0;My, you have no labels!
End If
If TotalLabels > 10
   Output Text 0;0;0;Wow, so many labels!
End If
If TotalLabels == 79
   Output Text 0;0;0;You have seventy nine labels
   Else
   Output Text 0;0;0;You don't have seventy nine labels
End If

When using the If action, it is key to note there are three important parts of the condition:

  • The Variable Name that you are checking the value of.
  • Method of Comparison you are using to see how the variable relates to the Value you are looking for. The main 3 are explained below:
    • == - If a variable definetly equals the value.
    • > - If a variable is greater than the value.
    • < - If a variable is less than the value.
  • The Value you are comparing against.

If the condition is true, for example, MyAge < 16 or MyAge == 15, the program executes the block of actions below the If until it reaches an End If action. It is vitally important to use an End If action. If you do not then the computer will not know when to categorise the actions as: i) Actions to happen if the If is true, ii) Actions to happen as another part of the game logic.

Else

Using the Else action executes the next block of actions until End If when the Ifs condition is not satisfied. For example:

If Myage == 15
   Output Text 0;0;0;You may be James
Else
   Output Text 0;0;0;You're not James
End If

Actions between Else -> End If run if the If is not satisfied: It does not take into account any values. Simple as, if MyAge is *not* 15, then what comes after Else will definetly run.

Here is an example of some logic using a Boolean variable:

Declare Room Global hasgun;bool;false
--- Add some actions here to detect if the user has a gun
If hasgun == true
   Output Text 0;0;0;You have a gun.
Else
   Output Text 0;0;0;You have anything but a gun.
End If

Combining conditions with Logical Operators

Consider the following block of actions. They work:

If MyAge == 15
   If MyShoeSize == 100
      If IsListening == true
         --- You might be James (James don't know his shoe size)
      End If
   End If
End If

You can actually combine conditions to produce the following:

If MyAge == 15 && MyShoeSize == 100 && IsListening == true
   --- You might be James (James don't know his shoe size)
End If

You can also perform an "OR" like so, using ||:

If Count == 32 || Count == 64
   --- Power of 2
End If
If Rainy == true || Snowy == true
   Set Variable CanGoOut;false
End If

Random Numbers

Associated with Artificial Intelligence, Random Numbers personalize game play and make it different every time. In a simulation game, you may want the player to start with a random amount of money, and in a driving game, you may want the car to have a random fuel depletion rate. These are both easily achieved.

This section uses PAlib functions - nothing to worry about, but if you're interested in what other functions from PAlib you can use inside your games, check out PAlib Documentation!

Random Bingo Number Predictor

First, we'll start by creating a variable to store our bingo number and telling the player what to do:

Declare Global BingoNumber;int;0
Output Text 0;1;1;Press A to get a number

Now we'll add in some Button detection:

If Stylus.Newpress
   --- Bingo time
End If

Let's get the program to draw our number:

Draw Variable 0;5;5;BingoNumber

Finally, add the random number generation, in between the If Stylus.Newpress and End If:

If Stylus.Newpress
   --- Bingo time
   Set Variable BingoNumber;PA_RandMinMax(1,50)
End If

The final assembled code looks as follows:

Declare Global BingoNumber;int;0
Output Text 0;1;1;Press A to get a number
If Stylus.Newpress
   --- Bingo time
   Set Variable BingoNumber;PA_RandMinMax(1, 50)
End If
Draw Variable 0;5;5;BingoNumber

What the code above does is set the variable BingoNumber to the result of the function PA_RandMinMax(1, 50), which gives a random number between 1 and 50 inclusive.

The function PA_RandMinMax has 2 arguments like actions: The lower boundary and the higher boundary for the random number. The amount of variation that can occur by using this simple function is enormous: For example, random scoring, random number of enemies, random messages from a character when you approach them.

Random Sprite Location

In this example, we will move a Sprite to a random location when the A button is pressed.

To begin, let's add some input detection:

If Button Press Newpress;A
   --- Do something exciting
End If

Now we will work out the logic required to set the location of the Sprite to random numbers:

Set Sprite X 0;0;PA_RandMinMax(0, 160)
Set Sprite Y 0;0;PA_RandMinMax(0, 224)
The finished actions list:
If Button Press Newpress;A
   --- Do something exciting
Set Sprite X 0;0;PA_RandMinMax(0, 160)
Set Sprite Y 0;0;PA_RandMinMax(0, 224)
End If

Loops

"Iteration: (computer science) a single execution of a set of instructions that are to be repeated; "the solution took hundreds of iterations".

In a computer program, you often need to perform a series of commands more than once. To do this, you do not need to add this block of actions many many times. Instead, it is possible to use a Loop. You can use a Loop to perform a block of actions a 100 or even a 1000 times.

A Loop also uses an iterator variable, and you can use this inside your block of actions to see how much of the loop has completed (the iteration the loop is on).

The Loop action in DS Game Maker has 3 actions:

  • Variable - The variable you use in the block of actions.
  • Start - The number from which to start looping.
  • End - The number to stop looping at.

The End number should be larger than the Start number. A Loop structure requires an End Loop action.

A Simple loop

This very simple loop requires just 3 actions.

Loop i;0;4
   Draw Variable 1;i;i;Big Brown Fox
End Loop

This loop will produce the following:

Big Brown Fox
 Big Brown Fox
  Big Brown Fox
   Big Brown Fox
    Big Brown Fox

Simple Loop with Timing

In this example we are going to use a Loop inside a Loop to create delayed revealing of the above. We will begin with the above example:

Loop i;0;4
   Draw Variable 1;i;i;Big Brown Fox
End Loop

We want the Nintendo DS to wait 1 second before outputting the next 'Big Brown Fox'. To get the DS to wait 1 second we must wait exactly 60 frames as this is the speed at which the DS' processor runs. To perform our wait we will insert the following loop before the End Loop action above:

Loop x;0;59
   Wait For VBL
End Loop

The final assembled code looks as follows:

Loop i;0;4
   Draw Variable 1;i;i;Big Brown Fox
   Loop x;0;59
      Wait For VBL
   End Loop
End Loop

Loop with Randoms and Sprites

To prove just how useful Loops really are, here is an example which will create a nice graphical effect.

Plot 10 ball sprites on the top DS screen for this example.

We will begin by looping from 0 to 10 so that our actions can apply to all Sprites on the screen:

Loop i;0;10
   --- Do something exciting
End Loop

Now we will add 2 actions to set the location of the current Sprite to random numbers:

Loop i;0;10
Set Sprite X 1;i;PA_RandMinMax(0, 224)
Set Sprite Y 1;i;PA_RandMinMax(0, 160)
End Loop

Finally we must add a waiting Loop so the above does not happen too fast for us to see it:

Loop x;0;30
   Wait For VBL
End Loop

The final assembled logic is so:

Loop i;0;10
   Set Sprite X 1;i;PA_RandMinMax(0, 224)
   Set Sprite Y 1;i;PA_RandMinMax(0, 160)
End Loop
Loop x;0;30
   Wait For VBL
End Loop

Optimization

TODO!

Making it: Balloon Popper

TODO!

By James Garner

© 2008-2010 James Garner. 'DS Game Maker', the 'DS Game Maker logo' & 'Invisionsoft' are pending trademarks of James Garner.

'Nintendo', 'DS', 'DSi' and 'DSi XL' are registered trademarks of Nintendo Co., Ltd.

Pro | Homebrew Kit | Website Use Statement | Privacy | Competitions

mail@dsgamemaker.com

Contact Us