How can I list the class names?

0 favourites
  • 7 posts
From the Asset Store
Template for scrollable list, fully documented in comment and video
  • I know that I had this kind of structure working before. But I'm kind of unconcentrated right now and I just can't get it to work.

    What I'm trying to do is just reading the class names "Name1" to "Name4" from a JSON object.

    What is the path to it? Why don't relative paths work?

    I tried so many different ways to read them. The screenshot below just has about half of everything that I tried.

    Only this exact path works Data.Get("classes.class_01.name"). I thought that inside a for each entry loop the path will be set relative. Why doesn't Data.Get(".name") work if the logic iterates through each entry of classes (class_01 to class_04)?

    Thanks for reading!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yeah, relative path Get(".name") should work inside of For Each Entry in "classes" loop.

    Try Browser Log Data.Path, see what it outputs.

    I also recommend this little project, it shows paths to all keys in JSON:

    dropbox.com/s/srgf9lme08by9wa/JSON-RecursiveRead.c3p

  • Thank you for the example project. Very handy.

    I realized why it wasn't working before. Because I used an object for the classes instead of an array.

  • Because I used an object for the classes instead of an array.

    It should've still worked - you should be able to access the first class name using Get(".name") in For Each entry in "classes" loop.

  • Hmmm. I don't know why it didn't work.

    {
    	"classes": {
    		"class_01": {
    			"name": "Name1"
    		},
    		"class_02": {
    			"name": "Name2"
    		},
    		"class_03": {
    			"name": "Name3"
    		},
    		"class_04": {
    			"name": "Name4"
    		},
    	}
    }

    ..wasn't working with a relative path.

    {
    	"classes": [
    		{
    			"name": "Name1"
    		},
    		{
    			"name": "Name2"
    		},
    		{
    			"name": "Name3"
    		},
    		{
    			"name": "Name4"
    		},
    	]
    }

    ..was working fine with a relative path.

    Wich is a shame because I would prefer having a JSON path rather than using an array so I can do things like classes.warrior.attack. I guess I could have an array with the class names for easily listing them and then still have their own paths as objects with their class stats in them.

  • There is an extra comma after class_04 in your JSON string. You can use any online JSON validation tool to test.

    If you remove the comma, then it works fine:

  • Oh yeah, my bad. In my previous post I had an extra comma at the end. But in my original JSON file I don't have this mistake.

    I will try it again though.

    Edit:

    Ok strange. Now it just works the way that I tested it before. Maybe I had a formatting issue or something like that...

    Thanks for your help!

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