![]() | |
PC Games
• Orb Tutorials
• 2D shoot 'em up Latest Updates
SDL2 Quest game tutorial
SDL2 Versus game tutorial
Download keys for SDL2 tutorials on itch.io
The Legend of Edgar 1.37
SDL2 Santa game tutorial 🎅
Tags • android (3) • battle-for-the-solar-system (10) • blob-wars (10) • brexit (1) • code (6) • edgar (9) • games (44) • lasagne-monsters (1) • making-of (5) • match3 (1) • numberblocksonline (1) • orb (2) • site (1) • tanx (4) • three-guys (3) • three-guys-apocalypse (3) • tutorials (18) • water-closet (4) Books ![]() A North-South Divide For over a hundred years, messenger Duncan has wandered the world, searching for the missing pieces of an amulet that will rid him of his curse; a curse that has burdened him with an extreme intolerance of the cold, an unnaturally long life, and the despair of watching all he knew and loved become lost to the ravages of time. But now, Duncan is close to the end of his long quest. |
— Simple 2D quest game — Note: this tutorial assumes knowledge of C, as well as prior tutorials.
Introduction The final quest type that we're going to implement is to dispose of an item. As mentioned in the previous part, this is like a fetch quest, but in reverse! This quest is also easy to create, since we already have most of the essential code pieces needed to support it. Extract the archive, run cmake CMakeLists.txt, followed by make, and then use ./quest16 to run the code. As before, you can move around with the WASD control scheme. Play the game as normal. Every now again, a Resident will request that you bury an item on an island somewhere. Head to the island, and search around until you find an X on the ground. Walk into it to dispose of the item (it will be removed from your inventory). With that done, return to the Resident to complete the quest (the quest log can help you locate the town that you need to return to). When you're finished, close the window to exit. Inspecting the code As already stated, this will be a very simple quest to put together, as we already have most of everything we need. We'll be adding one new entity, but otherwise the bulk of the work will be done in quests.c. So, starting with generateQuest:
As we're only supporting dispose quests in this part, we've set the first command as START_DISPOSE_QUEST. We then update executeQuestSteps, to support this new command:
When we encounter the START_DISPOSE_QUEST command, we're calling doStartDisposeQuest:
You're right in thinking that this looks kind of like a mix of the delivery and fetch quests. To begin with, we're creating an Item, and placing it in the player's inventory (by invoking its `touch`). Next, we're creating a new entity called Location (we'll get to this at the end). This entity is created in the Overworld (hence the Map context switching), and randomly placed on land. With the Location placed (at `x` and `y`), we call getIslandAt to find out which island is the target of our quest, and set quest's `island` to it. Again, this is why we want to ensure we have a valid island in getIslandAt. With all that done, we set up the quest steps we want to follow, and add some message boxes for the requester. Note how the first step is an INTERACT command, using the id of the Location we created. This will mean our quest progression will be paused until we touch the Location. So, in summary, we're giving the player an Item, creating a Location in the overworld, and integrating it into our quest steps. An example of a fully complete dispose script from this part might look like this: INTERACT 99 START_DISPOSE_QUEST INTERACT 176 REMOVE_INVENTORY_ITEM 175 UPDATE_DESCRIPTION Return to Terry Silver PULSE_ENTITY 99 1 SET_TOWN 5 INTERACT 99 PULSE_ENTITY 99 0 MSG_BOX Terry Silver;You've done it? Great! I hope no one saw you. MSG_BOX Terry Silver;You ... um ... might want to make yourself scarce for a few days. COMPLETE_QUEST Translated, line by line, this script would mean: - Wait for the player to interact with entity #99. - Setup the dispose quest. - Wait for the player to interact with entity #176. - Remove item #175 from the player's inventory - Update the quest description, with the text "Return to Terry Silver". - Make entity #99 pulse - Set the Quest's town to the one with id #5 - Wait for the player to interact with entity #99 - Make entity #99 stop pulsing - Add a message box, for Terry Silver, saying "You've done it? Great! I hope no one saw you." - Add a message box, for Terry Silver, saying "You ... um ... might want to make yourself scarce for a few days." - Complete the quest That should all make complete sense, as it is what we'd expect of having to get rid of something for someone.
That's all there is to quests.c! That couldn't have gotten easier. We now need to just look at the new Location entity we've created. This is handled in location.c, where you'll find just two functions: initLocation and `touch`. We'll start with initLocation:
Absolutely nothing special here - we're creating a non-solid entity, with a texture, and a `touch` function. The `touch` function is the most important part:
When the player touches the location, we'll call doQuestInteraction, to trigger any quest steps that are waiting on an interaction with this location. If so, we'll mark the Location as `dead`, to remove it from our game. If we wanted a little more control over this sort of thing, we could add a quest step to remove the entity, so it doesn't always happen. In our game, however, we don't want our Locations hanging around once the item has been buried, since it has served its purpose.
And that's it, that's all we had to do to create a dispose quest. As you can see, creating quests in our game is simple once we have everything set up (and creating such a quest system itself is actually not too tricky, once you reflect on it). Our game is now more or less done. What we shall do in the final part is add in some sound and music, ensure our quests are randomly picked, and tidy a few things up. Purchase The source code for all parts of this tutorial (including assets) is available for purchase, as part of the SDL2 tutorials bundle: From itch.io |