(Resolved) Load XML values and add them to a Glabal Variable

0 favourites
  • 14 posts
From the Asset Store
Easily store, modify, read and manipulate colors with Color Variables!
  • Hi there!

    I trying to make something very specific.

    For example, I have different objects supposed to be stage. Each stage has a unique variable number(1 for stage 1, 2 for stage 2,... ). I also have a xml file that contains multiple value for each stage (its name, the cost of level 1, 2 and 3 (yes, you can upgrade Stage)).

    So, I want to load that xml file, then get a value (stage name and/or Level cost) and add to the selected stage all those informations. the name should appear in the Text object and to cost to a global variable.

    The problem is that I'm not sure that it's even possible. I think of this method to get all the stages values in a single xml file, so I won't have to change everything in the capx file.

    (The capx file I create is attached)

    If anyone have a solution for this...

    Thanks for your help!

  • You don't need the extra quotes in the file, just the C2 string.

  • Well, I got a syntax error if I don't put them... How can I put the Xpath without the quotes?

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • <?xml version="1.0" ?>

    <stages>

    <stage>

    <ID>1</ID>

    <name>The First Stage</name>

    <Level1>5</Level1>

    <Level2>500</Level2>

    <Level3>5000</Level3>

    </stage>

    <stage>

    <ID>2</ID>

    <name>The Second Stage</name>

    <Level1>6</Level1>

    <Level2>600</Level2>

    <Level3>6000</Level3>

    </stage>

    <stage>

    <ID>3</ID>

    <name>The Third Stage</name>

    <Level1>7</Level1>

    <Level2>700</Level2>

    <Level3>7000</Level3>

    </stage>

    <stage>

    <ID>4</ID>

    <name>The Fouth Stage</name>

    <Level1>8</Level1>

    <Level2>800</Level2>

    <Level3>8000</Level3>

    </stage>

    <stage>

    <ID>5</ID>

    <name>The Fifth Stage</name>

    <Level1>9</Level1>

    <Level2>900</Level2>

    <Level3>9000</Level3>

    </stage>

    </stages>

  • Just to clarify further, blackhornet is referring to the quotes around the text within the StageTest.xml file. Just remove the start and end quotes.

  • Ah ok! Thanks, I get it.

    But it doesn't solve my issue in fact. My datas aren't loaded yet.

    Is it something in the events I miss?

  • You also had double quotes in this string, wrapping the "1.0":

    <?xml version="1.0" ?>

  • Alright, thank you guys. I change my xml file and now it's properly loaded.

    However,I get back now to my main question: can I just load the node that is equal to my global variable (named "Stage") ?

    Actually it only load the first node of the xml.

    I try different sting value like

    XML.StringValue("/stages/stage[ID='" & Stage & "']/name")[/code:2ca6fujm]  (I get that from another topic) but i fail...
  • Alright, thank you guys. I change my xml file and now it's properly loaded.

    However,I get back now to my main question: can I just load the node that is equal to my global variable (named "Stage") ?

    Actually it only load the first node of the xml.

    I try different sting value like

    XML.StringValue("/stages/stage[ID='" & Stage & "']/name")[/code:a8uxp7cm]  (I get that from another topic) but i fail...
    

    This should work

    "/stages/stage/ID[contains(text(), '" & stage & "')]/following-sibling::name/text()"[/code:a8uxp7cm]
    ---
    
    If you designed your xml like below might be easier
    
    [quote:a8uxp7cm]<stages>
    <stage id="1">
    <name>The First Stage</name>
    <Level1>5</Level1>
    <Level2>500</Level2>
    <Level3>5000</Level3>
    </stage>
    </stages>
    
    
    [code:a8uxp7cm]"/stages/stage[@id='"&stage&"']/name/text()"[/code:a8uxp7cm]
  • Still don't work. I designed my xml as you recommended IndieKiwi, but it doesn't change anything

  • Still don't work. I designed my xml as you recommended IndieKiwi, but it doesn't change anything

    The xpath is working for me. Its the other part of your logic that is broken, I've hardcoded Stage = 1, and used the first xpath from my other comment set to a new textfield named "test". It sets the value to "The First Stage" when its run.

    Changing Stage=2 would be The Second Stage etc.

  • Hello IndieKiwi,

    Thanks to your help, I finally make it!

    In fact I just remove the Functionand replace it by a "On Every trick" event. Now it properly display the correct node from the XML and also replace the chosen global variable by the XML datas!

    I attached the new file here as an exemple if anyone have the same issue!

    By the way, I get back to the original XML design because it didn't work with your design:

    <stages>
    <stage id="1">
    <name>The First Stage</name>
    <Level1>5</Level1>
    <Level2>500</Level2>
    <Level3>5000</Level3>
    </stage>
    </stages>[/code:rc8apsej]
    
    That could be maybe easier to code but it's working like this, so it's fine.
  • That is great! but i recommend not using on every tick as it could be constantly assigning the same text and drawing the text onto the canvas, which will have performance issues on something that is essentially static. Use something like a "do once", "for loop", on "layout load" etc could perform better.

    I haven't explored ajax or xml in construct 2 yet so i learnt something new too

  • This is a great example. It's nice to read from xml files. But what about saving back to xml file ? Is there a way to create a xml file, and save them into it? (For windows, node-webkit projects) .

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