Game Programming: Click and drag, chapter 3
Welcome back! Did I tell you I love comments? Very few people write... Be original :)
Chapter 3 takes you almost all the way through managing the click & drag inventory and actions management.
If you just got here, you should start on the main game programming page. You can get the source code to the game’s development to this point by right-clicking – saving target as here.
The keys to succeeding in doing this are object depth and object duplication. To the player, it will seem like there is a gun object and a cookies object (sample objects). In the game however, there are four different gun and cookie objects: travelling, inventory, equiped and control.
Inventory objects: They are the ones that appear in the inventory. They will stay there when clicked on, and only be destroyed when they are effectively equiped (dragged into the character equiped slot).
Travelling objects: Once an inventory object is selected, and as long as the left mouse button remains pressed, a travelling object is created and follows the cursor.
Equiped objects: If a travelling object exists, and the left mouse button is released over an equiped slot, then the travelling object is destroyed and an equiped object is created at the corresponding slot’s coordinate, in such a way that it will follow if the character panel is moved around.
Control objects: These are the ones that appear in the action panel below. Much like equiped objects, they are dragged there either from the inventory or from the character panel. The difference being that wherever it comes from, the original object will not be destroyed.

4 different objects with same sprites - illusion of same object transfering.
Note: more restrictions will need to be applied later, like not allowing to equip an object that cannot be equiped.
Each of these objects have different depth values. The travelling object has the shallowest, so that it will appear over anything it travels over.
Look for left mouse click events on those objects’ properties, as well as left mouse release events on the control bar and character panel squares. Those events trigger the process of creating and destroying the appropriate object types so that the illusion of transfering an object from one place to the other can take effect.

Left press event triggers travel
Since the first objects to exist are the inventory objects, as they appear in the inventory automatically when they are found in the game interactions, they are the first you will be able to click on once you find them.
Clicking and dragging is done with the left pressed mouse event. As long as the left mouse button is pressed over one of the two objects, that event is happening, and the associated actions are triggered.
Since this isn’t a draw event, it cannot trigger any code that contains any drawing instructions. That is OK, since we only need to do one thing: create the travelling object that corresponds to the one that has been clicked on (gun for gun, cookies for cookies).
Important: it must only do so if the travelling object doesn’t exist yet, so there has to be a control variable:
if instance_exists(gun_travelling)=0
instance_create(global.mouse_x – global.correct_x_travel[20],
global.mouse_y – global.correct_y_travel[20], gun_travelling);
If it didn’t only create it when the gun_travelling object doesn’t exist, it would create a new object at every step until the left button is released, which would get quite messy and bog the game down!
Then the left_released event, over the character or control bar squares (this shows in each of the squares where this is possible), causes the travelling object to be destroyed if it exists, and an equiped object to be created at the square’s location.

Left release kills travel and creates equiped object.
if global.object_travelling[20]=1
{
instance_create(x,y,gun_eq);
global.slot[100]=20;
global.item_equiped[20]=1;
global.object_travelling[20]=0;
global.slot_location_for_item[20]=100;
global.slot[global.slot_location_for_item[20]]=20;
}
for(i=0; i<=500; i+=1) global.object_travelling[i]=0;
A few variables are set for later use: the item is now in slot 100, and slot 100 holds item 20 (the value that corresponds to the water gun). Also, object 20 is no longer travelling.
Later chapters will cover restrictions such as only allowing a hat (or any object specifically meant to be worn on the head) to be equiped on the head slot, only being able to equip equipable objects, triggering events when clicking on an object that has been added to the control panel, using an object in the inventory when right-clicking…
Also, chapters will be shorter from now on so that I can release them more often, and make them easier to learn (locating the code in the new source code).
Have fun!
Most Important Articles So Far
Underworld Reverse Insulin Resistance Personal Project Management Freeware
Free Weight Loss Plan Be More Creative... Play!
Finding Your Passion
There are many more! Better health, creativity, productivity, lifestyle and... games!
Strength | Creativity | Productivity | Lifestyle | Games
First time here? Read about the free software, tools and content that will make you stronger, more creative and more productive on the 2-0 home page!
![]() |
![]() |








Recent Comments