[Tutorial]A dialog box system using XML

Get help using Construct 2

Post » Tue Apr 05, 2016 12:39 am

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[@id="myId"], and this statement will always return the npc node with id = "myId", even if its index changes.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Tue Apr 05, 2016 2:45 am

Magistross wrote: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[@id="myId"], 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 ...
ImageImageImage
B
44
S
26
G
11
Posts: 518
Reputation: 10,502

Post » Tue Apr 05, 2016 4:41 am

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.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Tue Apr 05, 2016 4:56 pm

Magistross wrote:
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?
ImageImageImage
B
44
S
26
G
11
Posts: 518
Reputation: 10,502

Post » Tue Apr 05, 2016 7:17 pm

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.
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Sun Apr 10, 2016 9:16 am

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?
B
18
S
6
G
3
Posts: 23
Reputation: 2,934

Post » Sun Apr 10, 2016 6:01 pm

eonysko5 wrote: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.
ImageImageImage
B
44
S
26
G
11
Posts: 518
Reputation: 10,502

Post » Sun Apr 10, 2016 7:06 pm

@badmoodtaylor 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
B
75
S
31
G
27
Posts: 1,000
Reputation: 20,735

Post » Mon Apr 11, 2016 12:09 pm

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.

Image
B
18
S
6
G
3
Posts: 23
Reputation: 2,934

Post » Tue Jun 21, 2016 10:43 pm

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
B
18
S
9
G
17
Posts: 84
Reputation: 10,519

PreviousNext

Return to How do I....?

Who is online

Users browsing this forum: warmty and 13 guests