How to Make an RPG
In this tutorial, you’ll learn how to use pre-built frameworks to create your own reusable RPG engine. By the time you’re done, you’ll have the groundwork to create the RPG of your dreams! By .
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
How to Make an RPG
45 mins
From Room to Room
Since the game uses a base tile map, switching between rooms is a simple matter of swapping out the tile map from under the character when they have walked over a certain area such as a door or a staircase.
Before you can switch to another area, though, you need to add two things:
- You need to define the location of the exit on the tile map;
- You need to build another area for your player to enter.
First, create an exit. Open room.tmx in the Tiled map editor. Below the Layers section, click the button to make a new Layer, and choose Add Object Layer, as shown in the screenshot below:
Note that this is different than the existing exit layer (a normal Tile Layer), because this new layer is an Object Layer. Object Layers allow you to define areas in the map with key/value properties that you can then easily retrieve in code. This is ideal for defining the area of the map where the exit is and defining where it should warp the player to.
Select the Insert Rectangle tool and draw a square over the stairs at the bottom of the room.
Now right-click on the square and select Object Properties. Set both the name and type to exit and add the following name/value pairs to the list of properties below:
- destination: town
- startx: 3
- starty: 15
The destination specifies the name of the tile map that you want to load when the user steps onto this exit. The startx and starty properties specify the tile into which the user will spawn when switching between rooms. This is important to show that the user is coming from a door and not just randomly spawning on the map.
Save room.tmx. Now to step two – building another area for your player to enter.
Lucky for you, the town.tmx file is already included in the project. Open it up in Tiled and have a look around, but don’t make any changes to it yet!
Remember that tile coordinate (0,0) in Tiled is the bottom left of the map. So coordinate (3, 15) is the area right below the door – count it out for yourself to see.
Warp Speed Ahead!
Now that you have an exit defined and another room for the user to enter, you need to add some code to check when the hero hits this spot, and transition to a new map.
First, since the exits layer is an object group, you need to define a CCTMXObjectGroup in order to access it. Back in Xcode, open GameLayer.m and in the private interface at the top of the file, add the following line:
@property (nonatomic, strong) CCTMXObjectGroup *exitGroup;
Next add the following line to the bottom of the loadMapNamed method:
self.exitGroup = [self.tileMap objectGroupNamed:@"exits"];
This gives you a handy reference to the new exits object layer that you created in the Tile Map. Now you just need to detect when the user steps on those exits.
Add the following code to the heroIsDoneWalking method:
// 1
NSArray *exits = self.exitGroup.objects;
for(NSDictionary *exit in exits)
{
// 2
CGRect exitRect = CGRectMake([exit[@"x"] floatValue], [exit[@"y"] floatValue],
[exit[@"width"] floatValue], [exit[@"height"] floatValue]);
// 3
if(CGRectContainsPoint(exitRect, self.hero.position))
{
// 4
NSString *name = exit[@"destination"];
CGPoint heroPoint = CGPointMake([exit[@"startx"] floatValue] * self.tileSize + (self.tileSize/2), [exit[@"starty"] floatValue] * self.tileSize + (self.tileSize/2));
self.hero.position = heroPoint;
[self loadMapNamed:name];
return;
}
}
Here’s what you are doing:
- Enumerate through the exits in the exit group.
- Convert the exit’s frame (the rectangle you drew) to a CGRect.
- Check to see if the player is standing in the exit.
- If so, fetch the name of the room and the new position for the player. Load the new room and update the player’s position.
Build and run the project. You should now be able to walk out of the door and into the town. Congratulations, you’re no longer a recluse!
Forever Alone No More!
When I first started developing games, the idea of NPCs boggled my mind. How did developers build so many characters capable of such complex interactions, particularly when it came to questing?
Well, I’ve come up with a rather simple and robust method for doing just that.
You see, when you are writing gameplay logic like this, it’s often easier to do so in a lightweight scripting language than a strongly typed language like Objective-C. It makes your code much faster to write and tweak, which is critical when you’re changing things a lot like you will when creating NPCs, quests, and so on.
So in this part I am going to show you how to integrate a scripting language called Lua into your game. As I mentioned earlier, Lua is ideal for this purpose, and is really easy to integrate into Xcode projects.
Here’s how it will work. You’ll add NPCs to the tile map inside of Tiled and give each of them a name. When your character approaches an NPC, the game will invoke the NPC’s Lua code. You’ll be building a simple bridge so that your Lua code can call upon your Objective-C code to do things like store key/value pairs, display chat boxes, and so on.
With this system in place, you can easily script an entire game with minimal changes to your engine.
All About Lua
Before you begin, let’s take a moment to discuss how the Lua integration will work at a high level.
Lua is a very high level scriping language made to run in just about any environment. It only has one complex type (called a table) which can be used to build high level data structures such as arrays, hashes, objects, etc. You can learn more about Lua on their official website.
Lua ships as a bunch of .c and .h files for it’s runtime that get compiled directly into your project. In order to get Objective-C and Lua talking, you are going to be using an open source class called LuaObjBridge. Although it’s no longer maintained, it will work perfectly for your purposes.
What this class does is makes use of Objective-C’s reflection ability to dynamically “link up” objective-c classes/methods/properties to the Lua runtime. This gives you the ability to call upon them using a specific syntax (to be discussed later).
For your game, you are going to be scripting the entire storyline (which is pretty shallow) and all of the interaction between the player and the NPCs.