Is there a way to mimic the Array's "IndexOf" expression for a JSON object?

0 favourites
  • 6 posts
From the Asset Store
112 High-Quality destruction sounds for videogames
  • I have a JSON object that contains several game object arrays (e.g. heroes, enemies, weapons, buildings, etc). I'm new to JSON, so I feel like what I need to do is easy and I'm just missing it.

    Basically, I want to say "find the building where the .objectName key = "weaponsmith" and return the value of the .tooltip key for that same building". Since the buildings are in an array, I can't just use JSON.Get() right off the bat because I don't know which index (e.g. .buildings.1, .buildings.2, etc) the "weaponsmith" is.

    I don't want to have to do a For Each loop through the entire building array for each building on the map (unless this is the only way to do it?).

    So, I think what I'm looking for is a JSON.IndexOf("") expression, which doesn't exist. Can anyone clear up how to do this or perhaps point out if I'm going about this incorrectly? Thank you.


    I thought of loading just the Building data into an Array object so I could use Array.IndexOf. However, the Array > Load from JSON action isn't doing anything when I feed it ObjectData.GetAsCompactString("objects.buildings"). Where ObjectData is the name of my JSON object. I verified the JSON string output is correct/valid. When I say it isn't doing anything, I mean the Array remains the default 10x1 array initialized to 0 after the Load from JSON action.

    So, question #2: How can you get a subset of a JSON object into an Array? C3 manual entries for JSON and Array don't address this specifically.


  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • I believe for "Array Load from JSON" to work, JSON string needs to be in a specific format, something like this:


    I guess the easiest option is to loop through all entries in "Buildings" every time you need to find a specific building.

  • To load "Buildings" into an array you need to do something like this:

  • Ahh, thank you. That looks familiar - it's from the JSON example project, isn't it? I had forgotten about that.

    I'll experiment with that. And I would only have to push the objectName to the array so I can capture the index. Then use that index for a JSON.Get(). Thanks again!

  • As you've seen we don't have a "indexOf" expression in the JSON plugin yet. But I believe it's not actually that useful, as it would basically only allow you to check the index of a string or a number in an array. The structure you have is an array of objects, and you want to check a specific key within each of those objects instead, which is a more complicated search.

    Typically I would suggest using a different structure instead, or if that isn't feasible then use a loop with a conditional inside ( if we implemented an expression for this is how it would work ).

    Looking at your JSON I would instead suggest

    	/* ... */
    	"buildings": {
    		"weaponsmith": { /* ... */ },
    		"weaponTraining": { /* ... */ },
    		"elementTraining": { /* ... */ }

    This only has 1 real limitations in comparison to your structure; you cannot have more than 1 "building" with the same "objectName". On the plus side you can now refer to that object using "objects.buildings.weaponsmith" without having to search for the key you need. So it's dependent on what you need out of your data.

  • Thank you for the reply and for the suggestion. The tactic of loading just the .objectName values into a 1D array and then using IndexOf is working great. However, I'll consider your suggested data structure as an alternative. I will need to support multiple instances of buildings, but perhaps I could name the keys weaponsmith1, weaponsmith2, etc. I'll see which way is cleaner. Maybe store the total number of weaponsmith instances (and other buildings) in a Dictionary or as another object in the JSON structure... just thinking out loud :)


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