How do I use tokenat on Dictionaries?

Get help using Construct 2

Post » Fri Nov 25, 2016 3:28 pm

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:
Image

My Dictionary keys/values:
Image

The output:
Image

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.)
B
10
S
2
G
2
Posts: 13
Reputation: 1,576

Post » Fri Nov 25, 2016 5:36 pm

try
Code: Select all
"Hello " & tokenat(playerDb.Get(playerDb.CurrentKey),2,",")
Image
B
63
S
23
G
7
Posts: 1,479
Reputation: 10,794

Post » Fri Nov 25, 2016 6:04 pm

korbaach wrote:try
Code: Select all
"Hello " & tokenat(playerDb.Get(playerDb.CurrentKey),2,",")


Nope, still the same output. :(
B
10
S
2
G
2
Posts: 13
Reputation: 1,576

Post » Fri Nov 25, 2016 7:02 pm

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.
ImageImageImage
B
71
S
21
G
220
Posts: 3,646
Reputation: 123,721

Post » Fri Nov 25, 2016 7:12 pm

Image
B
63
S
23
G
7
Posts: 1,479
Reputation: 10,794

Post » Fri Nov 25, 2016 7:31 pm

korbaach wrote:helloAjax.capx


Yeah, looks like I'm running into a bug. :(
B
10
S
2
G
2
Posts: 13
Reputation: 1,576

Post » Fri Nov 25, 2016 7:33 pm

blackhornet wrote: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. :(
B
10
S
2
G
2
Posts: 13
Reputation: 1,576

Post » Sat Nov 26, 2016 7:37 am

korbaach wrote: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).
B
10
S
2
G
2
Posts: 13
Reputation: 1,576

Post » Sat Nov 26, 2016 2:36 pm

KuJoe wrote:hopefully it's just something I'm doing wrong on my end.

Wrong Json format!

Dictionary or Associative array
The Dictionary object stores strings and numbers. Each value has an associated key
"name": "Joe"
"score": 100

my example:
Code: Select all
{
   "c2dictionary": true,
   "data": {
      "0": "13,1,KuJoe",
      "1": "14,2,Tester Bot",
      "2": "16,4,Shadow",
      "4": "17,"6",Tinner"
   }
}


your json:
Code: Select all
{
   "c2dictionary": true,
   "data": [
      ["13","1","KuJoe"],
      ["14","2","TesterBot"],
      ["15","3","Hello"],
      ["16","4","Shadow"],
      ["17","6","Tinner"]
   ]
}

you don't have keys inside "data"..but muliple arrays...
so you can't access dictionary data...
Image
B
63
S
23
G
7
Posts: 1,479
Reputation: 10,794

Post » Sat Nov 26, 2016 2:48 pm

korbaach wrote:
KuJoe wrote:hopefully it's just something I'm doing wrong on my end.

Wrong Json format!

Dictionary or Associative array
The Dictionary object stores strings and numbers. Each value has an associated key
"name": "Joe"
"score": 100

my example:
Code: Select all
{
   "c2dictionary": true,
   "data": {
      "0": "13,1,KuJoe",
      "1": "14,2,Tester Bot",
      "2": "16,4,Shadow",
      "4": "17,"6",Tinner"
   }
}


your json:
Code: Select all
{
   "c2dictionary": true,
   "data": [
      ["13","1","KuJoe"],
      ["14","2","TesterBot"],
      ["15","3","Hello"],
      ["16","4","Shadow"],
      ["17","6","Tinner"]
   ]
}

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.
B
10
S
2
G
2
Posts: 13
Reputation: 1,576

Next

Return to How do I....?

Who is online

Users browsing this forum: ksiads and 1 guest