# Game Maker Programming/Tough Topics

The purpose of this module is to allow more experienced students of Game Maker to write about how they solved specific problems for the benefit of others who might be struggling with the same (or similar) problem.
Tough Topics are not necessarily advanced topics. They can be anything that took you a long time to figure out, regardless of how basic or advanced the feature or operation.

# Please Contribute

If something about Game Maker or GML has stumped you for a while, why not write about it here?

## Structure

As with all wikibooks, the structure of this module will undoubtedly evolve over time. Experience has shown that people generally do better when given a sample structure to follow, with the freedom to invent any structure they wish. Over time, users will modify the structure of any page to what works best. As a help, here is a sample structure to follow for Tough Topics:
   What I was attempting to do:
   What did not work:
   What worked:
As already stated, feel free to use any structure that works for you.

# Concepts

This is not intended to be a rewrite of the Game Maker Manual. But there are some Tough Topics that new (and experienced) programmers have problems with. Concepts that are confusing or difficult to grasp should be described here.

## The Lingo
### Objects and Instances

Programs (games) created with Game Maker are designed with the principles of "Object Oriented Programming".
The three pillars of Object Oriented design are polymorphism, encapsulation, and inheritance, which are topics for another wiki page. However, all of these ideas are made possible by the idea of organizing the design of a complex program into "Objects". From an object, instances of that object are created and used base on the object's defined methods and properties.
Put quite simply, objects are basically a template for instances that are created from that template. Items that you see on screen while playing a game, for example, are "instances" of those objects.
An object can be thought of as a template or a recipe for how to create an instance. In general object oriented programming, an object has methods and properties. Method are the actions that an object can perform. If our object is a car, it would have methods like:
Each of these methods would be accompanied by a method definition, explaining in detail the steps required for the object to perform that action.
The properties of an object are simply descriptions of the object. For example

### OBJECT DEFINITION OF A CAR

So when we define the object "car" we would say, "Let there be an object of type 'car'. Each car has its own color, size, year, make, and model. Furthermore, each car can start, speed up, slow down, turn left, turn right, and back up".

### CREATING INSTANCES OF OBJECT "CAR"

Here is where we start using the object in our world. To create an instance of a car, we say "Give me a car. The car will be red, the car will be small in size, and from 1982. Call the make of this car 'mycarcompany', and call the model of this car 'SuperSpeedy'".
We would be handed a car with the desciprtion given, and would now have generated one instance of the object "car". Since all cars are created from the car object, the car would also come with the pre-defined ability to start, speed up, slow down, turn right, turn left, and back up, as defined in the "car" object (template).
Now we could say, "Give me a car. The car will be blue, the car will be large in size, and from 2010. Call the make of this car 'SuperCompany', and call the model of this car 'FasterCarNumberOne'".
At this point, we would have two INSTANCES of the OBJECT "car". The term "instance" comes from the fact that we are instanciating the object "car".
Another way of saying this is that from the recipe (object) of how to make a car, we have created (instanciated) two cars; we now have two instances of the object "car".
Advanced: Each instance is given its own instance ID when created. Using an instance's unique ID gives the designer the ability to directly reference a particular instance of the object, for example, when many instances have been created and there is a desire to reference just one of them, instead of all the instances that were created from the object.

### Action or Event?

In the Game Maker program, Events are displayed in the center of the object Properties box. Actions are dragged and dropped into the Events of the object.
An event is really a message that is given to an instance of an object in the game. Whenever the event takes place the actions that you placed in the event are performed. This produces the behavior for the instances of the object.

# Common Problems
## Problems with Game Maker Program

This section will deal with some problems people have when learning to use Game Maker. Error messages might also be discussed in this section.

### My computer locks up, hangs, or freezes

* Are you using a while statement anywhere?
    While statement is a common cause for system hangs. If the while loop does not finish (infinite loop) the computer will freeze up.

### I click on an icon in the Object Properties, but nothing happens

* Icons on the right side of the Object Properties menu are meant to be dragged and dropped into an Event.
* Right-clicking on an icon will also add it to the Event.

## Problems with Objects
### Object Getting Stuck

Lots of times a moving object gets "stuck" on a wall or other non-moving object.
* For moving items, set the "solid" flag to false.
* For stationary items, set the "solid" flag to true.
* (Platformer Only) For the moving object under the collision event with the wall check if there is an object(wall) just under the moving object, if so set the vspeed to zero.
* (Platformer Only) Now check if there is an object(wall) just left of the moving object, if so set the hspeed to zero, do the same if there is an object(wall) just right of the moving object.

### Object Falls Through Floor

Moving items should not be set to solid. The problem is that non-solid objects tend to fall through the floor, or sometimes pass through walls when you don't want them to.
* Create a non-transparent sprite either the same size or a little smaller than the object.
* In the object properties box go to ‘Mask’ and choose that sprite.

### Object Appears in the Upper Left Corner of Room

* Make sure the object's "Relative" flag is set to true.

## Problems with variables
### Strings and Numbers

Whenever I want to display a message, concatenate strings or add variables, I get the message, "Wrong type of arguments to +."
* Make sure you're not adding a string to a number and vice versa in the "Set Variable" dialogue box.

## Step Event Problems

Whatever is in the Step Event will happen every step. So be careful what you put in there.
Do NOT put these in the Step Event (or at least be very careful if you do...):
* Timelines
* Drawing Sprites
* Sounds
* Creating Instances
In order to avoid issues that can happen by using the following actions in Step Event, you may want to add a chance action. If you do not take precaution when you test with the following actions, It can cause lag. And if severe enough, can crash your computer.

## Drawing Problems
### Object is not visible

What might be happening: When you create a new object, Game Maker draws the sprite you choose to represent the object. However, whenever you add a Draw Event to the object, any action you put in the Draw Event overrides the normal drawing for that object.
* Make sure the "visible" flag is set to true.
* The Draw a Sprite action must be located in the 'Draw Event'.
    or
* A script in the 'Draw Event' must have the function:
    draw_sprite(sprite_index,image_single,x,y)

### Text not visible

* The First Rule of drawing text:
    There are only TWO places you can put DRAW actions.
    * The DRAW event (draw actions simply ignored elsewhere)
    * A SCRIPT (but NOT a "Execute a piece of code" action, it too, must be located in the DRAW event)
* The Second Rule of drawing text:
    Only drawn when the instance is flagged as VISIBLE

## Mouse and Keyboard Problems

* What's the difference between: Left Button, Left Button Press, and Left Button Release?
    Left Button - action keeps going as long as button held down.
    Left Button Pressed - happens once only.
    Left Button Release - happens one time only when button released.
* When I click my mouse to end the game, it frequently (or always) activates something on my Windows desktop.
    Set your End Game action to be activated by a mouse button Release. That way your click won't be passed to the desktop as your Game closes.
* I want to use my mouse to do something when I right click anywhere on the screen, but nothing happens!
    Make sure you set your mouse click Event to Global mouse.

## External links

If a website treats Game Maker issues from a problem solving standpoint (as opposed to tutorials, etc.) then please add a link.
* Game Maker Community - Great site! When the server isn't busy.
* GM Knowledge Base
* Yahn's GML