Tag: Making Games
Game Programming Tutorial – Making RPG Games – Chapter 2, Part 4.
by Charles on Sep.25, 2009, under Game Programming
This is the second chapter of the RPG game programming tutorials (click and drag inventory and action). Get the GM source code: right-click, save target as. If you just got here you should start there!
Game Programming tutorial – Making RPG games chapter 2, part 4.
Making games in GML: object oriented game programming.
Many objects besides the main character will make up a game. In this sample, there are the inactive objects that will be detected by the main character. There is the table, which, recognized as such, will cause specific reactions (see previous part). The doors will soon act appropriately using the same method. You previously read that the main character reacts to key prompts (arrow keys for movement and heading, O for renaming, space to see available options)
You can imagine countless options such as other characters to talk to, monsters to shoot, closets to explore, doors or windows to open…
There are the interface objects that react to mouse clicks or hovering (NoButton). Those are the character and inventory objects on the right (they react by creating or destroying the corresponding panels, as well as the objects they contain, by running the open and close inventory scripts), the two topbars and the squares.
And then there are item objects that are created when the right variables are set at the right values (the topbars are as well), and stored in their own inventory slots when they are picked up. That’s all they do for now, but they will soon react to mouse events as well.
Objects are the core of your game. They make the game loop continue. When all object events have caused the assigned actions, the next step begins and the loop is restarted, ready to react to new events by running new actions.
Making scripts run in your game.
We saw that the space key causes a check for close objects, and a variable change if it finds something. This is done by using the if statement and instance_position(). Once that variable is changed, you get reactions by making sure that variable is checked at each step. You do that by assigning that verification in an action (a script or execute a piece of code). This action results from an event that happens at every step. Draw is one of them, and it is the one you must use if what happens next involves drawing text or images. If it is just variable value updates, the Step event will work fine. If you aren’t sure yet, then just use the draw event to be sure.
In the main character draw event, if global.checking_character_options=1 character_options(); launches the script character_options because Space was pressed, causing the if condition here to be true.
Other variables determine if, ultimately, the gun and cookies end up in your inventory, and where. global.just_taken=10; in get_cookies() is a variable used to determine which object was just taken (item 10 is the cookies). This is used in the inventory scripts. First, add_to_inv() will determine what slot it will go in (the first available). Then when the inventory is open, open_inventory() draws the cookies inventory objects at the same coordinates as the square it was assigned to. Since the cookie’s inventory object has a lower depth than the inventory square, it appears on top, giving the illusion that it is inside it.
Arrays are used to do this quickly. This allows you to update values using single or double “for” loops. Take a look at the open_inventory() script in the inventory folder. You’ll see this is extremely useful (you can’t make a decent game without it!)
Post Footer automatically generated by Add Post Footer Plugin for wordpress.
Game Programming Tutorial – RPG Chapter 3.
by Charles on Sep.25, 2009, under Game Programming
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…
Post Footer automatically generated by Add Post Footer Plugin for wordpress.
