How to read an XML and make your game talk

10

Attached Files

The following files have been attached to this tutorial:

.zip

Stats

10,454 visits, 16,834 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 14 Jan, 2014. Last updated 19 Feb, 2019

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 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.

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.

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.

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.

  • 4 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • Without you pointing out the importance of ' and " to adress Numbervariables in XML attributes I would have been stuck forever. The manual is not helpful and the info there about Stringvalue and Numbervalue is way to barebones.

  • thank you

  • Hi, a bit sometimes to comment this. When I want to run this nothing happen. I am using a Business license.

  • I had some issues so I replaced the speech file and just edited and changed some code to make it work! Thank you for this tutorial, especially including the zip file!