[Tutorial]A dialog box system using XML

0 favourites
From the Asset Store
Over 2700 files with all the colour combinations from a 15 colours palette!
  • The reason to use IDs is to uniquely identify each of the dialogue. So if you want a particular dialogue, you can retrieve it using its ID, whatever its index is in the parent node, and wherever it is in the document.

    You should always prioritize the use of attributes to distinguish nodes instead of using different tag names. The structure of your XML document will be much more robust and easier to navigate through.

  • I mostly used the XML structure in the same way we can do picking in Construct 2.

    So in the end, in my tutorial, you are picking a dialog/answer/whatever through certain conditions (their attributes) in the same way you pick instances (through their instance variables).

    It may be kind of tedious for the organization, but it made sense to me as I was going along, and especially when I needed to link the logic of my XML to my display engine in the tutorial.

    To be fair I'm not super familiar with XML either, and I'm afraid there is not much more I can tell about the "best usage practice" of it.

    Again, for this tutorial that's what felt natural as I was going along, and still "readable" (allowing me to tie the logic of display/organisation to my code) nonetheless.

  • The reason to use IDs is to uniquely identify each of the dialogue. So if you want a particular dialogue, you can retrieve it using its ID, whatever its index is in the parent node, and wherever it is in the document.

    You should always prioritize the use of attributes to distinguish nodes instead of using different tag names. The structure of your XML document will be much more robust and easier to navigate through.

    The tutorials I have seen on here have simply used IDs to number each node, id=1, id=2, Id=3, etc this could be replaced by tag[1].

    I will keep in mind to try and make use of attribute tags though.

  • Attributes can be anything you want, I guess tutorials used "id" because it's short and is usually quite self-explanatory. The values could have been something else though, and maybe more verbose to include a bit of metadata (i.e <npc id="Guard1_Town1">...</npc>).

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Magistross It's not that I don't understand how to use them, I am just trying to figure out which method is efficient in different situations. I am still not sure how I can make use of IDs if they are unique, but I can see how attributes can be used effectively—something like this:

    Guard1 NormalMessage

    Guard1 SpecialMessage

    Guard2 NormalMessage

    Guard2 SpecialMessage

    So you can search for multiple attributes to narrow down the result?

    I guess if using nested tags is similar to using folders then using attributes and ids is similar to a search function?

  • A unique ID provides a way to associate a particular XML node to a particular Construct object. Yes you could assiociate an index, but indexes are volatile, they change as soon as you insert new data (unless you always append). IDs will stay the same until you change them, even if you insert new data right in the middle of your file.

    Suppose you are interacting with an npc, and you know its IDs, reading the right XML with XPath is as simple as /root/npcs/npc, and this statement will always return the npc node with id = "myId", even if its index changes.

  • A unique ID provides a way to associate a particular XML node to a particular Construct object. Yes you could assiociate an index, but indexes are volatile, they change as soon as you insert new data (unless you always append). IDs will stay the same until you change them, even if you insert new data right in the middle of your file.

    Suppose you are interacting with an npc, and you know its IDs, reading the right XML with XPath is as simple as /root/npcs/npc, and this statement will always return the npc node with id = "myId", even if its index changes.

    I get that but I don't get why using ID would benefit over doing this:

    root

    -npc

    --tom

    ---scene1

    ----message hello there /message

    ----message can i help you /message

    ---/scene1

    ---scene2

    ----message welcome back /message

    ---/scene2

    --/tom

    --john

    ---scene1

    ----message

    etc.

    Would you really want to have IDs that long? id=tomScene1Message1; id=tomScene2Message1; etc.

    And the indexes being volatile was actually the benefit I was initially pointing out. What if you have 20 lines of dialogue in a scene to tell a story then want to insert another line after the first—you will have to change 19 IDs. Not horrible . . .until you change your mind again ...

  • I'd go with something like this :

    root

    -npcs

    --npc id="tom"

    ---scene

    ----message hello there /message

    ----message can i help you /message

    ---/scene

    ---scene

    ----message welcome back /message

    ---/scene

    --/npc

    --npc id="john"

    ---scene

    ----message

    ...

    Grouping all npc under "npcs" would allow you to do a for each node /root/npcs/npc, load stuff inside objects and be done with XML parsing.

    Also, IDs aren't indexes, we aren't numbering stuff, we're giving names per say. I don't see how it would force us to change the previous ones as we insert new data.

  • Also, IDs aren't indexes, we aren't numbering stuff, we're giving names per say. I don't see how it would force us to change the previous ones as we insert new data.

    My initial post was asking why tutorials were doing:

    Id=1

    Id=2

    Id=3

    Etc

    They also had the same path and tag, so that was my suggestion to use index instead of ID. I believe that was my only time recommending using index.

    So I guess we are in agreement that using IDs to "number stuff" isn't a great practice?

  • In the end, it all comes to preference and how your XML is built. If you are manually creating the file, using numbers to identify nodes is a sure way to get lost in the long run! But say you are building your XML from a database, it would be completely normal and fine to have "auto_increment" IDs.

    That said, the question of whether or not you should be using indexes in your XPath depends on what you are doing. If you are iterating nodes, you can either use them or do a "for each node", but if you are reaching for a particular node, you should use its ID. Kyatric's example also had links between nodes, those should also be done with IDs, and not indexes.

  • Hello everyone.

    First I want to thank you for this wonderful dialog system.

    Just a quick question: I wonder how to display only particular discussion or dialog.

    Let say I have sprite.. and on collision with this sprite I want to display discussion id=1

    And then I have different sprite, so on collision with that sprite I want to display discussion id=2 etc..

    Please give me advice how can I do this?

  • Hello everyone.

    First I want to thank you for this wonderful dialog system.

    Just a quick question: I wonder how to display only particular discussion or dialog.

    Let say I have sprite.. and on collision with this sprite I want to display discussion id=1

    And then I have different sprite, so on collision with that sprite I want to display discussion id=2 etc..

    Please give me advice how can I do this?

    I usually would give them an instance variable then use the instance variable in the path. As per the discussion above I might change the system to using attributes/IDs instead of different tags but here is an example:

    XML.StringValue("/content/"&Interactables.class&"/"&Interactables.name&"/message/text()")

    So in this example Interactables is family (if you can't use family just make one object with different instances) an the class is either set to NPC or Sign, but works the same way, and the name is whatever name I give it. So my xml would be:

    -content

    --npc

    ---tom

    ----message Hello!

    But planning on eliminating the tom tag and giving the npc tag an id of tom.

    Magistross, I was going to ask if you can give an example of when you have used a for each xml node? I can't think how to implement it in any of my current situations.

  • I did an example a while back that iterated through a list of nodes to build said list but on the UI. Might not be exactly what you wanted, but whenever you need to iterate through all nodes, this is the way to go. Note that in a "for each node", subsequent XPath calls are relative to the current path.

    https://dl.dropboxusercontent.com/u/700 ... ample.capx

  • Hello again

    I think I need a little bit help here.

    I was trying to implement dialog system to my game. So here is result. I have prepared project file under this link:

    http://mmphotography.ie/game/elf_game.capx

    In the game you will meet Fairy and Troll.. As you will see I have prepared dialogs for Fiary and Troll.

    However every time when I meet Fairy I can see all the dialogs. Same with Troll..

    So my question is how to display right dialog for Fairy and Troll.. ?

    Feel free to modify code. Any advice - much appreciated. Thanks.

  • I just did a version of a dialogue box with XML, too bad I didn't see your tutorials first haha

    You can try it here: https://www.scirra.com/arcade/other-games/dialogue-box-for-lazy-people-8915

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)