How to read an XML and make your game talk

Favourite 35 favourites
Tutorial written by emoaedenOriginally published on 14th, January 2014

Introduction

How cool would it be if you could have a single external script with all the dialogue for your game, that you can also use to make your game talk to you ? Well ideally, that's what this tutorial will help to show you.

Requirements

You will need the following files
* Construct 2 [R157 or above]
* An XML file
* Chrome Canary

How to make an XML

There are a fair few products on the market that will let you save a file you have written as an XML, but I find that when making one, it's best to start from scratch to prevent a bunch of junk/unnecessary code being written.

I just use Notepad++ when I write mine, and for the purpose of this tutorial, the screen shots will be from said program.

The following is the code that I will be using :XML file layout

     <?xml version='1.0' ?>
    	<chapter1>
    		<hint id='intro1'>Good morning Mr Bond, how did you sleep ?</hint>
    		<hint id='intro2'>I have prepared your breakfast for you</hint>
    		<hint id='intro3'>Come find me when you're finished</hint>
    	</chapter1>

And it is important that you use single talking marks where specified and not double talking marks, and visa versa.

Project File

Setting it all up

This next section will deal with the Construct 2 project, and the different objects you will need.

* Text Box
* XML
* AJAX
* User Media
* Button

Once they are all added, it's time to add the XML file that you created earlier. You can do this by right clicking on Files under the Project Dock and then navigating to the location of your file.

Now you can either make the User Media sit out of view of the screen, or just give it an opacity of 0 to hide it.

Adding the code

If you have never done something like this before, don't despair if you mess up the code, as I will include a ZIP and CAPX for you to compare with at the end.

Step 1 : Counter for lines

This is just a simple Global Variable (or if could be a local or objects variable, if you want to try something a bit more advance) and it will be a number.Adding the variable


Step 2 : Reading the XML

You will need to use the AJAX object to request the XML that we imported earlier in the tutorial, and as soon as that is done, we're going to load that XML into the XML object.

It should look like the following image, and once done, you're only two steps away from having a basic dialogue going.Reading the XML


Step 3 : Reading the first line

This step allows the CAPX to read the first line of the XML file, and start the game off right away. The text that is being read reads as follows :

    xml.StringValue("/chapter1/hint[@id='intro1']/text()")

Apologies now if the code is illegible in the images. The important part of this is to ensure that you use double talking marks " at the start and end and a single talking mark ' when referencing the id.Almost there


Step 4 : Progressing when clicked

This last bit is just to progress the dialogue when you have finished listening to/reading it. Again I apologise for the image, but if you open the attached CAPX you can see exactly what it reads.

This list of code differs slightly from the previous one, as it is now being fancy and using the Variable we created earlier to determine what line is read next.

    xml.StringValue("/chapter1/hint[@id='intro"&NumberOfLines&"']/text()")

As I mentioned earlier, the use of ' and " is important, and as this is an intermediate tutorial, I hope you already understand the use of &ReferencingAVariableLikeThis&, if not, look into the Text Box tutorials as they can provide insight.


All Done

Hopefully if you have done everything correct, you should be able to open your project in Chrome Canary (by setting it as your custom browser) and then enjoy the possibilities that this tutorial has opened up for you.

Notes

I have used en-GB (Great Britain) instead of en-US (United States) for the language, purely as I prefer the way it sounds, and given it was about James Bond it seemed apt.

And if you have any queries/suggestions/comments, feel free to inbox me or leave the comments below.

Unlock your full gamedev potential

Upgrade to the Personal Edition of Construct 2, it has way more features and won't holding back from making money and using your full creativity like the free edition does. It's a one off payment and all Construct 2 editor updates are free for life!

View deals

Plus, it's got a lot of additional features that will help you save time and make more impressive games!

Congratulations on finishing this tutorial!

Did you learn a lot from it? Share it now with your friends!

Share and Copy this Tutorial

You are free to copy, distribute, transmit and adapt this work with correct attribution. Click for more info.

Comments

2
Lordshiva1948 44.3k rep

This is beyond me but, it's nice that you share your knowledge with everyone

Tuesday, January 14, 2014 at 10:23:22 AM
6
rozpustelnik 6,174 rep

Can You provide images in slightly higher resolution? They're hard to read now. Nonetheless it's very good tutorial.

Tuesday, January 14, 2014 at 10:38:19 AM
0
AllanR 18.5k rep

Cool! That is awesome. I could change the voice from en-GB to en-US, but changing the rate and pitch didn't seem to make any difference...

Tuesday, January 14, 2014 at 4:48:35 PM
0
emoaeden 10.3k rep

The images are contained within the accompanying ZIP file have said images. That said, I will try to get higher res ones in the next week or so.

Tuesday, January 14, 2014 at 9:57:55 PM
0
JohnnySix 12.0k rep

Great example, I tried adding some more lines and re-uploading the xml - but it seems to cache the XML, which is rather frustrating, as I was hoping to use something like this to make an blog/link thing. Is it possible to force the AJAX to load the most recent version rather than cached?

Saturday, January 18, 2014 at 5:46:54 PM
0
AbelaNET 14.9k rep

Thanks for sharing

Monday, January 20, 2014 at 11:00:46 PM
0
emoaeden 10.3k rep

JonnySix : You could reference an external XML, and do it that way.

Tuesday, January 21, 2014 at 6:26:38 AM
0
JohnnySix 12.0k rep

@emoaeden - thanks dude- it looks like there are workarounds, I'll try put it to Ashley on the forums if I can to add it as a feature in the plug-in. There are ways to prevent caching but without writing a custom plugin, aren't available in the vanilla one.

Monday, January 27, 2014 at 9:40:13 PM
0
Ubru 999 rep

Thank you for sharing this. It sounds interesting.

Sunday, March 09, 2014 at 9:58:16 AM
0
Dwayne 417 rep

Works great - yes caching is a little grr - but I just rename the xml file each time I edit it and change the call in the event list and it works. Would love to figure out the workaround for the caching issue. This inspires some killer game ideas! Again thank you emoaeden!

Friday, March 21, 2014 at 1:25:19 AM
0
GeorgeZaharia 48.2k rep

great xml tutorial.. however by the title i was expecting something else :) ..ehm... good job :P

Monday, May 30, 2016 at 10:39:55 PM
0
Mylon 1,044 rep

I can't read any of the attached screenshots. They're so tiny.

Thursday, July 14, 2016 at 2:27:30 PM
0
KoolEcky 16.3k rep

Great info, thanks very much

Saturday, September 24, 2016 at 5:11:30 PM
0
luan00 329 rep

How can I import XML file with Vietnamese font. I try with this tutorial but it makes font error.

Friday, January 20, 2017 at 9:56:23 AM
0
DIS GUY NAMED CT 204 rep

how do to the Global number part ????

Tuesday, May 08, 2018 at 1:12:36 PM

Leave a comment

Everyone is welcome to leave their thoughts! Register a new account or login.