0 Favourites

How do I use tokenat on Dictionaries?

  • I'm stumped, I've been searching for a while now and all of the threads on the forum that include examples of tokenat make me think my code would be working but alas it's not. I'm using AJAX to grab JSON output from a PHP file (from MySQL) and loading the data into a Dictionary, but all attempts to pull any data from any Dictionary value is not working.

    My code:

    My Dictionary keys/values:

    The output:

    I'm not sure if it's the last of sleep or what but this doesn't make sense to me.

    (I'm using the latest stable version of Construct 2 if that matters.)

  • try

    "Hello " & tokenat(playerDb.Get(playerDb.CurrentKey),2,",")[/code:2pqfksin]
  • try

    "Hello " & tokenat(playerDb.Get(playerDb.CurrentKey),2,",")[/code:mobn2m4u]
    

    Nope, still the same output.

  • Do you have more than one playerDB object? I tried a similar example, that worked for one dictionary, but if I create a second, the tokenat code shows blanks. If I add a Foreach palyerDB above the For each key, it then works.

  • helloAjax.capx

    Yeah, looks like I'm running into a bug.

  • Do you have more than one playerDB object? I tried a similar example, that worked for one dictionary, but if I create a second, the tokenat code shows blanks. If I add a Foreach palyerDB above the For each key, it then works.

    I had another Dictionary but I deleted it after creating the 2nd one. I tried the "Foreach" above the "Foreach Key" and it didn't work. Probably a bug.

  • helloAjax.capx

    This capx works correctly but if I replace the TXT file with the remote PHP file it doesn't work. I've opened a bug report here for anybody else who comes across this issue (hopefully it's just something I'm doing wrong on my end).

  • hopefully it's just something I'm doing wrong on my end.

    Wrong Json format!

    Dictionary or Associative array

    [quote:ygmxs0nb]The Dictionary object stores strings and numbers. Each value has an associated key

    "name": "Joe"

    "score": 100

    my example:

    {
    	"c2dictionary": true,
    	"data": {
    		"0": "13,1,KuJoe",
    		"1": "14,2,Tester Bot",
    		"2": "16,4,Shadow",
    		"4": "17,"6",Tinner"
    	}
    }[/code:ygmxs0nb]
    
    your json:
    [code:ygmxs0nb]{
    	"c2dictionary": true,
    	"data": [
    		["13","1","KuJoe"],
    		["14","2","TesterBot"],
    		["15","3","Hello"],
    		["16","4","Shadow"],
    		["17","6","Tinner"]
    	]
    }[/code:ygmxs0nb]
     you don't have keys inside "data"..but muliple arrays...
    so  you can't access dictionary data...
  • > hopefully it's just something I'm doing wrong on my end.

    >

    Wrong Json format!

    Dictionary or Associative array

    [quote:226gkk3r]The Dictionary object stores strings and numbers. Each value has an associated key

    "name": "Joe"

    "score": 100

    my example:

    {
    	"c2dictionary": true,
    	"data": {
    		"0": "13,1,KuJoe",
    		"1": "14,2,Tester Bot",
    		"2": "16,4,Shadow",
    		"4": "17,"6",Tinner"
    	}
    }[/code:226gkk3r]
    
    your json:
    [code:226gkk3r]{
    	"c2dictionary": true,
    	"data": [
    		["13","1","KuJoe"],
    		["14","2","TesterBot"],
    		["15","3","Hello"],
    		["16","4","Shadow"],
    		["17","6","Tinner"]
    	]
    }[/code:226gkk3r]
     you don't have keys inside "data"..but muliple arrays...
    so  you can't access dictionary data...
    
    
    Thanks, I'm still trying to wrap my head around the formatting for Construct 2 and I'm not strong  with JSON as it is. I was under the impression that Construct 2 would reject the JSON if it was bad like it does for Arrays. I'll look into trying to figure out how to get my PHP file to format it correctly now. Thanks again.
  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Turns out the problem was me not setting the key values even though they were automatically filled in with numbers. Consider this thread solved.

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