How do I retrieve values from a multi-dimensional array?

0 favourites
  • 4 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Well, I'm setting multidimensional array values from a JSON file to a C2 Array object:

    Take something like this for example:

    {
    	"c2array": true, 
    	"size": [3,2,1], 
    	"data": [
    		[
    			["title"],
    			["desc"]
    		],
    		[
    			["Frog"],
    			["Very green"]
    		],
    		[
    			["Camel"],
    			["Very yellow"]
    		]
    	]
    }
    [/code:30q6fhyr]
    
    I have something like this to load it up:
    
    [code:30q6fhyr]
    [On Start Layout ] -> AJAX -> Request words.json (tag "words")
    [On "words" completed] -> Words (Array) -> Load from JSON string AJAX.LastData
    [/code:30q6fhyr]
    
    Now I'm trying to use the data in this array in a Text object that I've programmatically spawned:
    
    [code:30q6fhyr]
    [On Start Layout] -> System -> Create Text on layer 0 at (200, 200)
    [On created Text]
        [Words is empty]
        [else]
            [Text] -> set text to Words.At[0,0,0]
    [/code:30q6fhyr]
    
    Using the .At() function, I'm only getting a value of "0" and not the text from the multidimensional array. Anyone know why this is so? The JSON data appears in the array viewer in the debugger.
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • It looks like you're trying to access the array before it has loaded. The "on start of layout" events that request the file and create the text object happen on the first tick. The "on 'words' completed" event happens some time later when the file has loaded. You'd want to move "set text to Words.At(0,0,0)" to the "on 'words' completed" event.

  • check capx from the last post

  • Thanks guys, the function object was the answer. ramones - if I move that in there, the Text object won't initialize. So I called a function to create the Text object and initializing it upon creation works fine.

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