How do I show a random node from a XML

Get help using Construct 2

Post » Thu Apr 10, 2014 8:28 am

construct2.jpg


I'm trying to show a random question from a XML. The variable sporsmalsnr holds a random number. For each node a counter increments. Then I want to loop through all nodes, and show the node where counter = sporsmalsnr. Somehow it doesn't work, as it only shows the first question.
You do not have the required permissions to view the files attached to this post.
Image
B
14
S
4
Posts: 145
Reputation: 1,943

Post » Thu Apr 10, 2014 1:38 pm

Well, giving it the limitations (round(random(1,2))) will do that, as you're only letting it have one number and that is 1. Try making it (round(random(0,10)))
Using R243 for all examples
B
69
S
20
G
6
Posts: 379
Reputation: 9,699

Post » Fri Apr 11, 2014 7:02 am

I have tested it. round(random(1,2)) gives integer 1 or 2. I have two questions in the XML, but only first question is showing.

Even when question number is 2 and counter is 2, it just shows the first question anyway. So it seems that if they are similar, it starts from the first node.
Image
B
14
S
4
Posts: 145
Reputation: 1,943

Post » Fri Apr 11, 2014 4:18 pm

Isn't it zero based? so 1st node is 0...2nd node is 1.. Try using round(random(0,1)) instead
Please tag me with @caiorosisca if your post is refering to me :). I don't always follow the topics I answer.
B
11
S
4
G
1
Posts: 272
Reputation: 1,643

Post » Fri Apr 11, 2014 4:56 pm

XPath use 1-based index, so to get the first question, use this :
Code: Select all
/questions/question[1]/@text"

For the 2nd :
Code: Select all
/questions/question[2]/@text"

And so on so forth.

In your case you could just ditch the for each node and directly use the action set text to :
Code: Select all
XML.StringValue("/questions/question[" & sporsmal & "]/@text")
B
69
S
28
G
24
Posts: 977
Reputation: 18,739


Return to How do I....?

Who is online

Users browsing this forum: Fenix18 and 52 guests